Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exclude = [
"agents-md/run1_main.py",
"python315-frozendict/",
"python315-lazy-imports",
"python315-new-features",
"ai-benchmark"
]

Expand Down
3 changes: 3 additions & 0 deletions python315-new-features/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python 3.15: Cool New Features for You to Try

This folder provides the code examples for the Real Python tutorial [Python 3.15: Cool New Features for You to Try](https://realpython.com/python315-new-features/)
15 changes: 15 additions & 0 deletions python315-new-features/ctxmgr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from contextlib import contextmanager

@contextmanager
def logged(label):
print(f"enter {label}")
yield
print(f"exit {label}")

@logged("reading")
def read_lines():
yield "first"
yield "second"

for line in read_lines():
print(line)
8 changes: 8 additions & 0 deletions python315-new-features/lazy_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sys

lazy import json

print("json loaded:", "json" in sys.modules)
config = json.dumps({"theme": "dark", "autosave": True})
print("json loaded:", "json" in sys.modules)
print(config)
1 change: 1 addition & 0 deletions python315-new-features/menu.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Crème brûlée — 7 €
18 changes: 18 additions & 0 deletions python315-new-features/python315_in_15_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys
from pathlib import Path
lazy import json

defaults = frozendict({"theme": "light", "autosave": True})
MISSING = sentinel("MISSING")

def resolve(overrides, name, default=MISSING):
value = (defaults | overrides).get(name, default)
return "unset" if value is MISSING else value

profiles = [{"theme": "crème brûlée"}, {"autosave": False}]
merged = {**profile for profile in profiles}
print("json imported:", "json" in sys.modules)
Path("user.json").write_text(json.dumps(defaults | merged, ensure_ascii=False))
print("json imported:", "json" in sys.modules)
print("theme:", resolve(merged, "theme"), "| font:", resolve(merged, "font"))
print(Path("user.json").read_text())
1 change: 1 addition & 0 deletions python315-new-features/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyright==1.1.414
14 changes: 14 additions & 0 deletions python315-new-features/typeddict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import TypedDict

class Movie(TypedDict, closed=True):
title: str
year: int

class Headers(TypedDict, extra_items=str):
content_type: str

movie: Movie = {"title": "Brazil", "year": 1985, "rating": 8.0}
headers: Headers = {"content_type": "text/html", "x-trace-id": "abc123"}
bad_headers: Headers = {"content_type": "text/html", "x-retries": 3}

print(Movie.__closed__, Headers.__extra_items__)
11 changes: 11 additions & 0 deletions python315-new-features/typeform_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import TypeForm, reveal_type

def parse_old[T](value: str, typ: type[T]) -> T: ...

def parse_new[T](value: str, typ: TypeForm[T]) -> T: ...

reveal_type(parse_old("42", int))
reveal_type(parse_old("42", int | None))
reveal_type(parse_new("42", int | None))
reveal_type(parse_new("42", list[int]))
parse_new("42", 42)
8 changes: 8 additions & 0 deletions python315-new-features/wordstats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from collections import Counter

def most_common_words(text, n=3):
words = [word.strip(".,").lower() for word in text.split()]
return Counter(words).most_common(n)

sample = "The quick brown fox jumps over the lazy dog. " * 100_000
print(most_common_words(sample))
Loading