scope

Ways to avoid that for-loop variables cut into Python's global namespace

╄→гoц情女王★ 提交于 2019-12-08 10:00:33
问题 I am wondering if there is a way to avoid that for-loop variables cut into Python's global namespace? The only solution I could come up with so far would be using closures, such as list comprehensions E.g., for the following code: i = 1 print([i for i in range(5)]) print(i, 'i in global') j = 1 for j in range(5): if j == 4: print(j, 'j in for-loop') print(j, 'j in global') prints [0, 1, 2, 3, 4] 1 i in global 4 j in for-loop 4 j in global EDIT: I assume that the for-loop namespace falls into

Not allowed to call my own methods (“static” mystery)

こ雲淡風輕ζ 提交于 2019-12-08 09:31:43
问题 I have a BroadcastReceiver as a separate class file, listening to sensor actions from a service in the background. I want it to show text on the screen on occassions. I want the BroadcastClass to call a method in my Activity class which takes care of the string to be shown. But my BroadcastReceiver class cannot call methods which are not static(?) "Cannot make a static reference to the non-static method showString(String) from the type myActivity" And if I make the particular method static in

How can I create a scope in a model that returns only those objects with an associated rank greater than the current_user's rank?

China☆狼群 提交于 2019-12-08 08:38:47
I am trying to create a :readable scope in my Page model to return all the Pages that the current_person has sufficient 'rank' to read: scope :readable, lambda { |current_person| joins(:role_readable) .where(:role_readable[:rank].gte(current_person.roles.first.rank) ) } I've tried many scope permutations, including this one, with no success (the one above gives a "can't convert Symbol into Integer" error). The problem is made more complex because Users (which handle authentication etc. / synonymous with accounts) have_many People , which represent the User 's presence in an organization — ie.

Ajax success with external variable

北城以北 提交于 2019-12-08 08:22:52
问题 How can I use an external variable i inside the Ajax success? For example: for (i = 0; i < 3; ++i) { $.ajax({ type: "POST", data: "user=132", url: "../php/order_ajax.php", success: function(data){ $('.obj' + i).html(data); } }); } 回答1: you should close it in for example anonymous function. It is because ajax call is asynchronous and I bet you that loop is finished even before the first ajax call is done which means that "i" will be 4 by that time. var user = 1; for (i = 0; i < 3; ++i) {

Passing 'this' scope to callback function

折月煮酒 提交于 2019-12-08 08:08:17
问题 I'm trying to pass 'this' to a callback function. I know a "suitable" way of doing this is setting this to a variable and using the variable... but that's not very elegant. var that = this; chrome.storage.sync.set(this.defaultOptions, function () { that.loadOptions(); }); I'm trying something else, but I can't figure out how to do it. Here's what I have tried: chrome.storage.sync.set(this.defaultOptions, (function () { this.loadOptions(); }).call(this)); But it's not working out. I'm getting

Ruby Instance Variables or Local Variables?

假装没事ソ 提交于 2019-12-08 08:01:53
问题 I am new to Ruby language. I understand that @@count: Class variables @name: Instance variables my_string: Local variables I keep the above in mind. However, I found one Ruby code like this: class HBaseController < ApplicationController ... def result conn = OkHbase::Connection.new(host: 'localhost', port: 9090, auto_connect: true) ... end end 'conn' confuses me a lot. Is 'conn' a instance variable, or local variable? And what is the scope of 'conn'? 回答1: I try to explain it with a little

Javascript variable not working. Why?

為{幸葍}努か 提交于 2019-12-08 07:59:35
问题 I'm not understanding something about variables in javascript. I am trying to change/calculate "offset" (with the variable "theOffset") either before the localScroll function occurs, or more preferably when you resize the window. None of the instances below work, accept for the "//initialize offset". How do I get the variable "theOffset" inside "$.localScroll" to change? jQuery(function( $ ){ //initialize offset var windowWidth = $(window).width(); if (windowWidth < 900) { theOffset = 0; }

Spring MVC - Can I autowire HttpServletRequest in RestController

[亡魂溺海] 提交于 2019-12-08 07:56:59
问题 Can I autowire HttpServletRequest in my RestController like following and will it returns different servletRequest even if it is executed in highly concurrent environment. I have a restriction that I can not have as method parameter because I am implementing an interface which is auto generated and will not have HttpServletRequest as the method parameter. @RestController public class MyController implements MyInterface { @Autowired private HttpServletRequest servletRequest; @Override

Use $scope variable in a JavaScript function called from the HTML code - AngularJS

流过昼夜 提交于 2019-12-08 07:52:30
问题 I'm using Angular and I'm trying to get $scope to use it in a JavaScript function. Here is an example: I have an html tag something like this: <a id="doSomething" onclick="javascript: doSomething(this);">Do Something</a> <div ng-show="flag">Click!</div> and a JavaScript function something like this: function doSomething(obj) { //$scope.flag = true; return true; } What I want is to use the $scope in this function. I haven't found a way to inject it. The thing is that I must use this way to

Autowire session-scoped bean into thread (Spring)

孤人 提交于 2019-12-08 07:36:08
问题 I have a session-scoped bean in Spring that is set within the web context. I have a task that runs as a Callable, and I need access to this bean from within that thread. How should I accomplish this? If I simply attempt autowiring the bean I get the error message: Scope 'session' is not active for the current thread The session-scoped bean I am injecting looks like this: <bean id="userInfo" class="com.company.web.UserInfoBean" scope="session"> <aop:scoped-proxy /> </bean> And the class I am