Skip to content

Commit 4923fc0

Browse files
committed
Post final QA
1 parent 7902b4e commit 4923fc0

File tree

7 files changed

+52
-48
lines changed

7 files changed

+52
-48
lines changed

python-t-strings/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Python 3.14 Preview: Template Strings (T-Strings)
22

3-
This folder provides the code examples for the Real Python tutorial [Python 3.14 Preview: Template Strings (T-Strings)](https://realpython.com/python-t-string/).
3+
This folder provides the code examples for the Real Python tutorial [Python 3.14 Preview: Template Strings (T-Strings)](https://realpython.com/python-t-strings/).

python-t-strings/asynchronous.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import asyncio
22

33

4-
async def get_name():
5-
# Simulate an asynchronous operation
6-
await asyncio.sleep(1)
7-
return "Pythonista"
4+
async def main():
5+
greeting = await greeting_template()
6+
print(greeting)
87

98

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

1412

15-
async def main():
16-
greeting = await greeting_template()
17-
print(greeting)
13+
async def get_name():
14+
# Simulate an asynchronous operation
15+
await asyncio.sleep(0.5)
16+
return "Pythonista"
1817

1918

2019
asyncio.run(main())
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ def generate_safe_html(template):
1414
return "".join(parts)
1515

1616

17-
# Uncomment in Python 3.14+
18-
# username = "<script>alert('Hacked!')</script>"
19-
# template = t"<p>Hello, {username}!</p>"
17+
username = "<script>alert('Hacked!')</script>"
18+
template = t"<p>Hello, {username}!</p>"
2019

21-
# safe_html = render_safe_html(template)
22-
# print(safe_html)
20+
safe_html = generate_safe_html(template)
21+
print(safe_html)

python-t-strings/logging_message.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def __str__(self):
3333
return f"{self.message} >>> {json.dumps(self.values_dict)}"
3434

3535

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

python-t-strings/sql.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ def sanitized_sql(template):
1818
return query, tuple(params)
1919

2020

21-
# Uncomment in Python 3.14+
22-
# username = "john'); DROP TABLE students;--"
23-
# template = t"SELECT * FROM students WHERE name = {username}"
24-
# query, params = sanitized_sql(template)
25-
# print("Sanitized SQL Query:", query)
21+
username = "'; DROP TABLE students;--"
22+
template = t"SELECT * FROM students WHERE name = {username}"
23+
query, params = sanitized_sql(template)
2624

27-
# print("Parameters:", params)
25+
print("Sanitized SQL Query:", query)
26+
print("Parameters:", params)

python-t-strings/to_string.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from string.templatelib import Template
2+
3+
4+
def to_string(template):
5+
if not isinstance(template, Template):
6+
raise TypeError("t-string expected")
7+
8+
def convert(value, conversion):
9+
func = {
10+
"a": ascii, "r": repr, "s": str
11+
}.get(conversion, lambda x: x)
12+
return func(value)
13+
14+
parts = []
15+
for item in template:
16+
if isinstance(item, str):
17+
parts.append(item)
18+
else:
19+
value = format(
20+
convert(item.value, item.conversion),
21+
item.format_spec
22+
)
23+
parts.append(value)
24+
return "".join(parts)
25+
26+
27+
price = 234.8765
28+
print(to_string(t"The price is ${price:.2f}"))
29+
30+
header = "Report"
31+
print(to_string(t"{header:=^20}"))

python-t-strings/to_strings.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)