Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string.Template: Rewrite docs to emphasize i18n use case #64023

Closed
techtonik mannequin opened this issue Nov 28, 2013 · 12 comments
Closed

string.Template: Rewrite docs to emphasize i18n use case #64023

techtonik mannequin opened this issue Nov 28, 2013 · 12 comments
Assignees
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@techtonik
Copy link
Mannequin

techtonik mannequin commented Nov 28, 2013

BPO 19824
Nosy @warsaw, @alex, @bitdancer, @serhiy-storchaka
PRs
  • Improve the documentation for template strings #856
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/warsaw'
    closed_at = <Date 2017-03-28.14:04:19.566>
    created_at = <Date 2013-11-28.17:07:04.537>
    labels = ['type-feature', 'docs']
    title = 'string.Template: Rewrite docs to emphasize i18n use case'
    updated_at = <Date 2017-03-28.14:04:19.566>
    user = 'https://bugs.python.org/techtonik'

    bugs.python.org fields:

    activity = <Date 2017-03-28.14:04:19.566>
    actor = 'barry'
    assignee = 'barry'
    closed = True
    closed_date = <Date 2017-03-28.14:04:19.566>
    closer = 'barry'
    components = ['Documentation']
    creation = <Date 2013-11-28.17:07:04.537>
    creator = 'techtonik'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 19824
    keywords = []
    message_count = 12.0
    messages = ['204677', '204682', '204686', '204687', '204695', '204700', '290547', '290548', '290552', '290563', '290618', '290717']
    nosy_count = 6.0
    nosy_names = ['barry', 'techtonik', 'alex', 'r.david.murray', 'docs@python', 'serhiy.storchaka']
    pr_nums = ['856']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue19824'
    versions = []

    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Nov 28, 2013

    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.

    @techtonik techtonik mannequin assigned docspython Nov 28, 2013
    @techtonik techtonik mannequin added the docs Documentation in the Doc dir label Nov 28, 2013
    @warsaw
    Copy link
    Member

    warsaw commented Nov 28, 2013

    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-placeholders

    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Nov 28, 2013

    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.

    @alex
    Copy link
    Member

    alex commented Nov 28, 2013

    Using locals() in this fashion is a serious anti-pattern, I'm -∞ on the docs suggesting it.

    @techtonik
    Copy link
    Mannequin Author

    techtonik mannequin commented Nov 28, 2013

    @alex, have you seen http://pythonhosted.org/flufl.i18n/docs/using.html#substitutions-and-placeholders? 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?

    @warsaw
    Copy link
    Member

    warsaw commented Nov 28, 2013

    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.)

    @serhiy-storchaka
    Copy link
    Member

    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.

    @warsaw
    Copy link
    Member

    warsaw commented Mar 26, 2017

    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.

    @bitdancer
    Copy link
    Member

    Let's retitle this, then.

    @bitdancer bitdancer changed the title string.Template: Add PHP-style variable expansion example string.Template: Rewrite docs to emphasize i18n use case Mar 27, 2017
    @serhiy-storchaka
    Copy link
    Member

    In that case bpo-20314 looks related.

    @serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Mar 27, 2017
    @warsaw warsaw assigned warsaw and unassigned docspython Mar 27, 2017
    @warsaw
    Copy link
    Member

    warsaw commented Mar 27, 2017

    I'll take this one, and see if I can address 20314 also.

    @warsaw
    Copy link
    Member

    warsaw commented Mar 28, 2017

    New changeset 9f74deb by Barry Warsaw in branch 'master':
    Improve the documentation for template strings (#856)
    9f74deb

    @warsaw warsaw closed this as completed Mar 28, 2017
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants