This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel, validator | |
class UserModel(BaseModel): | |
name: str | |
username: str | |
password1: str | |
password2: str | |
@validator("name") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person(BaseModel): | |
id: PersonId | |
name: str | |
bank_account: Decimal | |
birthdate: datetime.date | |
friends: Optional[List[PersonId]] = None | |
class Config: | |
extra = "forbid" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel, Field | |
class Person(BaseModel): | |
friends : List[int] = Field(default_factory=list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |