Be careful with exec and eval in Python
lucumr.pocoo.org
lucumr.pocoo.org
I've also used it myself on occasion in quick-and-dirty scripts, but never in production code. It's just too easy to make a small mistake and blow your security wide open.
There are countless of examples where the template engine is the bottleneck of an application. If you are doubting that, I welcome you to try it yourself. Benchmarking Django's templates and Genshi is an eye opening experience. :)
From a quick google search, they seem pretty comparable. And in fact Django oftentimes wins.
http://genshi.edgewall.org/wiki/GenshiPerformance
http://stackoverflow.com/questions/1324238/what-is-the-faste...
I have seen many people switching from Django templates to Jinja2 especially because of the increased performance.
Tornado uses eval for parsing templates. The nice effect of this is that you write Python snippets in your templates rather than learning an entirely new DSL for it (like in Django). It's pretty nice as long you're disciplined enough to not put business logic in the templates.
Also, I just realized you're my former coworker. Hi Igor, it's Yusuf :)
How do you define complex templates? And as Mitsuhiko has pointed out, it is not just a problem with GAE.
Where my data is coming from local cache, the template does stick out in the request/response chain.
Given the ease of going horribly wrong with this strategy, I strongly recommend only using it in cases where it is obviously a huge win. Such cases are not non-existent, but they are rare.
http://documentcloud.github.com/underscore/docs/underscore.h...
One (bad) example would go along the lines of:
>>> def f(x): ... if x in dir(scipy): ... print eval("scipy." + x + "(2)") ... >>> f("sin") 0.909297426826
Then you can let the user plot whatever function they want.
scipy.__dict__[x](2) getattr(scipy, x)(2)Long answer: I have used it as part of a function that generates functions when I was writing an AST-generating portion of a parser. You need to make a bunch of similar functions, so you make a list of tuples containing all the bits that differ between the functions, then write a generator function which converts these strings into real Python functions which you can then eval in global context. Surprisingly enough this actually made the code more readable and easier to maintain. :)
But that is literally the only time I have ever thought it was a good idea -- and that's in code where I completely control the data going to exec(), in code that will never be in an Internet-facing arrangement.
http://svn.python.org/view/python/tags/r271/Lib/code.py?revi...
It lets me do hot-reloading of updated files, easily examine the namespace to find functions marked with various decorators, and not litter the plugin directory with unnecessary .pyc files.
The builtin imp module has problems with reloading already-loaded modules, and I might have been able to do some weird hacks with __import__ to make it work like I wanted, but when the core functionality boils down to 3 lines of code, why bother?
eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);')