[issue19824] string.Template: Add PHP-style variable expansion example
New submission from anatoly techtonik: http://docs.python.org/2/library/string.html#template-strings This class could be more useful with the following example:
from string import Template t = Template('$who likes $what') who = 'tim' what = 'kung pao' t.substitute(locals()) 'tim likes kung pao'
This will help PHP folks to transition their .php files. ---------- assignee: docs@python components: Documentation messages: 204677 nosy: docs@python, techtonik priority: normal severity: normal status: open title: string.Template: Add PHP-style variable expansion example _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Changes by Barry A. Warsaw <barry@python.org>: ---------- nosy: +barry _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Barry A. Warsaw added the comment: On Nov 28, 2013, at 05:07 PM, anatoly techtonik wrote:
This class could be more useful with the following example:
from string import Template t = Template('$who likes $what') who = 'tim' what = 'kung pao' t.substitute(locals()) 'tim likes kung pao'
This will help PHP folks to transition their .php files.
I'm not sure what you want to add to the class. Your example works out of the box. See this for an approach my third party library takes: http://pythonhosted.org/flufl.i18n/docs/using.html#substitutions-and-placeho... ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
anatoly techtonik added the comment: There is nothing to add to the class itself. It is about expanding docs section with helpful examples. `string.Template` is undervalued, because it is hard to see how it can be more useful than standard string formatting functions. But for people coming from PHP world, this can be a good start. The docs just need an entrypoint that shows how to use locally defined variables in template string. PHP does this for strings automatically. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Alex Gaynor added the comment: Using locals() in this fashion is a serious anti-pattern, I'm -∞ on the docs suggesting it. ---------- nosy: +alex _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
anatoly techtonik added the comment: @Alex, have you seen http://pythonhosted.org/flufl.i18n/docs/using.html#substitutions-and-placeho... I really like the brevity, and it is the function that does the magic, so it is fully transparent and you don't need to instantiate string.Template every time. I think its awesome. Do you have some explanations why passing locals() to string.Template is anti-pattern? I understand that passing "all that you have" is not good, but from my past experience with PHP I can't remember any problems that there are more names than I used. It is templating after all - what do you want to protect from? ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Barry A. Warsaw added the comment: A few notes about flufl.i18n's style. We chose this (extracted from the GNU Mailman project) because $strings are *way* less error prone for translators than %s strings, especially when you consider that some languages change the order of placeholders. The automatic extraction of substitutions from locals and globals (under the hood, via the sys._getframe() hack) was critical to making the source code readable, by avoiding not just duplication, but triplication of names. There is a potential security hole though - a malicious translator with access to the source could analyze the local and global context in which the translation+substitution is being made, and craft a gettext catalog that adds some new substitutions that expose sensitive information. Given that most translations get little scrutiny, this could be used as an attack vector for users of some languages (though not English, since it's typically the source language and thus not translated). We've decided to accept the risks in exchange for the huge convenience. We've never seen such an attack and if we did, we'd address it in the code by manipulating the globals and locals to avoid the possibility of a leak. (We'd also learn to never trust the translators that added the hack.) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Serhiy Storchaka added the comment: Formatting with locals() and globals() should be used with careful. This is not the most idiomatic way of formatting. I think that the post in specialised blog would be better place for describing it than Python stdlib documentation. Peoples coming from PHP world can use f-strings. ---------- nosy: +serhiy.storchaka status: open -> pending _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Barry A. Warsaw added the comment: We should really restructure string.Template documentation to emphasize i18n. That's always been its prime use case, and f-strings don't change that (because f-strings are not really appropriate for translations). Before f-strings, string.Template had other common uses. But f-strings do fulfill most other cases where people were using string.Template, so let's make sure that distinction is clear to people. ---------- status: pending -> open _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
R. David Murray added the comment: Let's retitle this, then. ---------- nosy: +r.david.murray title: string.Template: Add PHP-style variable expansion example -> string.Template: Rewrite docs to emphasize i18n use case _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Serhiy Storchaka added the comment: In that case issue20314 looks related. ---------- stage: -> needs patch type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Changes by Barry A. Warsaw <barry@python.org>: ---------- assignee: docs@python -> barry _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Barry A. Warsaw added the comment: I'll take this one, and see if I can address 20314 also. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Changes by Barry A. Warsaw <barry@python.org>: ---------- pull_requests: +758 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Barry A. Warsaw added the comment: New changeset 9f74deba784fc8781d13ed564f69c02ed7c331bb by Barry Warsaw in branch 'master': Improve the documentation for template strings (#856) https://github.com/python/cpython/commit/9f74deba784fc8781d13ed564f69c02ed7c... ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
Changes by Barry A. Warsaw <barry@python.org>: ---------- resolution: -> fixed stage: needs patch -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue19824> _______________________________________
participants (5)
-
Alex Gaynor
-
anatoly techtonik
-
Barry A. Warsaw
-
R. David Murray
-
Serhiy Storchaka