tempfile.NamedTemporaryFile use case?

Alice Bevan–McGregor alice at gothcandy.com
Fri Dec 31 17:58:58 EST 2010


I'm a bad person, but one use case I have is for shuffling templates 
around such that:

* An inherited ('parent') template can be stored in a database.
* The 'views' of my application are told to either use the real master 
template or the db parent template.
* The rendering engine loads the parent from disk.

Thus I create a NamedTemporaryFile (with some custom prefix and suffix 
stuff), write the parent template from DB to it, flush, then let the 
rendering engine do its thing.  Works like a hot damn, and lets the 
users of my CMS manage custom layouts from within the CMS.

Templates are cached using the CMS template path as the dict key, and a 
2-tuple of modification time and the NamedTemporaryFile as the value.  
Cleanup of old versions on-disk is simple: just close the file!

	- Alice.





More information about the Python-list mailing list