Variables viewer on Eclipse debugging evaluates Python property automatically - python

When I new some instance, its __ init __ will do, e.g.
self._regex = value
self._regex_dict = {}
In the Eclipse/PyDev debugger's variables view,self._regex_dict is not equal to empty but has one entry instead.
It seems variables view generatesself.regexautomatically, and unfortunatelly, the following method is defined and will output one entry toself._regex_dict
#property
def regex(self):
...
self._regex_dict[language_code] = compiled_regex
return self._regex_dict[language_code]
Is there any configuration to prevent such auto-generation behavior of Eclipse/PyDev debugger's variables view?
P.S. If I turn off the Eclipse/PyDev debugger's variables view, this issue won't happen. But I really need it when debugging
Thanks,

Well, when the debugger hits a breakpoint and you have the variables view visible, it'll do a dir() on all the objects in the context and for each value it'll do a str() to show them.
As you have a property (regex) which will add an entry to your dict 'behind the scenes' when accessed and as the variables view will definitely try to show that value to you, I'm not sure you have a way around it other than changing your program or changing the debugger itself (it'll get the vars at pydevd_vars.frameVarsToXML).

Related

How to access self.context property using a variable in flask

I want to access a property exist in the self.context using a variable. I have a variable name "prop" and it contains a value and it is already set in the self.context. I am using Flask Restplus framework.
prop = 'binding'
If I try to access this property like below then it gives me an error:
Object is not subscriptable
I want to know if there is any way to get the value? Like doing this:
print(self.context[prop])
I only get one solution don't know if its correct or not, I tried this :
self.context.__getattribute__(prop)
There are two ways to do this, the simplest is using getattr:
getattr(self.context, prop)
This function internally calls __getattribute__ so it's the same as your code, just a little neater.
However, you still have the problem that you probably only want to access some of the context, while not risking editing values set by different parts of your application. A much better way would be to store a dict in context:
self.context.attributes = {}
self.context.attributes["binding"] = False
print(self.context.attributes[prop])
This way, only certain context variables can be accessed and those that are meant to be dynamic don't mess with any used by your application code directly.

Define variables as variables in Robot Framework with Python file

This works
<<file1.robot>>
**Setting**
Resource file2.robot
**Variables**
${file1_var1} ${file2_var1}
**Keyword**
Check It
Click ${file1_var1}
where
<<file2.robot>>
**Settings**
Variables file2Locator.py
<<file2Locator.py>>
file2_var1 = "id=Clickable"
Click id=Clickable is called successfully, with keyword "Click It"
However, the following FAILS:
<<file1Fail.robot>>
**Setting**
Resource file2.robot
Variables file1Fail.py firstSet
**Keyword**
Check It
Click ${file1_var1}
Where
<<file1Fail.py>>
SetOneVar = {'file1_var1': "${file2_var1}"}
def get_variables(arg):
if arg == 'firstSet': return SetOneVar
else return
due to the error where UiSelector[DESCRIPTION=${file2_var1}] clearly does not work.
In other words, upon getting variables from a python file 1, Click ${file2_var1} is literally called, instead of Click id=Clickable. "${file2_var1}" is not being replaced by "id=Clickable" anymore.
Any solution to this? How to call define a variable as another variable imported at another resource file
In the python file the value of the variable you've defined (file1_var1) is set to the string ${file2_var1} (which looks like a RF variable definition due to the $ and {}, but it's just a string). When the framework imports it as a variable file, it does not automatically substitute it - "you set the value to be a string, you're getting a string".
Later on, after it's imported you can substitute/replace it with the value behind ${file2_var1} - as long as that file2_var1 is defined in the current scope. That's done by calling Replace Variables which returns the substituted version (e.g. doesn't modify the original variable):
${file2_var1}= Replace Variables ${file2_var1}

Is there a way to make Eclipse (with pydev) insert an empty docstring at the start of every class or def statement?

I'm trying to get in the habit of writing docstrings, and one way I think I could force myself to do that would be by automatically having Eclipse insert an empty docstring when I type a class or a def statement. It's only 8 key presses but it seems handy to me.
Would creating my own Eclipse plugin be the best method?
Just for reference, I'd like it to look something like this (yes I know the function is silly, it's just a demo):
def timeInit():
'''
'''
return ti.time()
PyDev doesn't do that automatically, but you can do Ctrl+1 in the def line and select the option for it to generate the docstring for you (it'll include all params in the docstring).
Another option is creating a template (in preferences > pydev > editor > templates) which automatically comes with the docstring and then use it to create the method (or you can edit the existing def template.

Django: "referenced before assignment" but only for some variables

I'm writing a small app in Django and I'm keeping the state saved in a few variables I declare out of the methods in views.py. Here is the important part of this file:
from app.playerlist import fullList
auc_unsold = fullList[:]
auc_teams = []
auc_in_progress = []
auc_current_turn = -1
print(auc_in_progress)
def auc_action(request):
data = json.loads(request.GET["data"])
# ...
elif data[0] == "start":
random.shuffle(auc_teams)
print(auc_unsold)
print(auc_in_progress)
auc_in_progress = [None, 0, None]
print(auc_in_progress)
The auc_unsold and auc_teams variables work fine; the auc_in_progress variable is not seen by this method, though, giving the error in the title. If I take out the print statement and let this code assign a value to it, the exception will be thrown somewhere else in the code as soon as I use that variable again.
I have tried making another variable and this new one seems to suffer from this problem as well.
What is happening?
Edit: I found a solution: if I write global auc_in_progress just before the print statements, then everything works fine. If I try writing that as I declare the variable above it doesn't work, though, for some reason.
I am unsatisfied with this, because I don't know why this happens and because I dislike using global like that, but eh. Someone has an explanation?
You should absolutely not be doing this, either your original code or your proposed solution with global.
Anything at module level will be shared across requests, not only for the current user but for all users for that process. So everyone will see the same auction, etc.
The reason for your error is because you assign to that variable within your function, which automatically makes it a local variable: see this question for more details. But the solution recommended there, which is the same as your workaround - ie use global - is not appropriate here; you should store the data somewhere specifically associated with the user, eg the session.

Debug history of variable changing in python

I need magic tool, that helps me to understand where one my problem variable is changed in the code.
I know about perfect tool:
pdb.set_trace()
and I need something similar format, but about only one variable changing history.
For example, my current problem is strange value of context['request'] variable inside Django's tag template definition method. The value is string '<<request>>' and I don't understand where it modified from Django's Request object. I can't debug it, because problem is appearing not so often, but permanently. I only see it in error emails and I can't call it specially. The perfect solution will be to create a log with variable's assignment and any modifications.
I'm not really familiar with django, so your mileage may vary. In general, you can override the __setitem__ method on objects to capture item assignment. However, this doesn't work on dictionaries, only on user-created classes, so first of all it depends on what this context object is.
As I get from a short look at the Django docs, it's indeed not a regular dict, so you can try something like this:
def log_setitem(obj):
class Logged(obj.__class__):
def __setitem__(self, item, val):
print "setting", item, "to", val, "on", self
super(Logged, self).__setitem__(item, val)
obj.__class__ = Logged
d = {}
try:
log_setitem(d) # throws an error
except:
print "doesn't work"
class Dict2(dict):
pass
d2 = Dict2()
log_setitem(d2) # this works
d2["hello"] = "world" # prints the log message before assigning
Even if this works, it of course only works if the assignment actually happens through the "standard" way, i.e. somewhere in the code there's a call like context['request'] = "something".
Might be worth a try, but I can't promise you anything.

Categories

Resources