Skip to content

Instantly share code, notes, and snippets.

View Kludex's full-sized avatar

Marcelo Trylesinski Kludex

View GitHub Profile
@Kludex
Kludex / collect.py
Created April 17, 2023 10:11
Scripts used to generate the list of objects on https://github.com/pydantic/pydantic/pull/5480
import importlib
import pkgutil
def get_public_objects(package_name, parent_module=""):
objects = []
full_package_name = (
parent_module + "." + package_name if parent_module else package_name
)
package = importlib.import_module(full_package_name)
@Kludex
Kludex / main.py
Created July 3, 2023 15:06
ObjectId with Pydantic protocol
from typing import Type, Callable, Any
from bson.objectid import ObjectId, InvalidId
from pydantic import BaseModel, ValidationError
from pydantic_core import CoreSchema, core_schema
class PydanticObjectId(ObjectId):
@classmethod
def __get_pydantic_core_schema__(
@Kludex
Kludex / main.py
Created July 13, 2023 08:24
How to create an `NonEmpty` type with Pydantic by Adrian
from typing import Any, Collection, List, TypeVar, Annotated
from annotated_types import MinLen
from pydantic import BaseModel
_CollectionT = TypeVar("_CollectionT", bound=Collection[Any])
NonEmpty = Annotated[_CollectionT, MinLen(1)]
@Kludex
Kludex / main.py
Created September 13, 2023 09:10
How to disable `model_` as protected namespace globally in Pydantic
from pydantic import BaseModel
BaseModel.model_config['protected_namespaces'] = ()
class Potato(BaseModel):
model_potato: str
class Carrot(BaseModel):
model_carrot: str
@Kludex
Kludex / count_perf.py
Created August 15, 2024 19:47
Count performance
def a(n: int) -> int:
records = 0
for _ in range(n):
for _ in range(n):
for _ in range(n):
records += 1
return records
def sum_a(n: int) -> int:
@Kludex
Kludex / main.py
Last active November 24, 2024 14:49
Test Typer with FastAPI `uv run pytest main.py`
from fastapi import FastAPI
import httpx
from typer import Typer, Context
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@Kludex
Kludex / main.py
Created April 14, 2025 15:47
MCP Server with Logfire
from mcp.server import FastMCP
import logfire
import uvicorn
logfire.configure()
server = FastMCP("Summarizer")
app = server.sse_app()
logfire.instrument_starlette(app)