Skip to content

Instantly share code, notes, and snippets.

Avatar

Martin Thoma MartinThoma

View GitHub Profile
View scope_example.py
print(min([1, 2, 3]))
def foo():
min = lambda n: "enclosing"
def bar():
"""Bar is enclosed by 'foo'"""
print(min([1, 2, 3]))
def baz():
View pydantic_hypothesis_pytest.py
import pytest
from hypothesis import given
from hypothesis.strategies import from_type
from pydantic import ValidationError
from model import Person
from pydantic_example import get_account_difference
@given(from_type(Person), from_type(Person))
View rename_serialize.py
class Person(BaseModel):
id: PersonId
name: str
bank_account: Decimal
birthdate: datetime.date
friends: Optional[List[PersonId]] = None
class Config:
allow_population_by_field_name = True
# We use the Python attribute 'bank_account',
View self_friend_fix.py
from decimal import Decimal
from typing import List, NewType, Optional
import datetime
from pydantic import BaseModel, root_validator
PersonId = NewType("PersonId", int)
class Person(BaseModel):
View no_self_friend_error.py
from decimal import Decimal
from typing import List, NewType, Optional
import datetime
from pydantic import BaseModel, root_validator
PersonId = NewType("PersonId", int)
class Person(BaseModel):
View pydantic_password.py
from pydantic import BaseModel, validator
class UserModel(BaseModel):
name: str
username: str
password1: str
password2: str
@validator("name")
View show_extra.py
class Person(BaseModel):
id: PersonId
name: str
bank_account: Decimal
birthdate: datetime.date
friends: Optional[List[PersonId]] = None
class Config:
extra = "forbid"
View default_empty_list.py
from pydantic import BaseModel, Field
class Person(BaseModel):
friends : List[int] = Field(default_factory=list)
View pydantic-article-2.py
from pathlib import Path
from typing import List, Mapping
import numpy as np
from model import Person, PersonId
from pydantic import parse_file_as
def main(filepath: Path):
people = get_people(filepath)
View input_parsing.py
from pathlib import Path
from typing import List, Mapping
from model import Person, PersonId
from pydantic import parse_file_as
def get_people(filepath: Path) -> Mapping[PersonId, Person]:
people = parse_file_as(List[Person], filepath)
return {person.id: person for person in people}
You can’t perform that action at this time.