Skip to content

Commit cf7cda4

Browse files
author
hfhoffman1144
committed
tmp
1 parent cff57fb commit cf7cda4

File tree

8 files changed

+106
-10
lines changed

8 files changed

+106
-10
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

python-pydantic/ideation.ipynb

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 25,
12+
"execution_count": 13,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
1616
"from pydantic import BaseModel, EmailStr\n",
17-
"from datetime import date\n",
17+
"from datetime import date, timedelta\n",
18+
"from dataclasses import dataclass\n",
1819
"from uuid import UUID, uuid4"
1920
]
2021
},
@@ -271,18 +272,53 @@
271272
},
272273
{
273274
"cell_type": "code",
274-
"execution_count": null,
275+
"execution_count": 15,
275276
"metadata": {},
276-
"outputs": [],
277-
"source": []
277+
"outputs": [
278+
{
279+
"ename": "TypeError",
280+
"evalue": "'year' is an invalid keyword argument for __new__()",
281+
"output_type": "error",
282+
"traceback": [
283+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
284+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
285+
"Cell \u001b[0;32mIn[15], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtimedelta\u001b[49m\u001b[43m(\u001b[49m\u001b[43myear\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m18\u001b[39;49m\u001b[43m)\u001b[49m\n",
286+
"\u001b[0;31mTypeError\u001b[0m: 'year' is an invalid keyword argument for __new__()"
287+
]
288+
}
289+
],
290+
"source": [
291+
"timedelta(year=18)"
292+
]
293+
},
294+
{
295+
"cell_type": "code",
296+
"execution_count": 16,
297+
"metadata": {},
298+
"outputs": [
299+
{
300+
"data": {
301+
"text/plain": [
302+
"1.063013698630137"
303+
]
304+
},
305+
"execution_count": 16,
306+
"metadata": {},
307+
"output_type": "execute_result"
308+
}
309+
],
310+
"source": [
311+
"date_delta = date.today() - date(2023, 2, 1)\n",
312+
"date_delta.days / 365"
313+
]
278314
},
279315
{
280316
"cell_type": "code",
281317
"execution_count": 65,
282318
"metadata": {},
283319
"outputs": [],
284320
"source": [
285-
"from pydantic import PositiveFloat, validate_call"
321+
"from pydantic import PositiveFloat, validate_call, Field"
286322
]
287323
},
288324
{
@@ -292,7 +328,7 @@
292328
"outputs": [],
293329
"source": [
294330
"@validate_call\n",
295-
"def send_billing_email(client_name:str,\n",
331+
"def create_billing_email(client_name:str = Field(min_length=1),\n",
296332
" client_email:EmailStr,\n",
297333
" items_purchased:list[str],\n",
298334
" amount_owed:PositiveFloat) -> None:\n",
@@ -306,9 +342,7 @@
306342
" {items_purchased}\n",
307343
" \"\"\"\n",
308344
" \n",
309-
" print(f\"Sending to {client_email}...\")\n",
310-
" print(email_str)\n",
311-
" print(\"Sent!\")\n",
345+
" return email_str\n",
312346
" "
313347
]
314348
},

python-pydantic/pydantic_models.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import Self
2+
from datetime import date
3+
from uuid import UUID, uuid4
4+
from enum import Enum
5+
from pydantic import (
6+
BaseModel,
7+
EmailStr,
8+
Field,
9+
field_validator,
10+
model_validator,
11+
)
12+
13+
14+
class Department(Enum):
15+
HR = "HR"
16+
SALES = "SALES"
17+
IT = "IT"
18+
ENGINEERING = "ENGINEERING"
19+
20+
21+
class Employee(BaseModel):
22+
employee_id: UUID = Field(default_factory=lambda: uuid4(), frozen=True)
23+
name: str = Field(min_length=1, frozen=True)
24+
email: EmailStr = Field(pattern=r".+@company\.com$")
25+
date_of_birth: date = Field(alias="birth_date", repr=False, frozen=True)
26+
salary: float = Field(alias="compensation", gt=0, repr=False)
27+
department: Department
28+
elected_benefits: bool
29+
30+
@field_validator("date_of_birth")
31+
@classmethod
32+
def check_valid_age(cls, date_of_birth: date) -> date:
33+
34+
date_delta = date.today() - date_of_birth
35+
age = date_delta.days / 365
36+
37+
if age < 18:
38+
raise ValueError("Employees must be at least 18 years old.")
39+
40+
return date_of_birth
41+
42+
@model_validator(mode="after")
43+
def check_it_benefits(self) -> Self:
44+
45+
department = self.department
46+
elected_benefits = self.elected_benefits
47+
48+
if department == Department.IT and elected_benefits:
49+
raise ValueError(
50+
"IT employees are contractors and don't qualify for benefits."
51+
)
52+
return self
53+
54+
55+
new_employee = {
56+
"name": "Chris DeTuma",
57+
"email": "[email protected]",
58+
"birth_date": "1998-04-02",
59+
"compensation": 100_000,
60+
"department": "IT",
61+
"elected_benefits": True,
62+
}

python-pydantic/validate_functions.py

Whitespace-only changes.

0 commit comments

Comments
 (0)