This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Marco Sulla
Recipients Marco Sulla
Date 2020-03-03.23:57:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1583279879.75.0.642484587376.issue39842@roundup.psfhosted.org>
In-reply-to
Content
In `string` module, there's a very little known class `Template`. It implements a very simple template, but it has an interesting method: `safe_substitute()`.

`safe_substitute()` permits you to not fill the entire Template at one time. On the contrary, it substitute the placeholders that are passed, and leave the others untouched.

I think it could be useful to have a similar method for the format minilanguage. I propose a partial_format() method.

=== WHY I think this is useful? ===

This way, you can create subtemplates from a main template. You could want to use the template for creating a bunch of strings, all of them with the same value for some placeholders, but different values for other ones. This way you have *not* to reuse the same main template and substitute every time the placeholders that does not change.

`partial_format()` should act as `safe_substitute()`: if some placeholder misses a value, no error will be raised. On the contrary, the placeholder is leaved untouched.

Some example:

>>> "{} {}".partial_format(1)
'1 {}'


>>> "{x} {a}".partial_format(a="elephants")
'{x} elephants'

>>> "{:-f} and {:-f} nights".partial_format(1000)
'1000 and {:-f} nights'
History
Date User Action Args
2020-03-03 23:57:59Marco Sullasetrecipients: + Marco Sulla
2020-03-03 23:57:59Marco Sullasetmessageid: <1583279879.75.0.642484587376.issue39842@roundup.psfhosted.org>
2020-03-03 23:57:59Marco Sullalinkissue39842 messages
2020-03-03 23:57:59Marco Sullacreate