|
1 | | -from string.templatelib import Interpolation |
| 1 | +# Uncomment in Python 3.14+ |
2 | 2 |
|
3 | | -name = "Pythonista" |
4 | | -site = "realpython.com" |
5 | | -template = t"Hello, {name}! Welcome to {site}!" # noqa |
| 3 | +# from string.templatelib import Interpolation |
6 | 4 |
|
7 | | -# for item in template: |
8 | | -# print(item) |
9 | | - |
10 | | -# for s in template.strings: |
11 | | -# print(s) |
12 | | - |
13 | | -for value in template.values: |
14 | | - print(value) |
15 | | - |
16 | | -debit = 300.4344 |
17 | | -credit = 450.5676 |
18 | | -balance = credit - debit |
19 | | -template = t"Credit: {credit:.2f}, Debit: {debit:.2f}, Balance: {balance:.2f}" # noqa |
| 5 | +# name = "Pythonista" |
| 6 | +# site = "realpython.com" |
| 7 | +# template = t"Hello, {name}! Welcome to {site}!" |
20 | 8 |
|
21 | 9 |
|
22 | 10 | # def build_report(template): |
23 | 11 | # parts = ["Account Report:\n"] |
24 | 12 | # for item in template: |
25 | | -# if isinstance(item, str): |
26 | | -# parts.append(item.strip(", ").upper()) |
27 | | -# else: |
28 | | -# parts.append(f"> ${item.value:{item.format_spec}}") |
| 13 | +# match item: |
| 14 | +# case str() as s: |
| 15 | +# parts.append(s.strip(", ").upper()) |
| 16 | +# case Interpolation() as i: |
| 17 | +# parts.append(f"> ${i.value:{i.format_spec}}") |
29 | 18 | # parts.append("\n") |
30 | 19 | # return "".join(parts) |
31 | 20 |
|
32 | 21 |
|
33 | | -def build_report(template): |
34 | | - parts = ["Account Report:\n"] |
35 | | - for item in template: |
36 | | - match item: |
37 | | - case str() as s: |
38 | | - parts.append(s.strip(", ").upper()) |
39 | | - case Interpolation() as i: |
40 | | - parts.append(f"> ${i.value:{i.format_spec}}") |
41 | | - parts.append("\n") |
42 | | - return "".join(parts) |
43 | | - |
44 | | - |
45 | | -report = build_report(template) |
46 | | -print(report) |
| 22 | +# report = build_report(template) |
| 23 | +# print(report) |
0 commit comments