Dead Easy Templates In 4 Lines Of Pure Python

Last update on Nov. 11, 2013.

In the introduction for TIE's documentation (which is STILL not updated for a 0.2 release which has been ready for maybe two month... shame on me), I briefly mention python's built in string formatting functions and utilities, reasoning that those might very well be more than enough for really simple templating needs.

(I'm realizing more and more than the lib's targeted use cases - string formatting just hairy enough to become a small pain in the butt with only those builtins, but still too simple to grab for the heavy stuff - might be too niche to make it actually useful to anyone. But hey, it was fun to build, so i don't really care that much, and that's not the point of this post anyway).

A few weeks ago, I stumbled on some snippets that illustrated this very well (Sadly, i can't remember where the hell I found those. I'll update the post and link to them if i happen to find them again), using some of those neat tricks that make me love python, so I thought I'd discuss them and show how you could build a truly minimal template system using nothing more than the built-in str.format() method.

The whole trick simply involves the fact that functions in python - like everything else - are first class objects. What this means is, among other thing, that you can pass them around, assign them to variables, and generally treat them like any other value:

>>> def f():
...     return "foo"
... 
>>> f
<function f at 0x7f5d993e1578>
>>> var = f
>>> var
<function f at 0x7f5d993e1578>
>>> var()
'foo'

As you can see, the f and var name both reference the same object in memory.

Now, what about methods ? They're basically just functions tied to an object, right ?

>>> class Foo(object):
...     bar = "bar"
...     def my_method(self):
...         return self.bar
... 
>>> foo_object = Foo()
>>> foo_object.my_method
<bound method Foo.my_method of <__main__.Foo object at 0x7f5d993dda90>>

Yep. That "bound method..." nonsense basically means what we just said: "tied to an object". Which means it will remember that objects state, even if you start fooling around with it:

>>> var = foo_object.my_method
>>> var()
'bar'
>>> foo_object.bar = 'foo'
>>> var()
'foo'

Here, the my_method object keeps access to the Foo class' dictionnary of attribute, which is why it is still able to return its bar attribute, even though we're not making any reference to the actual Foo object the method is bound too. Think closures in javascript - or python, for that matter.

I had some trouble wrapping my head around that when I first learned about this. If you don't get it, don't worry, the important thing is to remember that this:

>>> my_template = "Hello, {stuff}!".format
>>> my_template(stuff="world")
'Hello, world!'

will run without any trouble. We simply store the string's format method in a variable, which will then remember its associated format string when we call it (For more information about this method, start here.

Now, lets just build a quick helper to load our template from a file, and we'll have the heart of our pseudo engine, in the advertized 4 lines of code (I'll use the same dummy template as before, and save is as tmpl.txt):

>>> def template(path):
...     with open(path) as f:
...         tmpl_str = f.read()
...     # add a call to strip to get rid of unwanted newlines characters
...     return tmpl_str.strip().format
...
>>> my_template = template("tmpl.txt")
>>> my_template(stuff="Goodbye")
'Hello, Goodbye!'

How you manage those after that is up to you. Use them right away, store them in a dictionnary or some other container... This system certainly won't handle anything, but it should be enough for quick and dirty substitution work. 

EDIT: I half expected it, so of course it happened. Stoopid TinyMCE pooped up the code snippets. I corrected the first one, way too lazy to fix the others tonight, so i'll do that some other day.

EDIT2: Allright, fixed. Stoopid wysiwygz.

Comments

  1. พลุ ดอกไม้ไฟ

    พลุ ดอกไม้ไฟ on 02/09/2015 1:31 p.m. #

    Greate post. Keep writing such kind of info on your page. Im really impressed
    by your blog.
    Hey there, You've done an excellent job. I will definitely digg it
    and individually suggest to my friends. I'm confident they'll be benefited
    from this web site.

Comments are closed.

Pingbacks

Pingbacks are closed.

Trackbacks

  1. garcinia cambogia weight loss pill gnc on 02/10/2015 11:10 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  2. Leatherman Multi Tool Rebar Steel on 02/11/2015 9:55 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  3. Http://Www.Nyttigbras.Dk on 02/11/2015 11:08 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  4. aljazeera-club.com on 02/11/2015 1:43 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  5. pure garcinia cambogia side effects on 02/12/2015 6:06 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  6. Frisco Dentist on 02/13/2015 1:33 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  7. stop ringing in ears on 02/13/2015 7:16 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  8. ocoee orlando florida on 02/13/2015 7:24 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  9. please click the following post on 02/13/2015 7:26 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  10. click through the following website page on 02/13/2015 5:26 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  11. santos juicer on 02/14/2015 12:09 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  12. website on 02/14/2015 11:48 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  13. www.codecademy.com on 02/15/2015 1:07 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  14. %anchot_text% on 02/17/2015 2:13 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  15. %anchot_text% on 02/17/2015 2:30 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  16. this page on 02/17/2015 11:46 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  17. read this post here on 02/17/2015 11:48 p.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  18. try online dating sites on 02/18/2015 12:29 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  19. go to this site on 02/18/2015 12:53 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  20. earn lots of money online on 02/18/2015 1:17 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  21. קורס מנעולן מקוון באזור סביון on 02/18/2015 2:35 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python

  22. get paid to take online surveys on 02/18/2015 4:46 a.m. #

    Fried Chicken And Gazoline - Dead easy templates in 4 lines of pure python