Skip to content

Instantly share code, notes, and snippets.

View Kludex's full-sized avatar

Marcelo Trylesinski Kludex

View GitHub Profile
@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 / 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 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 / 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 April 13, 2023 08:14
Create a virtual environment, and run a different python process.
"""Create a virtual environment, and run a different python process."""
import os
import subprocess
import tempfile
import venv
with tempfile.TemporaryDirectory() as temp_dir:
VENV_PATH = os.path.join(temp_dir, "venv")
venv.create(VENV_PATH, with_pip=True)
python_exe = os.path.join(VENV_PATH, "bin", "python")
@Kludex
Kludex / build_monorepo.sh
Last active December 28, 2022 15:02
Build a monorepo repository from scratch!
#!/bin/sh
# Creates a new monorepo by fusing multiple repositories
# Mainly copied from http://www.sevangelatos.com/monorepo/
# Child repositories that are going to be fused
CHILDREN="repo_lib_a repo_lib_b"
# Name of the created monorepo
MONOREPO="monorepo"
@Kludex
Kludex / main.py
Created December 5, 2022 08:58
Uvicorn logging for prod & dev
import logging
import sys
from typing import List
import structlog
import uvicorn
from fastapi import FastAPI
from structlog.stdlib import ProcessorFormatter
from structlog.types import Processor
@Kludex
Kludex / main.py
Last active December 4, 2022 19:46
Use WakaTime API
import hashlib
import os
import httpx
from httpx_auth import OAuth2AuthorizationCode
from pydantic import BaseSettings
from rich.console import Console
console = Console()
@Kludex
Kludex / main.py
Created November 15, 2022 07:39
Inject JS script into ASGI application
from __future__ import annotations
from starlette.datastructures import MutableHeaders
BROWSER_SYNC_SCRIPT =b"<script>console.log('Hi there!');</script>"
class InjectScriptMiddleware:
def __init__(self, app, script: bytes = BROWSER_SYNC_SCRIPT):
self.app = app
self.script = script
@Kludex
Kludex / main.py
Created October 18, 2022 14:29
Mandatorily have a parameter, either a or b.
from __future__ import annotations
from typing import overload
@overload
def potato(a: int, b: None = None) -> int:
...