Source code for spike.util.debug_tools
#!/usr/bin/env python
# encoding: utf-8
'''
Created by Lionel Chiron 07/10/2013
Copyright (c) 2013 __NMRTEC__. All rights reserved.
decorate the class with decclassdebugging and in each method to be debugged make :
if debug(self) : instructions..
'''
from __future__ import print_function
import sys, os
import inspect
import unittest
[docs]def pr(self, var, message = ''):
'''
print
'''
frame = inspect.currentframe()
loc = frame.f_back.f_locals
def show(value):
if message == '':
print(var + ' ', value)
else:
print(message + ' ' + var + ' ', value)
try:
show(eval(var))
except Exception:
show(loc[var])
[docs]def dec_class_pr(cl):
'''
Decorates the class for printing the local variables
'''
setattr(cl, 'pr', pr)
return cl
[docs]def name_up():
frame = sys._getframe(2)
return frame.f_code.co_name
[docs]def debug(cl):
'''
returns the name of the class followed by "_debug"
'''
return getattr(cl, name_up() + '_debug')
[docs]def decclassdebugging(cl):
'''
decorator for debugging classes.
It allows to trigger specifically which method in a class will be observed.
if debug(self) : " where debug is needed
After instantiation of the class, add class.method_name_debug = True to activate the debugging.
'''
for m in dir(cl):
if len(m.split('__')) == 1:
setattr(cl, m + '_debug', False)
return cl
if __name__ == '__main__':
unittest.main()