# ===== CodeLab: Python 3.12 新特性:f-string 增强与 PEP 695 类型参数 =====
# 来源: https://aoerliang.dpdns.org/articles/python-312-features
# 以下代码片段按文章出现顺序拼接, 共 6 段

# ----- 片段 1 (python) -----
lines = ["a", "b", "c"]
print(f"{'\n'.join(lines)}")  # 反斜杠转义,3.12 前是 SyntaxError

lang = "Python"
print(f"{f"{lang}"}")  # 同引号嵌套 f-string,3.12 前也是 SyntaxError

def fmt(width):
    return f"{3.14159:{width}.2f}"  # 同一引号随意复用

print(fmt(10))  # '      3.14'

# ----- 片段 2 (python) -----
info = {"name": "Python", "year": 1991}
print(f"结果: {
    info["name"].upper()  # 表达式内注释,3.12 起支持
}")  # 输出: 结果: PYTHON

# ----- 片段 3 (python) -----
from collections.abc import Callable

type IntList = list[int]
type Callback = Callable[[int], str]

def first(seq: IntList) -> int:
    return seq[0]

# ----- 片段 4 (python) -----
# 旧写法:TypeVar + Generic 双导入
from typing import TypeVar, Generic

T = TypeVar("T")

class Box(Generic[T]):
    def __init__(self, value: T):
        self.value = value

    def get(self) -> T:
        return self.value


# 新写法:class Box[T],一个方括号搞定
class Box[T]:
    def __init__(self, value: T):
        self.value = value

    def get(self) -> T:
        return self.value


def first_item[T](items: list[T]) -> T:
    return items[0]

# ----- 片段 5 (python) -----
from itertools import batched

for batch in batched(range(10), 3):
    print(batch)
# (0, 1, 2)
# (3, 4, 5)
# (6, 7, 8)
# (9,)

# ----- 片段 6 (bash) -----
pyenv install 3.12.9
pyenv local 3.12.9
python3.12 --version
python3.12 -m pip install --upgrade pip
python3.12 -m venv .venv && source .venv/bin/activate
