Message363317
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' |
|
Date |
User |
Action |
Args |
2020-03-03 23:57:59 | Marco Sulla | set | recipients:
+ Marco Sulla |
2020-03-03 23:57:59 | Marco Sulla | set | messageid: <1583279879.75.0.642484587376.issue39842@roundup.psfhosted.org> |
2020-03-03 23:57:59 | Marco Sulla | link | issue39842 messages |
2020-03-03 23:57:59 | Marco Sulla | create | |
|