forked from balapriyac/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
229 lines (188 loc) · 5.36 KB
/
examples.py
File metadata and controls
229 lines (188 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# ! pip install pydantic
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
email: str
# Create a user
user = User(name="Alice", age="25", email="alice@example.com")
print(user.age)
print(type(user.age))
from pydantic import BaseModel, Field
from typing import Optional
class Product(BaseModel):
name: str
price: float
description: Optional[str] = None
in_stock: bool = True
category: str = Field(default="general", min_length=1)
# All these work
product1 = Product(name="Widget", price=9.99)
product2 = Product(name="Gadget", price=15.50, description="Useful tool")
from pydantic import BaseModel, field_validator
import re
class Account(BaseModel):
username: str
email: str
password: str
@field_validator('username')
def validate_username(cls, v):
if len(v) < 3:
raise ValueError('Username must be at least 3 characters')
if not v.isalnum():
raise ValueError('Username must be alphanumeric')
return v.lower() # Normalize to lowercase
@field_validator('email')
def validate_email(cls, v):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if not re.match(pattern, v):
raise ValueError('Invalid email format')
return v
@field_validator('password')
def validate_password(cls, v):
if len(v) < 8:
raise ValueError('Password must be at least 8 characters')
return v
account = Account(
username="JohnDoe123",
email="john@example.com",
password="secretpass123"
)
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime
class Address(BaseModel):
street: str
city: str
state: str
zip_code: str
@field_validator('zip_code')
def validate_zip(cls, v):
if not v.isdigit() or len(v) != 5:
raise ValueError('ZIP code must be 5 digits')
return v
class Contact(BaseModel):
name: str
phone: str
email: Optional[str] = None
class Company(BaseModel):
name: str
founded: datetime
address: Address
contacts: List[Contact]
employee_count: int
is_public: bool = False
# Complex nested data gets fully validated
company_data = {
"name": "Tech Corp",
"founded": "2020-01-15T10:00:00",
"address": {
"street": "123 Main St",
"city": "San Francisco",
"state": "CA",
"zip_code": "94105"
},
"contacts": [
{"name": "John Smith", "phone": "555-0123"},
{"name": "Jane Doe", "phone": "555-0456", "email": "jane@techcorp.com"}
],
"employee_count": 150
}
company = Company(**company_data)
from pydantic import BaseModel, Field, field_validator
from typing import Union, Optional
from datetime import datetime
import json
class APIResponse(BaseModel):
status: str
message: Optional[str] = None
data: Optional[dict] = None
timestamp: datetime = Field(default_factory=datetime.now)
class UserProfile(BaseModel):
id: int
username: str
full_name: Optional[str] = None
age: Optional[int] = Field(None, ge=0, le=150) # Age constraints
created_at: Union[datetime, str] # Handle multiple formats
is_verified: bool = False
@field_validator('created_at', mode='before')
def parse_created_at(cls, v):
if isinstance(v, str):
try:
return datetime.fromisoformat(v.replace('Z', '+00:00'))
except ValueError:
raise ValueError('Invalid datetime format')
return v
# Simulate API response
api_json = '''
{
"status": "success",
"data": {
"id": 123,
"username": "alice_dev",
"full_name": "Alice Johnson",
"age": "28",
"created_at": "2023-01-15T10:30:00Z",
"is_verified": true
}
}
'''
response_data = json.loads(api_json)
api_response = APIResponse(**response_data)
if api_response.data:
user = UserProfile(**api_response.data)
print(f"User {user.username} created at {user.created_at}")
from pydantic import BaseModel, ValidationError
from typing import List
class Order(BaseModel):
order_id: int
customer_email: str
items: List[str]
total: float
@field_validator('total')
def positive_total(cls, v):
if v <= 0:
raise ValueError('Total must be positive')
return v
# Invalid data
bad_data = {
"order_id": "not_a_number",
"customer_email": "invalid_email",
"items": "should_be_list",
"total": -10.50
}
try:
order = Order(**bad_data)
except ValidationError as e:
print("Validation errors:")
for error in e.errors():
field = error['loc'][0]
message = error['msg']
print(f" {field}: {message}")
# Get JSON representation of errors
print("\nJSON errors:")
print(e.json(indent=2))
from pydantic import BaseModel
from datetime import datetime
class Event(BaseModel):
name: str
date: datetime
attendees: int
is_public: bool = True
event = Event(
name="Python Meetup",
date=datetime(2024, 3, 15, 18, 30),
attendees=45
)
# Export to dictionary
event_dict = event.model_dump()
print(event_dict)
# Export to JSON string
event_json = event.model_dump_json()
print(event_json)
# Export with exclusions
public_data = event.model_dump(exclude={'attendees'})
print(public_data)
# Export with custom serialization
formatted_json = event.model_dump_json(indent=2)
print(formatted_json)