Skip to content

Instantly share code, notes, and snippets.

View dmontagu's full-sized avatar

David Montague dmontagu

  • Pydantic
  • Bozeman, MT
View GitHub Profile
@dmontagu
dmontagu / docker-compose.deploy.volumes-placement.yml
Last active October 12, 2021 03:33
Elasticsearch config files for use with FastAPI generated project
version: '3.3'
services:
db:
volumes:
- app-db-data:/var/lib/postgresql/data/pgdata
deploy:
placement:
constraints:
- node.labels.${STACK_NAME}.app-db-data == true
es01:
@dmontagu
dmontagu / cython_all.log
Last active May 25, 2019 23:48
Pydantic benchmark line-profiled
Full output on cython branch:
-----------------------------
pydantic time=6.347s, success=50.60%
pydantic time=7.167s, success=50.60%
pydantic time=8.733s, success=50.60%
pydantic time=6.832s, success=50.60%
pydantic time=7.747s, success=50.60%
pydantic best=6.347s, avg=7.365s, stdev=0.919s
pydantic best=211.579μs/iter avg=245.504μs/iter stdev=30.618μs/iter
@dmontagu
dmontagu / py37_cython_not_present.log
Created May 27, 2019 10:21
Benchmark timings with PyPy
pydantic time=1.073s, success=50.60%
pydantic time=1.078s, success=50.60%
pydantic time=1.071s, success=50.60%
pydantic time=1.071s, success=50.60%
pydantic time=1.092s, success=50.60%
pydantic best=1.071s, avg=1.077s, stdev=0.009s
@dmontagu
dmontagu / main.py
Last active June 14, 2019 13:18
FastAPI app with response shape wrapping
from functools import lru_cache
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
from pydantic import BaseModel, create_model
from starlette.requests import Request
from starlette.responses import JSONResponse
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
@dmontagu
dmontagu / generics.py
Created June 14, 2019 10:08
pydantic generics
from typing import Any, Dict, Generic, Optional, Tuple, Type, TypeVar, Union, get_type_hints, no_type_check
from pydantic import BaseModel, create_model, validator
from pydantic.class_validators import gather_validators
class BaseGenericModel:
def __init__(self, *args: Any, **kwargs: Any) -> None:
pass
@dmontagu
dmontagu / orm_mode_patch.py
Created June 15, 2019 06:33
Inherit from APIModel instead of BaseModel to get from_orm functionality
from typing import Any, Dict, Set, Type, TypeVar
from pydantic import BaseConfig, BaseModel, ConfigError, DictError, validate_model
from pydantic.utils import change_exception
Model = TypeVar("Model", bound=BaseModel)
class APIModel(BaseModel):
@dmontagu
dmontagu / mypy.ini
Last active August 7, 2019 09:23
Pydantic mypy plugin for signature checking
[mypy]
plugins = pydanticmypy.py
follow_imports = silent
strict_optional = True
warn_redundant_casts = True
warn_unused_ignores = True
disallow_any_generics = True
check_untyped_defs = True
ignore_missing_imports = True
@dmontagu
dmontagu / build.py
Created September 4, 2019 07:12
CMake + Cython extension building with poetry
"""
Adapted from https://github.com/pybind/cmake_example
"""
import os
import platform
import re
import subprocess
import sys
import sysconfig
from distutils.version import LooseVersion
@dmontagu
dmontagu / Makefile
Created September 5, 2019 09:42
FastAPI project makefile
.DEFAULT_GOAL := default
app_root = backend/app
pkg_src = $(app_root)/app
tests_src = $(app_root)/tests
local_tests_src = $(app_root)/tests/local
isort = isort -rc $(pkg_src) $(tests_src)
black = black $(pkg_src) $(tests_src)
flake8 = flake8 $(pkg_src) $(tests_src)
mypy = mypy $(pkg_src)
UserCreateT = TypeVar("UserCreateT", bound=UserCreate)
UserCreateRequestT = TypeVar("UserCreateRequestT", bound=UserCreateRequest)
UserInDBT = TypeVar("UserInDBT", bound=UserInDB)
UserUpdateT = TypeVar("UserUpdateT", bound=UserUpdate)
UserApiT = TypeVar("UserApiT", bound=UserBaseInDB)
UserOrmT = TypeVar("UserOrmT", bound=BaseUser)
logger = logging.getLogger(__name__)