>>> from printval import printval >>> a = 4 >>> b = 5 >>> printval| a a is 4 >>> printval| b b is 5 >>> print('3*a*b is '+str(3*a*b)) 3*a*b is 60 >>> printval| 3*a*b 3*a*b is 60
See the last example -- saves typing!
Here's all of printval:
import inspect, itertools class PythonPrintVal(object): def _output(self, name, val): # expands a struct to one level. print('printval| '+name+' is '+repr(val)) if name=='locals()': for key in val: if not key.startswith('_'): self._output(key, val[key]) elif ' object at 0x' in repr(val): for propname in dir(val): if not propname.startswith('_'): sval = repr(val.__getattribute__(propname)) print(' \t\t.'+propname+' = '+sval) def __or__(self, val): # look in the source code for the text fback = inspect.currentframe().f_back try: with open(fback.f_code.co_filename, 'r') as srcfile: line = next(itertools.islice(srcfile, fback.f_lineno-1, fback.f_lineno)) self._output(line.replace('printval|','',1).strip(), val) except (StopIteration, IOError): return self._output('?',val) printval = PythonPrintVal() #really, that's all the code there is!
It will also enumerate through the fields of a class. If you use printval| locals(), you get a more-nicely formatted representation of all locals in scope.
(In an earlier version, I used the syntax printval.a, so I could create chains like printval.a.b. This method doesn't require looking at source code -- the printval object knows the string 'a' from the parameter passed to __getattribute__, and retrieves its value from the scope of the caller. As a benefit this could work in compiled/frozen apps. The downside is that this doesn't allow expressions like printval|a+4)
It turns out that other people have had similar ideas; there are modules for ScopeFormatter, Say, and Show.
For ultimate scratchpad-ability: