[Python-ideas] Dynamic code NOPing

Rene Nejsum rene at stranden.com
Tue Dec 25 12:11:09 CET 2012


Interessting alternatives, but they do not quite come on as flexible/usefull enough…

Often debug statements have a lot of text and variables, like:

	log.debug( "The value of X, Y, Z is now: %d %s %d" % ( x, lookup(y), factorial(2**15))

It would be nice if args in log.debug() was only evaluated if debug was on. But I don't think this is possible with the current Python evaluation rules.

But if debug() was indeed NOP'able, maybe it could be done ?

/Rene

On Dec 25, 2012, at 10:35 AM, Jakob Bowyer <jkbbwr at gmail.com> wrote:

> Why not pass the function/method, args, kwargs to log.debug and let log.debug decide if it should execute or not,
> e.g.
> 
> log.debug(factorial, 2**15)
> 
> 
> On Tue, Dec 25, 2012 at 7:40 AM, Jasper St. Pierre <jstpierre at mecheye.net> wrote:
> if __debug__:
>     log(factorial(2**15))
> 
> Running python with -O will squash this statement. To have something inline, you could also abuse assert statements to do the job.
> 
> def debug_log(x):
>     log(x)
>     return True
> 
> assert debug_log(factorial(2**15))
> 
> In optimized builds, the statement will be removed entirely.
> 
> 
> 
> On Tue, Dec 25, 2012 at 1:28 AM, anatoly techtonik <techtonik at gmail.com> wrote:
> For the logging module it will be extremely useful if Python included a way to disactivate processing certain blocks to avoid making sacrifices between extensive logging harness and performance. For example, instead of writing:
> 
> if log.DEBUG==True:
>   log(factorial(2**15))
> 
> It should be possible to just write:
>   log(factorial(2**15))
> 
> if if log() is an instance of some Nopable class, the statement in log's braces is not executed.
> -- 
> anatoly t.
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
> 
> 
> 
> 
> -- 
>   Jasper
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121225/b9876bf5/attachment.html>


More information about the Python-ideas mailing list