Skip to content

Commit 571e723

Browse files
update file
1 parent cbaa470 commit 571e723

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

fastapi_intro/.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
1+
# .gitignore for Python projects
2+
.venv/
3+
/__pycache__/
4+
*.pyc
5+
uv.lock
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from pydantic import BaseModel
2+
3+
4+
class Address(BaseModel):
5+
"""
6+
Represents an address with street, city, state, and postal code.
7+
"""
8+
street: str
9+
city: str
10+
state: str
11+
postal_code: str
12+
13+
14+
class Patient(BaseModel):
15+
"""
16+
Represents a patient with personal and medical information.
17+
"""
18+
name: str
19+
gender: str
20+
age: int
21+
address: Address
22+
23+
24+
def print_patient_details(patient: Patient) -> None:
25+
"""
26+
Prints the details of a Patient instance, including nested address.
27+
"""
28+
print(f"Name: {patient.name}")
29+
print(f"Gender: {patient.gender}")
30+
print(f"Age: {patient.age}")
31+
print("Address:")
32+
print(f" Street: {patient.address.street}")
33+
print(f" City: {patient.address.city}")
34+
print(f" State: {patient.address.state}")
35+
print(f" Postal Code: {patient.address.postal_code}")
36+
37+
38+
# Example usage: Creating Address and Patient instances from dictionaries
39+
address_data = {
40+
"street": "123 Main St",
41+
"city": "Springfield",
42+
"state": "IL",
43+
"postal_code": "62701"
44+
}
45+
46+
patient_data = {
47+
"name": "John Doe",
48+
"gender": "male",
49+
"age": 30,
50+
"address": address_data
51+
}
52+
53+
patient1 = Patient(**patient_data)
54+
55+
# Print all values of the patient using the print_patient_details function
56+
print_patient_details(patient1)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Import BaseModel from Pydantic for data validation and serialization
2+
from pydantic import BaseModel
3+
4+
5+
# Define Address model for nested serialization
6+
class Address(BaseModel):
7+
"""
8+
Represents an address with street, city, state, and postal code.
9+
"""
10+
street: str
11+
city: str
12+
state: str
13+
postal_code: str
14+
15+
16+
# Define Patient model with nested Address
17+
class Patient(BaseModel):
18+
"""
19+
Represents a patient with personal and medical information.
20+
"""
21+
name: str
22+
gender: str
23+
age: int
24+
address: Address
25+
26+
27+
# Function to print all details of a Patient instance, including nested address
28+
def print_patient_details(patient: Patient) -> None:
29+
"""
30+
Prints the details of a Patient instance, including nested address.
31+
"""
32+
print(f"Name: {patient.name}")
33+
print(f"Gender: {patient.gender}")
34+
print(f"Age: {patient.age}")
35+
print("Address:")
36+
print(f" Street: {patient.address.street}")
37+
print(f" City: {patient.address.city}")
38+
print(f" State: {patient.address.state}")
39+
print(f" Postal Code: {patient.address.postal_code}")
40+
41+
42+
# Example usage: Creating Address and Patient instances from dictionaries
43+
address_data = {
44+
"street": "123 Main St",
45+
"city": "Springfield",
46+
"state": "IL",
47+
"postal_code": "62701"
48+
}
49+
50+
patient_data = {
51+
"name": "John Doe",
52+
"gender": "male",
53+
"age": 30,
54+
"address": address_data
55+
}
56+
57+
# Create Patient instance from dictionary
58+
patient1 = Patient(**patient_data)
59+
60+
# Demonstrate Pydantic serialization methods
61+
# model_dump returns a dict representation of the model
62+
# model_dump_json returns a JSON string, with options to include/exclude fields
63+
# exclude_unset only includes fields that were set
64+
temp = patient1.model_dump # Reference to the model_dump method (not called)
65+
temp2 = patient1.model_dump_json(exclude=["name", "age"]) # JSON without name and age
66+
temp3 = patient1.model_dump_json(include=["name", "age"]) # JSON with only name and age
67+
temp4 = patient1.model_dump_json(exclude_unset=True) # JSON with only set fields
68+
69+
# Print the various serialization outputs
70+
print(temp2)
71+
print(temp3)
72+
print(temp4)
73+
print(temp) # This will print the method object, not the data
74+
print(type(temp))
75+
print(type(temp2))

0 commit comments

Comments
 (0)