Skip to content

Commit f6dfa42

Browse files
committed
More fixes
1 parent eb4545b commit f6dfa42

File tree

8 files changed

+60
-74
lines changed

8 files changed

+60
-74
lines changed

python-t-strings/asynchronous.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ async def get_name():
88

99

1010
async def greeting_template():
11-
return t"Hello, {await get_name()}!" # noqa
11+
"Uncomment in Python 3.14+"
12+
# return t"Hello, {await get_name()}!"
1213

1314

1415
async def main():

python-t-strings/i18n.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
name = input("Enter your name please: ")
22

3-
translations = {
4-
"en": t"Hello, {name}! Welcome back!", # noqa
5-
"es": t"¡Hola, {name}! ¡Bienvenido de vuelta!", # noqa
6-
"fr": t"Bonjour, {name}! Bon retour!" # noqa
7-
}
3+
# Uncomment in Python 3.14+
4+
# translations = {
5+
# "en": t"Hello, {name}! Welcome back!",
6+
# "es": t"¡Hola, {name}! ¡Bienvenido de vuelta!",
7+
# "fr": t"Bonjour, {name}! Bon retour!"
8+
# }
89

910

1011
def get_localized_greeting(lang):
11-
template = translations.get(lang, translations["en"])
12-
parts = []
13-
for item in template:
14-
if isinstance(item, str):
15-
parts.append(item)
16-
else:
17-
parts.append(item.value)
18-
return "".join(parts)
12+
"Uncomment in Python 3.14+"
13+
# template = translations.get(lang, translations["en"])
14+
# parts = []
15+
# for item in template:
16+
# if isinstance(item, str):
17+
# parts.append(item)
18+
# else:
19+
# parts.append(item.value)
20+
# return "".join(parts)
1921

2022

2123
print(get_localized_greeting("en"))

python-t-strings/logging_message.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import logging
33

4-
logging.basicConfig(level=logging.INFO, format='%(message)s')
4+
logging.basicConfig(level=logging.INFO, format="%(message)s")
55

66

77
class TemplateMessage:
@@ -17,7 +17,7 @@ def message(self):
1717
else:
1818
parts.append(str(item.value))
1919
return "".join(parts)
20-
20+
2121
@property
2222
def values_dict(self):
2323
values = {}
@@ -30,6 +30,7 @@ def __str__(self):
3030
return f"{self.message} >>> {json.dumps(self.values_dict)}"
3131

3232

33-
action, amount, item = "refund", 7, "keyboard"
34-
msg_template = TemplateMessage(t"Process {action}: {amount:.2f} {item}") # noqa
35-
logging.info(msg_template)
33+
# Uncomment in Python 3.14+
34+
# action, amount, item = "refund", 7, "keyboard"
35+
# msg_template = TemplateMessage(t"Process {action}: {amount:.2f} {item}")
36+
# logging.info(msg_template)

python-t-strings/scape_html.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ def render_safe_html(template):
1111
return "".join(parts)
1212

1313

14-
username = "<script>alert('Hacked!')</script>"
15-
template = t"<p>Hello, {username}!</p>" # noqa
14+
# Uncomment in Python 3.14+
15+
# username = "<script>alert('Hacked!')</script>"
16+
# template = t"<p>Hello, {username}!</p>"
1617

17-
safe_html = render_safe_html(template)
18-
print(safe_html)
18+
# safe_html = render_safe_html(template)
19+
# print(safe_html)

python-t-strings/sql.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def sanitized_sql(template):
1313
return query, tuple(params)
1414

1515

16-
template = t"SELECT * FROM students WHERE name = '{username}';" # noqa
17-
query, params = sanitized_sql(template)
18-
print("Sanitized SQL Query:", query)
19-
print("Parameters:", params)
16+
# Uncomment in Python 3.14+
17+
# template = t"SELECT * FROM students WHERE name = '{username}';"
18+
# query, params = sanitized_sql(template)
19+
# print("Sanitized SQL Query:", query)
20+
# print("Parameters:", params)

python-t-strings/template_to_string.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ def to_string(template):
44
if isinstance(part, str):
55
parts.append(part)
66
else:
7-
value = {
8-
"a": ascii,
9-
"r": repr,
10-
"s": str}.get(part.conversion, lambda _: part.conversion)(part.value)
7+
value = {"a": ascii, "r": repr, "s": str}.get(
8+
part.conversion, lambda _: part.conversion
9+
)(part.value)
1110
value = format(value, part.format_spec)
1211
parts.append(value)
1312
return "".join(parts)
1413

1514

16-
price = 234.8765
17-
print(to_string(t"The price is ${price:.2f}")) # noqa
18-
print(to_string(t"The price is ${price!s:.2f}")) # noqa
15+
# Uncomment in Python 3.14+
16+
# price = 234.8765
17+
# print(to_string(t"The price is ${price:.2f}"))
18+
# print(to_string(t"The price is ${price!s:.2f}"))

python-t-strings/to_stirngs.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ def convert(value, conversion):
88
if isinstance(item, str):
99
parts.append(item)
1010
else:
11-
value = format(convert(item.value, item.conversion), item.format_spec)
11+
value = format(
12+
convert(item.value, item.conversion), item.format_spec
13+
)
1214
parts.append(value)
1315
return "".join(parts)
1416

1517

16-
price = 234.8765
17-
print(to_string(t"The price is ${price:.2f}")) # noqa
18+
# Uncomment in Python 3.14+
19+
# price = 234.8765
20+
# print(to_string(t"The price is ${price:.2f}"))
1821

19-
header = "Report"
20-
print(to_string(t"{header:=^20}")) # noqa
22+
# header = "Report"
23+
# print(to_string(t"{header:=^20}"))

python-t-strings/traverse_items.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
1-
from string.templatelib import Interpolation
1+
# Uncomment in Python 3.14+
22

3-
name = "Pythonista"
4-
site = "realpython.com"
5-
template = t"Hello, {name}! Welcome to {site}!" # noqa
3+
# from string.templatelib import Interpolation
64

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}!"
208

219

2210
# def build_report(template):
2311
# parts = ["Account Report:\n"]
2412
# 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}}")
2918
# parts.append("\n")
3019
# return "".join(parts)
3120

3221

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

Comments
 (0)