Libraries: Pydantic Flashcards

(10 cards)

1
Q

Define simple pydantic model

A
  • Derive from BaseModel
  • Use Field for validation
from pydantic import BaseModel

class Employee(BaseModel):
    employee_id: UUID = Field(default_factory=uuid4, frozen=True)
    department: Department
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Defiine a field that is:
- Random uuid4 by default
- Read only

A
from uuid import UUID, uuid4

employee_id: UUID = Field(default_factory=uuid4, frozen=True)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Defiine a string field that:
- has at least size 1
- has at most size 10

A

name: str = Field(min_length=1, max_length=10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Defiine a float field that:
- is greater than 0
- is less than 110
- doesn’t show up in string representation of an object (like password)
- can be accessed by different name

A

salary: float = Field(alias="compensation", gt=0, lt=110, repr=False)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  • Create pydantic model from dict
  • Convert model to dict
A

From dict

# From
Employee.model_validate(new_employee_dict)

To
new_employee.model_dump()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  • Create pydantic model from JSON string
  • Convert model to JSON string
A

From JSON

new_employee = Employee.model_validate_json(new_employee_json)

To JSON
new_employee.model_dump_json()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generate JSON schema of Employee model

A

Employee.model_json_schema()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to define custom validation for a field

A

Use field_validator

class Employee(BaseModel):
    date_of_birth: date = Field(alias="birth_date", repr=False, frozen=True)

    @field_validator("date_of_birth")
    @classmethod
    def check_valid_age(cls, date_of_birth: date) -> date:
        pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to define custom validation that involves multiple fields

A

Use model_validator

class Employee(BaseModel):
    # `mode="after"` - Pydantic waits until after you’ve instantiated your model to run `.check_it_benefits()`.
    @model_validator(mode="after")
    def check_it_benefits(self) -> Self:
        pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is pydantic-settings

A
  • You can use pydantic-settings to create models, similar to BaseModel, that parse and validate environment variables.
  • The main class in pydantic-settings is BaseSettings, and it has all of the same functionalities as BaseModel.
  • However, if you create a model that inherits from BaseSettings, the model initializer will try to read any fields not passed as keyword arguments from environment variables.
  • BaseSettings is not case-sensitive when matching environment variables to field names.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly