Code
from pydantic import BaseModel
class InventoryItem(BaseModel):
str
item_name: float
price: = InventoryItem(item_name="Banana", price=10)
my_item my_item
InventoryItem(item_name='Banana', price=10.0)
DSAN 5500: Data Structures, Objects, and Algorithms in Python
from pydantic import BaseModel
class InventoryItem(BaseModel):
str
item_name: float
price: = InventoryItem(item_name="Banana", price=10)
my_item my_item
InventoryItem(item_name='Banana', price=10.0)
= InventoryItem(item_name="Banana", price="100 dollar") invalid_item
--------------------------------------------------------------------------- ValidationError Traceback (most recent call last) Cell In[2], line 1 ----> 1 invalid_item = InventoryItem(item_name="Banana", price="100 dollar") File ~/.pyenv/versions/3.11.5/lib/python3.11/site-packages/pydantic/main.py:176, in BaseModel.__init__(self, **data) 174 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks 175 __tracebackhide__ = True --> 176 self.__pydantic_validator__.validate_python(data, self_instance=self) ValidationError: 1 validation error for InventoryItem price Input should be a valid number, unable to parse string as a number [type=float_parsing, input_value='100 dollar', input_type=str] For further information visit https://errors.pydantic.dev/2.7/v/float_parsing
from pydantic import BaseModel, EmailStr, PositiveInt
class Employee(BaseModel):
str
name:
email: EmailStr
age: PositiveInt= Employee(
invalid_employee ="Jeef",
name="fakeemail!!!",
email=50
age )
--------------------------------------------------------------------------- ValidationError Traceback (most recent call last) Cell In[3], line 6 4 email: EmailStr 5 age: PositiveInt ----> 6 invalid_employee = Employee( 7 name="Jeef", 8 email="fakeemail!!!", 9 age=50 10 ) File ~/.pyenv/versions/3.11.5/lib/python3.11/site-packages/pydantic/main.py:176, in BaseModel.__init__(self, **data) 174 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks 175 __tracebackhide__ = True --> 176 self.__pydantic_validator__.validate_python(data, self_instance=self) ValidationError: 1 validation error for Employee email value is not a valid email address: The email address is not valid. It must have exactly one @-sign. [type=value_error, input_value='fakeemail!!!', input_type=str]
= Employee(
invalid_employee2 ="Jeeferson",
name="valid@email.com",
email=-3
age )
--------------------------------------------------------------------------- ValidationError Traceback (most recent call last) Cell In[4], line 1 ----> 1 invalid_employee2 = Employee( 2 name="Jeeferson", 3 email="valid@email.com", 4 age=-3 5 ) File ~/.pyenv/versions/3.11.5/lib/python3.11/site-packages/pydantic/main.py:176, in BaseModel.__init__(self, **data) 174 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks 175 __tracebackhide__ = True --> 176 self.__pydantic_validator__.validate_python(data, self_instance=self) ValidationError: 1 validation error for Employee age Input should be greater than 0 [type=greater_than, input_value=-3, input_type=int] For further information visit https://errors.pydantic.dev/2.7/v/greater_than
Team
objectInventoryItem
object for each row)LogarithmicHashTable
key
and value
separately, or together?tuple
s or InventoryItem
s?InventoryItem
s: What type(s) for item_name
? What type(s) for price
? 😵txt
, epub
(can contain images), pdf
(sometimes text is embedded, sometimes not), images of scans, audiobooks