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.
Personally, i’d store text. You can look at text with an editor or whatever, which will make debugging and mucking about easier. It’s also vastly easier to write unit tests for, if you want to test that the contents of the cache file are what you expect.
Later, if you find that your system isn’t fast enough, and if profiling reveals that parsing the cached templates is taking a lot of time, you can try switching to storing bytecode – but only then. As long as the storage mechanism is properly encapsulated, this change should be fairly painless.