top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

collecting variable assignments through settrace

0 votes
252 views

I'm writing a custom profiler that uses sys.settrace. I was wondering if there was any way of tracing the assignments of variables inside a function as its executed, without looking at locals() at every single line and comparing them to see if anything has changed.

Sort of like xdebug's collect_assignments parameter in PHP.

posted Jun 18, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

The stdlib has an obscure module bdb (basic debugger) that is used in both pdb (python debugger) and idlelib.Debugger. The latter can display global and local variable names. I do not know if it does anything other than rereading globals() and locals(). It only works with a file loaded in the editor, so it potentially could read source lines to looks for name binding statements (=, def, class, import) and determine the names just bound. On the other hand, re-reading is probably fast enough for human interaction.

My impression from another issue is that traceing causes the locals dict to be updated with each line, so you do not actually have to have to call locals() with each line. However, that would mean you have to make copies to compare.

answer Jun 18, 2013 by anonymous
Similar Questions
+1 vote

My code has this structure:

class Example(wx.Frame,listmix.ColumnSorterMixin):
 def __init__(self,parent):
 wx.Frame.__init__(self,parent)

 self.InitUI()

 def InitUI(self): 
 ..... 

when a button is clicked this function is called and i take the self.id_number which is a number

 def OnB(self, event):
 self.id_number = self.text_ctrl_number.GetValue()
 aa = latitude[int(self.id_number)]
 bb = longitude[int(self.id_number)]

I want to pass the variables aa and bb to a different script called application. This script by calling it with import, automatically pop up a window. I need by clicking the button that is linked with OnB definition to pop up the window from the other script as it does when i am running it alone and display lets say for example the variables aa and bb, how can I do it

0 votes

I've been using the settrace function to write a tracer for my program, which is working great except that it doesn't seem to work for built-in functions, like open('filename.txt'). This doesn't seem to be documented, so I'm not sure if I'm doing something wrong or that's the expected behavior.

If settrace's behavior in this regard is fixed, is there any way to trace calls to open()? I don't want to use Linux's strace, as it'll run for whole program (not just the part I want) and won't show my python line numbers/file names, etc. The other option I considered was monkey-patching the open function through a wrapper, like:

def wrapped_open(*arg,**kw):
 print 'open called'
 traceback.print_stack()
 f = __builtin__.open(*arg,**kw)
 return f
open = wrapped_open

but that seemed very brittle to me. Could someone suggest a better way of doing this?

+1 vote

To loop though an iterator one usually uses a higher-level construct such as a 'for' loop. However, if you want to step through it manually you can do so with next(iter).

I expected the same functionality with the new 'asynchronous iterator' in Python 3.5, but I cannot find it.

I can achieve the desired result by calling 'await aiter.__anext__()', but this is clunky.

Am I missing something?

+2 votes

I know basic of python and I have an xml file created from csv which has three attributes "category", "definition" and "definition description". I want to parse through xml file and identify actors, constraints, principal from the text.

However, I am not sure what is the best way to go. Any suggestion?

+2 votes

I need to search through a directory of text files for a string. Here is a short program I made in the past to search through a single text file for a line of text.

How can I modify the code to search through a directory of files that have different filenames, but the same extension?

fname = raw_input("Enter file name: ") #"*.txt"
fh = open(fname)
lst = list()
biglst=[]
for line in fh:
 line=line.rstrip()
 line=line.split()
 biglst+=line
final=[]
for out in biglst:
 if out not in final:
 final.append(out)
final.sort()
print (final)
...