Skip to content

Commit 32672b6

Browse files
committed
Pudantic
1 parent 75957e6 commit 32672b6

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎Pydantic_tutorial/lecture_1.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pydantic import BaseModel
2+
3+
class User(BaseModel):
4+
username : str
5+
email : str
6+
age : int
7+
8+
user1 = User(username="Ashwini", email= "ashwini@gmail.com", age= 26)
9+
print(user1)
10+
11+
# user2 = User(username="badal", email=None, age=25)
12+
# print(user2)
13+
# Error for email
14+

‎Pydantic_tutorial/lecture_2.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from pydantic import BaseModel, ValidationError
2+
from datetime import datetime
3+
4+
class User(BaseModel):
5+
uid : int
6+
username : str
7+
email : str
8+
9+
verified_at : datetime | None = None
10+
11+
bio : str = ""
12+
is_active : bool = True
13+
14+
full_name : str | None = None
15+
16+
user = User(
17+
uid = 1,
18+
username="Ashwini",
19+
email = "ashwini@gmail.com"
20+
)
21+
22+
# print(user)
23+
24+
print(user.username)
25+
26+
# user.bio = 123
27+
# This will work even if 123 is not a string
28+
# print(user.bio)
29+
30+
user.bio = "Python dev"
31+
32+
print(user.model_dump_json(indent=2))

‎Pydantic_tutorial/lecture_3.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pydantic import BaseModel, ValidationError
2+
from datetime import datetime
3+
4+
class User(BaseModel):
5+
uid : int
6+
username : str
7+
email : str
8+
9+
verified_at : datetime | None = None
10+
11+
bio : str = ""
12+
is_active : bool = True
13+
14+
full_name : str | None = None
15+
16+
try:
17+
user = User(
18+
uid = 1,
19+
username=None,
20+
email = 123,
21+
)
22+
23+
# print(user)
24+
except ValidationError as e:
25+
print(e)
26+
27+
28+
# user.bio = "Python dev"
29+
# error because user object not created because of 2 validation error in try block
30+
# print(user.model_dump_json(indent=2))

‎Pydantic_tutorial/lecture_4.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from pydantic import BaseModel, ValidationError, Field
2+
from datetime import datetime, UTC
3+
from functools import partial
4+
from typing import Literal
5+
6+
class User(BaseModel):
7+
uid : int
8+
username : str
9+
email : str
10+
11+
verified_at : datetime | None = None
12+
13+
bio : str = ""
14+
is_active : bool = True
15+
16+
full_name : str | None = None
17+
18+
class BlogPost(BaseModel):
19+
title : str
20+
content : str
21+
view_count : int = 0
22+
is_published : bool = False
23+
24+
tags : list[str] = Field(default_factory=list)
25+
26+
# create_at : datetime = Field(default_factory=lambda : datetime.now(tz=UTC))
27+
# both will do same work lambda or partial function
28+
create_at : datetime = Field(default_factory=partial(datetime.now,tz=UTC))
29+
30+
author_id : str | int
31+
status : Literal["draft", "published", "archived"] = "draft"
32+
33+
post = BlogPost(
34+
title="Getting started with Python",
35+
content="python is dynamically typing language.",
36+
author_id="123",
37+
)
38+
39+
print(post)

0 commit comments

Comments
 (0)