public
anonymous / watcher.py
Created

  • Download Gist
watcher.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 
import traceback
 
class Watcher(object):
def __init__(self, obj=None, attr=None, log_file='log.txt', enabled=False):
"""
Debugger that watches for changes in object attributes
obj - object to be watched
attr - string, name of attribute
log_file - string, where to write output
"""
 
self.log_file=log_file
with open(self.log_file, 'wb'): pass
if obj:
self.value = getattr(obj, attr)
self.obj = obj
self.attr = attr
self.enabled = enabled # Important, must be last line on __init__.
 
def __call__(self, *args, **kwargs):
kwargs['enabled'] = True
self.__init__(*args, **kwargs)
 
def check_condition(self):
tmp = getattr(self.obj, self.attr)
result = tmp != self.value
self.value = tmp
return result
 
def trace_command(self, frame, event, arg):
if not self.enabled:
return self.trace_command
if not self.check_condition():
return self.trace_command
with open(self.log_file, 'ab') as f:
print >>f, "Value of",self.obj,".",self.attr,"changed!"
print >>f,"###### Before this line:"
print >>f,''.join(traceback.format_stack(frame))
return self.trace_command
import sys
watcher = Watcher()
sys.settrace(watcher.trace_command)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.