Here I came up with the solution to the other question asked by me on how to remove all costly calling to debug output function scattered over the function code (slowdown was 25 times with using empty function lambda *p: None
).
The solution is to edit function code dynamically and prepend all function calls with comment sign #
.
from __future__ import print_function
DEBUG = False
def dprint(*args,**kwargs):
'''Debug print'''
print(*args,**kwargs)
def debug(on=False,string='dprint'):
'''Decorator to comment all the lines of the function code starting with string'''
def helper(f):
if not on:
import inspect
source = inspect.getsource(f)
source = source.replace(string, '#'+string) #Beware! Swithces off the whole line after dprint statement
with open('temp_f.py','w') as file:
file.write(source)
from temp_f import f as f_new
return f_new
else:
return f #return f intact
return helper
def f():
dprint('f() started')
print('Important output')
dprint('f() ended')
f = debug(DEBUG,'dprint')(f) #If decorator @debug(True) is used above f(), inspect.getsource somehow includes @debug(True) inside the code.
f()
The problems I see now are these:
#
commets all line to the end; but there may be other statements separated by;
. This may be addressed by deleting allpprint
calls inf
, not commenting, still it may be not that trivial, as there may be nested parantheses.temp_f.py
is created, and then newf
code is loaded from it. There should be a better way to do this without writing to hard drive. I found this recipe, but haven't managed to make it work.- if decorator is applied with special syntax used
@debug
, theninspect.getsource
includes the line with decorator to the function code. This line can be manually removed from string, but it may lead to bugs if there are more than one decorator applied tof
. I solved it with resorting to old-style decorator applicationf=decorator(f)
.
What other problems do you see here?
How can all these problems be solved?
What are upsides and downsides of this approach?
What can be improved here?
Is there any better way to do what I try to achieve with this code?
I think it's a very interesting and contentious technique to preprocess function code before compilation to byte-code. Strange though that nobody got interested in it. I think the code I gave may have a lot of shaky points.