-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Python 3.14: Cool New Features (WIP) #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bzaczynski
wants to merge
3
commits into
master
Choose a base branch
from
python-314
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Python 3.14: Cool New Features for You to Try | ||
|
||
The materials contained in this folder are designed to complement the Real Python tutorial [Python 3.14: Cool New Features for You to Try](https://realpython.com/python314-new-features/). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def fib(n): | ||
print(f"Calculating fib({n})...") | ||
return n if n < 2 else fib(n - 2) + fib(n - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
try: | ||
from sys import _jit | ||
except ImportError: | ||
print("Module sys._jit unavailable") | ||
else: | ||
print("Python compiled with JIT support:", _jit.is_available()) | ||
print("JIT enabled for this process:", _jit.is_enabled()) | ||
print("JIT currently running:", _jit.is_active()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Uncomment for Python 3.14 | ||
# | ||
# from dataclasses import dataclass | ||
# from typing import Any, Optional | ||
# | ||
# | ||
# @dataclass | ||
# class LinkedList: | ||
# head: Node | ||
# | ||
# | ||
# @dataclass | ||
# class Node: | ||
# value: Any | ||
# next: Optional[Node] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser("Command-Line Interface") | ||
parser.add_argument("path", help="Path to a file") | ||
parser.parse_args() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Set a custom color theme in Python 3.14 | ||
try: | ||
from _colorize import ANSIColors, default_theme, set_theme | ||
except ImportError: | ||
pass | ||
else: | ||
custom_theme = default_theme.copy_with( | ||
syntax=default_theme.syntax.copy_with( | ||
prompt=ANSIColors.GREY, | ||
builtin="\x1b[38;2;189;147;249m", | ||
comment="\x1b[38;2;98;114;164m", | ||
definition="\x1b[38;2;139;233;253m", | ||
keyword="\x1b[38;2;255;121;198m", | ||
keyword_constant="\x1b[38;2;255;121;198m", | ||
soft_keyword="\x1b[38;2;255;121;198m", | ||
number="\x1b[38;2;189;147;249m", | ||
op="\x1b[38;2;249;152;204m", | ||
string="\x1b[38;2;241;250;140m", | ||
), | ||
traceback=default_theme.traceback.copy_with( | ||
error_highlight=ANSIColors.BOLD_YELLOW, | ||
error_range=ANSIColors.YELLOW, | ||
filename=ANSIColors.BACKGROUND_RED, | ||
frame=ANSIColors.BACKGROUND_RED, | ||
line_no=ANSIColors.BACKGROUND_RED, | ||
message=ANSIColors.RED, | ||
type=ANSIColors.BOLD_RED, | ||
), | ||
) | ||
set_theme(custom_theme) | ||
|
||
# Set a custom shell prompt | ||
import platform | ||
import sys | ||
|
||
version = platform.python_version() | ||
sys.ps1 = f"{version} \N{SNAKE} " | ||
sys.ps2 = "." * len(sys.ps1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import unittest | ||
|
||
|
||
class TestDummy(unittest.TestCase): | ||
def test_dummy(self): | ||
self.assertEqual(2 + 2, 22) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Shopping List": [{"title": "eggs", "details": null, "quantity": 12, "completed": true}, {"title": "bacon", "details": "smoke, 500g pack", "quantity": 1, "completed": false}, {"title": "ghee", "details": null, "quantity": 1, "completed": false}]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from concurrent.futures import ( | ||
InterpreterPoolExecutor, | ||
ProcessPoolExecutor, | ||
ThreadPoolExecutor, | ||
) | ||
from time import perf_counter | ||
|
||
MAX_VALUE = 35 | ||
NUM_VALUES = 4 | ||
NUM_WORKERS = 4 | ||
|
||
|
||
def fib(n): | ||
return n if n < 2 else fib(n - 2) + fib(n - 1) | ||
|
||
|
||
def compute(Pool): | ||
t1 = perf_counter() | ||
with Pool(max_workers=NUM_WORKERS) as pool: | ||
list(pool.map(fib, [MAX_VALUE] * NUM_VALUES)) | ||
t2 = perf_counter() | ||
print(f"{Pool.__name__}: {(t2 - t1):.2f}s") | ||
|
||
|
||
if __name__ == "__main__": | ||
compute(InterpreterPoolExecutor) | ||
compute(ProcessPoolExecutor) | ||
compute(ThreadPoolExecutor) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from dataclasses import dataclass | ||
from string.templatelib import Interpolation, Template, convert | ||
from typing import Any | ||
|
||
|
||
@dataclass(frozen=True) | ||
class SQLQuery: | ||
statement: str | ||
params: list[Any] | ||
|
||
def __init__(self, template: Template) -> None: | ||
items, params = [], [] | ||
for item in template: | ||
match item: | ||
case str(): | ||
items.append(item) | ||
case Interpolation(value, _, conversion, format_spec): | ||
converted = convert(value, conversion) | ||
if format_spec: | ||
converted = format(converted, format_spec) | ||
params.append(converted) | ||
items.append("?") | ||
object.__setattr__(self, "statement", "".join(items)) | ||
object.__setattr__(self, "params", params) | ||
|
||
|
||
def find_users_query_v1(name: str) -> str: | ||
"""Return a SQL query to find users by name.""" | ||
return f"SELECT * FROM users WHERE name = '{name}'" | ||
|
||
|
||
# Uncomment for Python 3.14: | ||
# | ||
# def find_users_query_v2(name: str) -> Template: | ||
# """Return a SQL query to find users by name.""" | ||
# return t"SELECT * FROM users WHERE name = '{name}'" | ||
# | ||
# | ||
# def find_users(name: str) -> SQLQuery: | ||
# """Return a SQL query to find users by name.""" | ||
# return SQLQuery(t"SELECT * FROM users WHERE name = {name}") | ||
|
||
|
||
def render(template: Template) -> str: | ||
return "".join( | ||
f"{text}{value}" | ||
for text, value in zip(template.strings, template.values) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Insecure f-strings | ||
print(find_users_query_v1("' OR '1'='1")) | ||
|
||
# Uncomment for Python 3.14: | ||
# | ||
# # More secure t-strings | ||
# print(find_users_query_v2("' OR '1'='1")) | ||
# | ||
# # Insecure way of rendering t-strings into plain strings | ||
# print(render(find_users_query_v2("' OR '1'='1"))) | ||
# | ||
# # Rendering t-strings into an alternative representation | ||
# print(find_users("' OR '1'='1")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import sys | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
|
||
HOSTNAME = "localhost" | ||
PORT = 8000 | ||
|
||
|
||
class Handler(BaseHTTPRequestHandler): | ||
def do_GET(self): | ||
user_agent = self.headers.get("User-Agent") | ||
self.send_response(200) | ||
self.end_headers() | ||
self.wfile.write(f"Hello, {user_agent}".encode()) | ||
|
||
|
||
def main(): | ||
print(f"Starting the server on: http://{HOSTNAME}:{PORT}") | ||
print("Run this command as a superuser to attach the debugger:") | ||
print(f"{sys.executable} -m pdb -p {os.getpid()}") | ||
HTTPServer((HOSTNAME, PORT), Handler).serve_forever() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the tutorial, we could mention that the sample files used to showcase the REPL's syntax highlighting features are available in the
materials
repo?