Skip to content

Commit 6847426

Browse files
committed
python: add 3.11.py (TaskGroup, ExceptionGroup, tomllib, StrEnum); normalize 3.10.py to use passed() and proper context manager
1 parent 4d01a17 commit 6847426

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

python/3.10.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3.10
2+
3+
# https://docs.python.org/3.10/whatsnew/index.html
4+
5+
from passed import passed
6+
7+
num = 19
8+
assert bin(num) == '0b10011'
9+
assert num.bit_count() == 3
10+
11+
#assertNoLogs
12+
13+
class CtxManager1():
14+
def __enter__(self):
15+
return self
16+
def __exit__(self, exc_type, exc, tb):
17+
return False
18+
19+
20+
with (CtxManager1() as example1,
21+
CtxManager1() as example2,
22+
):
23+
pass
24+
25+
26+
match 1:
27+
case 1:
28+
pass
29+
case _:
30+
pass
31+
32+
passed()

python/3.11.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3.11
2+
3+
# https://docs.python.org/3.11/whatsnew/3.11.html
4+
5+
from passed import passed
6+
7+
import asyncio
8+
import enum
9+
import tomllib
10+
11+
12+
# ExceptionGroup and except* (PEP 654)
13+
def _raise_exception_group() -> None:
14+
raise ExceptionGroup(
15+
"demo",
16+
[ValueError("bad value"), TypeError("bad type")],
17+
)
18+
19+
20+
handled_value_error = False
21+
handled_type_error = False
22+
23+
try:
24+
_raise_exception_group()
25+
except* ValueError as eg:
26+
handled_value_error = True
27+
# Each subgroup only contains the matched exception type
28+
assert all(isinstance(e, ValueError) for e in eg.exceptions)
29+
except* TypeError as eg:
30+
handled_type_error = True
31+
assert all(isinstance(e, TypeError) for e in eg.exceptions)
32+
33+
assert handled_value_error and handled_type_error
34+
35+
36+
# tomllib in the standard library (PEP 680)
37+
toml_text = """
38+
name = "alice"
39+
age = 42
40+
"""
41+
data = tomllib.loads(toml_text)
42+
assert data == {"name": "alice", "age": 42}
43+
44+
# enum.StrEnum (bakes in str behavior)
45+
class Color(enum.StrEnum):
46+
RED = "red"
47+
BLUE = "blue"
48+
49+
assert Color.RED == "red"
50+
assert isinstance(Color.BLUE, str)
51+
52+
# asyncio.TaskGroup (structured concurrency)
53+
async def _tg_demo() -> int:
54+
async with asyncio.TaskGroup() as tg:
55+
task_one = tg.create_task(asyncio.sleep(0.01, result=1))
56+
task_two = tg.create_task(asyncio.sleep(0.01, result=2))
57+
return task_one.result() + task_two.result()
58+
59+
assert asyncio.run(_tg_demo()) == 3
60+
61+
passed()

python/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ all:
1414
-./3.7.py
1515
./3.8.py
1616
./3.9.py
17+
./3.10.py
18+
./3.11.py

python/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Python features examples by releases
22

33
## [2.0 - 2.3](2.3.py), [2.4](2.4.py), [2.5](2.5.py), [2.6](2.6.py), [2.7](2.7.py)
4-
## [3.0](3.0.py), [3.1](3.1.py), [3.2](3.2.py), [3.3](3.3.py), [3.4](3.4.py), [3.5](3.5.py), [3.6](3.6.py), [3.7](3.7.py), [3.8](3.8.py), [3.9](3.9.py)
4+
## [3.0](3.0.py), [3.1](3.1.py), [3.2](3.2.py), [3.3](3.3.py), [3.4](3.4.py), [3.5](3.5.py), [3.6](3.6.py), [3.7](3.7.py), [3.8](3.8.py), [3.9](3.9.py), [3.10](3.10.py), [3.11](3.11.py)
55

66
## Features
77
* Minimal and practical self descriptive as possible source code.

0 commit comments

Comments
 (0)