Code style: one file, no external libraries besides pathlib and Pydantic, never use with open, Python 3.10+ modern code style (list
not List
style hints), no bloat or sprawl. Prefer list/dictcomps over loops where possible. Don't use global or otherwise 'free' variables for configuration when you could use a Pydantic model, and set default values on fields rather than setting them at runtime where applicable.
This file contains 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
uv add --dev $(tq -f pyproject.toml 'tool.pdm.dev-dependencies.test' -o json | jq -r '.[]') |
This file contains 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
from __future__ import annotations | |
from typing import Any | |
from pydantic import BaseModel, GetCoreSchemaHandler, TypeAdapter, constr | |
from pydantic_core import CoreSchema, core_schema | |
aa_ta = TypeAdapter(constr(pattern=r"^(?i)aa.{10}$")) |
This file contains 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
from pprint import pformat | |
from inline_snapshot import outsource, snapshot | |
def round_down_to_order_of_magnitude(n): | |
"""Small values go to a decimal, so `int` would coerce to zero""" | |
magnitude = 10 ** (len(str(n)) - 1) | |
return int((n // magnitude) * magnitude) or n |
This file contains 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
from pathlib import Path | |
from pydantic import BaseModel, BeforeValidator, TypeAdapter, model_validator | |
from typing import Annotated, Union, Any, Generic, TypeVar | |
RoutePart = Union[int, str] | |
rp_ta = TypeAdapter(RoutePart) | |
def split_route(route: str) -> list[RoutePart]: | |
match route: |
This file contains 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
from pathlib import Path | |
from pydantic import BaseModel, BeforeValidator, TypeAdapter | |
from typing import Annotated, Union | |
RoutePart = Union[int, str] | |
rp_ta = TypeAdapter(RoutePart) | |
def split_route(route: str) -> list[RoutePart]: | |
match route: |
This file contains 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
from pydantic import BaseModel, model_validator | |
class Model(BaseModel, extra="allow"): | |
a: int | |
b: int | |
@model_validator(mode="before") | |
@classmethod | |
def pop_off_extra(cls, values: dict): | |
fields = list(cls.model_fields) |
This file contains 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
from inspect import Parameter, signature | |
from typing import Any, Callable, ClassVar, Generic, ParamSpec, TypeVar | |
from pydantic import BaseModel, Field, model_validator | |
P = ParamSpec("P") | |
R = TypeVar("R") | |
T = TypeVar("T", bound=BaseModel) |
This file contains 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
from inspect import Parameter, signature | |
from typing import Any, Callable, Generic, ParamSpec, TypeVar | |
from pydantic import BaseModel, Field, model_validator | |
P = ParamSpec("P") | |
R = TypeVar("R") | |
T = TypeVar("T", bound=BaseModel) |
This file contains 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
from typing import Any, Callable, Generic, Literal, ParamSpec, TypeVar | |
from pydantic import BaseModel, Field, model_validator, TypeAdapter, NewPath, FilePath | |
from inspect import signature, Parameter | |
from pathlib import Path | |
P = ParamSpec("P") | |
R = TypeVar("R") | |
class FileEffect(BaseModel): |
NewerOlder