Skip to content

Commit 1e79808

Browse files
authored
Add Python 3.11 examples from the final tutorial (#319)
* Add Python 3.11 examples from the final tutorial * Linter * Fix typo * Final QA updates
1 parent fe609dd commit 1e79808

19 files changed

+515
-22
lines changed

python-311/README.md

Lines changed: 198 additions & 20 deletions
Large diffs are not rendered by default.

python-311/active_exception.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import sys
22

33
try:
4-
raise ValueError("bpo-46328")
4+
raise ValueError("gh-90486")
55
except ValueError:
66
print(f"Handling {sys.exception()}")
77

88
# Typically you should prefer except ValueError as err:
99
try:
10-
raise ValueError("bpo-46328")
10+
raise ValueError("gh-90486")
1111
except ValueError as err:
1212
print(f"Handling {err}")

python-311/dead_imghdr.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import imghdr
2+
3+
import magic
4+
5+
print(imghdr.what("python-311.jpg"))
6+
7+
print(magic.from_file("python-311.jpg"))

python-311/download_peps_gather.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
3+
import aiohttp
4+
5+
PEP_URL = (
6+
"https://raw.githubusercontent.com/python/peps/master/pep-{pep:04d}.txt"
7+
)
8+
9+
10+
async def main(peps):
11+
async with aiohttp.ClientSession() as session:
12+
await download_peps(session, peps)
13+
14+
15+
async def download_peps(session, peps):
16+
tasks = [asyncio.create_task(download_pep(session, pep)) for pep in peps]
17+
await asyncio.gather(*tasks)
18+
19+
20+
async def download_pep(session, pep):
21+
print(f"Downloading PEP {pep}")
22+
url = PEP_URL.format(pep=pep)
23+
async with session.get(url, params={}) as response:
24+
pep_text = await response.text()
25+
26+
title = pep_text.split("\n")[1].removeprefix("Title:").strip()
27+
print(f"Downloaded PEP {pep}: {title}")
28+
29+
30+
asyncio.run(main([492, 525, 530, 3148, 3156]))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import asyncio
2+
3+
import aiohttp
4+
5+
PEP_URL = (
6+
"https://raw.githubusercontent.com/python/peps/master/pep-{pep:04d}.txt"
7+
)
8+
9+
10+
async def main(peps):
11+
async with aiohttp.ClientSession() as session:
12+
await download_peps(session, peps)
13+
14+
15+
async def download_peps(session, peps):
16+
async with asyncio.TaskGroup() as tg:
17+
for pep in peps:
18+
tg.create_task(download_pep(session, pep))
19+
20+
21+
async def download_pep(session, pep):
22+
print(f"Downloading PEP {pep}")
23+
url = PEP_URL.format(pep=pep)
24+
async with session.get(url, params={}) as response:
25+
pep_text = await response.text()
26+
27+
title = pep_text.split("\n")[1].removeprefix("Title:").strip()
28+
print(f"Downloaded PEP {pep}: {title}")
29+
30+
31+
asyncio.run(main([492, 525, 530, 3148, 3156]))

python-311/inverse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def inverse(number):
2+
return 1 / number
3+
4+
5+
print(inverse(0))

python-311/pair_order.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import TypeVar
2+
3+
T0 = TypeVar("T0")
4+
T1 = TypeVar("T1")
5+
6+
7+
def flip(pair: tuple[T0, T1]) -> tuple[T1, T0]:
8+
first, second = pair
9+
return (second, first)
10+
11+
12+
pair = ("one", "two")
13+
print(f"{pair} -> {flip(pair)}")

python-311/programmers.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{"name": {"first": "Uncle Barry"}},
3+
{
4+
"name": {"first": "Ada", "last": "Lovelace"},
5+
"birth": {"year": 1815},
6+
"death": {"month": 11, "day": 27}
7+
},
8+
{
9+
"name": {"first": "Grace", "last": "Hopper"},
10+
"birth": {"year": 1906, "month": 12, "day": 9},
11+
"death": {"year": 1992, "month": 1, "day": 1}
12+
},
13+
{
14+
"name": {"first": "Ole-Johan", "last": "Dahl"},
15+
"birth": {"year": 1931, "month": 10, "day": 12},
16+
"death": {"year": 2002, "month": 6, "day": 29}
17+
},
18+
{
19+
"name": {"first": "Guido", "last": "Van Rossum"},
20+
"birth": {"year": 1956, "month": 1, "day": 31},
21+
"death": null
22+
}
23+
]

python-311/programmers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
import pathlib
3+
from dataclasses import dataclass
4+
from typing import Any, Self, TypeAlias
5+
6+
Info: TypeAlias = dict[str, Any]
7+
8+
programmers = json.loads(
9+
pathlib.Path("programmers.json").read_text(encoding="utf-8")
10+
)
11+
12+
13+
@dataclass
14+
class Person:
15+
name: str
16+
life_span: tuple[int, int]
17+
18+
@classmethod
19+
def from_dict(cls, info: Info) -> Self:
20+
return cls(
21+
name=f"{info['name']['first']} {info['name']['last']}",
22+
life_span=(info["birth"]["year"], info["death"]["year"]),
23+
)
24+
25+
26+
def convert_pair(first: Info, second: Info) -> tuple[Person, Person]:
27+
return Person.from_dict(first), Person.from_dict(second)

python-311/python-311.jpg

329 KB
Loading

0 commit comments

Comments
 (0)