I'm hacking a little template engine. I've a class (pompously named the template compiler) that produce a string of dynamically generated code.
for instance :
def dynamic_function(arg):
#statement
return rendered_template
At rendering time, I call the built-in function exec against this code, with a custom globals dictionary (in order to control as mush as possible the code inserted into the template by a potential malicious user).
But, I need to cache the compiled template to avoid compiling it each execution. I wonder if it's better to store the string as plain text and load it each time or use compile to produce code_object and store that object (using the shelve module for example).
Maybe it worth mentioning that ultimately I would like to make my template engine thread safe.
Thank for reading ! Thomas
edit : as S.Lott is underlining better doesn't have a sense itself. I mean by better faster, consume less memory simpler and easier debugging. Of course, more and tastier free coffee would have been even better.