Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Created July 25, 2019 16:49
Show Gist options
  • Save samuelcolvin/2971a63ed6980d2e850e5136e2f2f357 to your computer and use it in GitHub Desktop.
Save samuelcolvin/2971a63ed6980d2e850e5136e2f2f357 to your computer and use it in GitHub Desktop.
call with `kernprof -lv profiling_pydantic.py`, doesn't seem to work with cythonized source
import json
from datetime import datetime
from pathlib import Path
from typing import List
from pydantic import BaseModel, PositiveInt, compiled, constr, version, validate_model
from pydantic.validators import _VALIDATORS, IfConfig
print(f'pydantic {version.VERSION}, compiled: {compiled}')
class Model(BaseModel):
id: int
client_name: constr(max_length=255)
sort_index: float
# client_email: EmailStr = None
client_phone: constr(max_length=255) = None
class Location(BaseModel):
latitude: float = None
longitude: float = None
location: Location = None
contractor: PositiveInt = None
upstream_http_referrer: constr(max_length=1023) = None
grecaptcha_response: constr(min_length=20, max_length=1000)
last_updated: datetime = None
class Skill(BaseModel):
subject: str
subject_id: int
category: str
qual_level: str
qual_level_id: int
qual_level_ranking: float = 0
skills: List[Skill] = []
THIS_DIR = Path(__file__).parent
json_path = THIS_DIR / 'benchmarks' / 'cases.json'
with json_path.open() as f:
cases = json.load(f)
for field in Model.__fields__.values():
profile.add_function(field.validate)
profile.add_function(field._validate_singleton)
profile.add_function(field._apply_validators)
for v in field.validators:
profile.add_function(v)
for val_type, validators in _VALIDATORS:
for v in validators:
if isinstance(v, IfConfig):
profile.add_function(v.validator)
else:
profile.add_function(v)
@profile
def run():
for case in cases:
validate_model(Model, case, raise_exc=False)
# profile.add_function(validate_model)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment