Difference between revisions of "Python templates"
From Noah.org
Jump to navigationJump to search (New page: Category:Engineering Category:Python Python has an excellent built-in template system -- the % operator. Granted this is not what many people would consider a "real" template syst...) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Engineering]] | [[Category:Engineering]] | ||
[[Category:Python]] | [[Category:Python]] | ||
+ | |||
+ | == Python string templates using the % template operator == | ||
Python has an excellent built-in template system -- the % operator. | Python has an excellent built-in template system -- the % operator. | ||
Line 6: | Line 8: | ||
it's much more powerful than most people give it credit for. | it's much more powerful than most people give it credit for. | ||
− | + | The key is to combine % with the locals() function. Template code practically writes itself. | |
Remember string templates can have more than just %d and %s style replacements. | Remember string templates can have more than just %d and %s style replacements. | ||
It can also take a dictionary, like %(DICT_KEY1)s or %(DICT_KEY2)d. | It can also take a dictionary, like %(DICT_KEY1)s or %(DICT_KEY2)d. | ||
Line 15: | Line 17: | ||
TEMPLATE="""From: %(FROM)s | TEMPLATE="""From: %(FROM)s | ||
To: %(TO)s | To: %(TO)s | ||
+ | Subject: %(SUBJECT)s | ||
+ | |||
Hello %(USERNAME)s, | Hello %(USERNAME)s, | ||
− | |||
Your account is over quota. | Your account is over quota. | ||
Your quota limit is %(LIMIT)d bytes. | Your quota limit is %(LIMIT)d bytes. | ||
Your amount used is %(USED)d bytes. | Your amount used is %(USED)d bytes. | ||
+ | """ | ||
− | + | def format_alert_email (USERNAME, FROM, TO, SUBJECT, LIMIT, USED): | |
+ | return TEMPLATE % locals() | ||
+ | </pre> | ||
+ | |||
+ | Running the template will give the following results: | ||
+ | <pre> | ||
+ | >>> print format_alert_email('Joe Bloe','root@example.com','jbloe@example.com','over quota',1000,1234) | ||
+ | From: root@example.com | ||
+ | To: jbloe@example.com | ||
+ | Subject: over quota | ||
+ | |||
+ | Hello Joe Bloe, | ||
+ | |||
+ | Your account is over quota. | ||
+ | Your quota limit is 1000 bytes. | ||
+ | Your amount used is 1234 bytes. | ||
− | + | >>> | |
− | |||
− | |||
− | |||
</pre> | </pre> |
Latest revision as of 16:28, 6 June 2007
Python string templates using the % template operator
Python has an excellent built-in template system -- the % operator. Granted this is not what many people would consider a "real" template system, but it's much more powerful than most people give it credit for.
The key is to combine % with the locals() function. Template code practically writes itself. Remember string templates can have more than just %d and %s style replacements. It can also take a dictionary, like %(DICT_KEY1)s or %(DICT_KEY2)d. Next remember that the locals() function returns a dictionary of local variables. This makes it trivial to merge local variables into a string template.
TEMPLATE="""From: %(FROM)s To: %(TO)s Subject: %(SUBJECT)s Hello %(USERNAME)s, Your account is over quota. Your quota limit is %(LIMIT)d bytes. Your amount used is %(USED)d bytes. """ def format_alert_email (USERNAME, FROM, TO, SUBJECT, LIMIT, USED): return TEMPLATE % locals()
Running the template will give the following results:
>>> print format_alert_email('Joe Bloe','root@example.com','jbloe@example.com','over quota',1000,1234) From: root@example.com To: jbloe@example.com Subject: over quota Hello Joe Bloe, Your account is over quota. Your quota limit is 1000 bytes. Your amount used is 1234 bytes. >>>