-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfstring.py
More file actions
39 lines (32 loc) · 1.25 KB
/
fstring.py
File metadata and controls
39 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Implement f-string like behavior on top of t-strings.
Template strings are a generalization of f-strings. t-strings evaluate
to a new type (`Template`); it is possible to take a `Template` and build
a string that exactly matches the result had an `f`-prefix been used in the
first place.
See also `test_fstring.py`
"""
from string.templatelib import Interpolation, Template
from typing import Literal
def convert(value: object, conversion: Literal["a", "r", "s"] | None) -> object:
"""Convert the value to a string using the specified conversion."""
# Python has no convert() built-in function, so we have to implement it.
if conversion == "a":
return ascii(value)
if conversion == "r":
return repr(value)
if conversion == "s":
return str(value)
return value
def f(template: Template) -> str:
"""Implement f-string behavior using the PEP 750 t-string behavior."""
parts = []
for item in template:
match item:
case str() as s:
parts.append(s)
case Interpolation(value, _, conversion, format_spec):
value = convert(value, conversion)
value = format(value, format_spec)
parts.append(value)
return "".join(parts)