Message363376
Do you have some concrete use case for this, or is this a speculative feature request?
This will do what you want, although it uses some undocumented internals of the _string module. I've only tested it a little, and I've done especially little testing for the error conditions:
from _string import formatter_parser, formatter_field_name_split
def partial_format(spec, *args, **kwargs):
idx = 0
auto_number = False
manual_number = False
result = []
for literal_text, field_name, format_spec, conversion in formatter_parser(spec):
result.append(literal_text)
found = False
if field_name is None:
# We're at the end of the input.
break
if not field_name:
# Auto-numbering fields.
if manual_number:
raise ValueError(
"cannot switch from manual field specification to automatic field numbering"
)
auto_number = True
try:
value = args[idx]
idx += 1
found = True
except IndexError:
pass
elif isinstance(field_name, int):
# Numbered fields.
if auto_number:
raise ValueError(
"cannot switch from automatic field number to manual field specification"
)
manual_number = True
try:
value = args[field_name]
found = True
except IndexError:
pass
else:
# Named fields.
try:
value = kwargs[field_name]
found = True
except KeyError:
pass
spec = f":{format_spec}" if format_spec else ""
conv = f"!{conversion}" if conversion else ""
if found:
s = f"{{{conv}{spec}}}"
result.append(s.format(value))
else:
result.append(f"{{{field_name}{conv}{spec}}}")
return "".join(result) |
|
Date |
User |
Action |
Args |
2020-03-04 18:53:38 | eric.smith | set | recipients:
+ eric.smith, Marco Sulla |
2020-03-04 18:53:38 | eric.smith | set | messageid: <1583348018.76.0.0445522918418.issue39842@roundup.psfhosted.org> |
2020-03-04 18:53:38 | eric.smith | link | issue39842 messages |
2020-03-04 18:53:38 | eric.smith | create | |
|