Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add support for Template type (t-strings) to pprint #134551

Open
Labels
stdlibPython modules in the Lib dirtype-featureA feature request or enhancement
@loic-simon

Description

@loic-simon

Feature or enhancement

Proposal:

Updatepprint functions to understand and formatTemplate andInterpolation types:

>>>from pprintimport pp>>> name="World">>> pp(t"Hello{World}")Template(strings=('Hello ', ''),         interpolations=(Interpolation('World', 'name', None, ''),))""")

I believe it will be quite common to play with t-strings in the REPL and other dynamic environments, where pretty formatting could help a lot. I don't know how high is the bar for adding types to thepprint registry, though.

If this is deemed worthwhile, I have aworking implementation, pretty straightforward.


More complex examples (inspired from the PEP), to give a sense of what it could look like:

>>> attributes= {"id":"main"}>>> attribute_value="shrubbery">>> content="hello">>> template= t"<span{attributes} data-value={attribute_value}>{content}</span>">>> pp(template)Template(strings=('<span ', ' data-value=', '>', '</span>'),         interpolations=(Interpolation({'id': 'main'}, 'attributes', None, ''),                         Interpolation('shrubbery',                                       'attribute_value',                                       None,                                       ''),                         Interpolation('hello', 'content', None, '')))>>>>>> nested= t"<div>{template}</div>">>> pp(nested)Template(strings=('<div>', '</div>'),         interpolations=(Interpolation(Template(strings=('<span ',                                                         ' data-value=',                                                         '>',                                                         '</span>'),                                                interpolations=(Interpolation({'id': 'main'},                                                                              'attributes',                                                                              None,                                                                              ''),                                                                Interpolation('shrubbery',                                                                              'attribute_value',                                                                              None,                                                                              ''),                                                                Interpolation('hello',                                                                              'content',                                                                              None,                                                                              ''))),                                       'template',                                       None,                                       ''),))

Unfortunately, "interpolation" being a quite long word repeated 2 times in the repr of a t-string, with default formatting rules we soon have text crumpled to the right of the screen... but I believe it is still more readable than

Template(strings=('<div>','</div>'),interpolations=(Interpolation(Template(strings=('<span ', 'data-value=', '>', '</span>'), interpolations=(Interpolation({'id': 'main'}, 'attributes', None, ''),Interpolation('shrubbery','attribute_value',None,''),Interpolation('hello','content',None,''))),'template',None,''),))

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp