Skip to content

Instantly share code, notes, and snippets.

View Tinche's full-sized avatar

Tin Tvrtković Tinche

View GitHub Profile
from typing import Any, Callable, Final, Generic, TypeVar
T = TypeVar("T")
class StructureSpec(Generic[T]):
required_attrs: list[tuple[str, Callable]]
optional_attrs: list[tuple[str, Callable]]
cls: type[T]
>>> from cattr import structure
>>> structure("a", int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/tintvrtkovic/pg/incant/.venv/lib/python3.9/site-packages/cattr/converters.py", line 300, in structure
return self._structure_func.dispatch(cl)(obj, cl)
File "/Users/tintvrtkovic/pg/incant/.venv/lib/python3.9/site-packages/cattr/converters.py", line 404, in _structure_call
return cl(obj)
ValueError: invalid literal for int() with base 10: 'a'
# A function with a single dependency:
Benchmarking incant: Mean +- std dev: 390 ns +- 17 ns
Benchmarking wired: Mean +- std dev: 54.3 us +- 2.0 us
Benchmarking di: Mean +- std dev: 14.1 us +- 0.4 us
Benchmarking dependency_injector: Mean +- std dev: 2.61 us +- 0.08 us
# A function with two nested dependencies, taking a parameter:
Benchmarking incant: Mean +- std dev: 651 ns +- 19 ns
@Tinche
Tinche / a04.py
Created September 16, 2021 23:23
Environment variable loading using cattrs
from os import environ
from typing import TypeVar
from attr import NOTHING, define, fields
from cattr import GenConverter
from cattr._compat import is_sequence
@define
from typing import (
Callable,
Optional,
Type,
)
from mypy.plugin import FunctionContext, Plugin
from mypy.nodes import (
Block,
ClassDef,
from pydantic import BaseModel, constr
class Test(BaseModel):
a_string: str = constr(min_length=20, max_length=1000)
t = Test()
> python run.py
generating test cases...
testing pydantic, attrs + cattrs, valideer, marshmallow, voluptuous, trafaret, schematics, django-rest-framework, cerberus, 5 times each
pydantic (1/5) time=0.662s, success=48.45%
pydantic (2/5) time=0.642s, success=48.45%
pydantic (3/5) time=0.633s, success=48.45%
pydantic (4/5) time=0.643s, success=48.45%
pydantic (5/5) time=0.634s, success=48.45%
pydantic best=0.633s, avg=0.643s, stdev=0.012s
FROM python:3.6.5-stretch as builder
WORKDIR /app
RUN python3 -m venv .venv
COPY src/hr ./src/hr/
COPY src/fbs ./src/fbs/
COPY setup.py .
RUN .venv/bin/pip install wheel
RUN .venv/bin/pip install .
import attr
@attr.s
class C:
a = attr.ib()
__hash__ = attr.hash_by_values
> pyperf timeit -g -s "i = [(e, 1) for e in range(1000)]" "[e[0] for e in i]"
.....................
39.5 us: 1 ###########
39.6 us: 2 #######################
39.7 us: 4 #############################################
39.9 us: 6 ####################################################################
40.0 us: 6 ####################################################################
40.1 us: 7 ###############################################################################
40.2 us: 6 ####################################################################
40.3 us: 6 ####################################################################