-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture_5.py
More file actions
53 lines (40 loc) · 1.38 KB
/
lecture_5.py
File metadata and controls
53 lines (40 loc) · 1.38 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
from pydantic import BaseModel, ValidationError, Field
from datetime import datetime, UTC
from functools import partial
from typing import Literal,Annotated
class User(BaseModel):
uid : Annotated[int, Field(gt=0)]
username : Annotated[str, Field(min_length=3, max_length=20)]
email : str
age : Annotated[int, Field(ge=13,le=130)]
verified_at : datetime | None = None
bio : str = ""
is_active : bool = True
full_name : str | None = None
class BlogPost(BaseModel):
title : Annotated[str, Field(min_length=1,max_length=200)]
content : Annotated[str, Field(min_length=20)]
view_count : int = 0
is_published : bool = False
tags : list[str] = Field(default_factory=list)
# create_at : datetime = Field(default_factory=lambda : datetime.now(tz=UTC))
# both will do same work lambda or partial function
create_at : datetime = Field(default_factory=partial(datetime.now,tz=UTC))
author_id : str | int
status : Literal["draft", "published", "archived"] = "draft"
slug : Annotated[str, Field(pattern=r"^[a-z0-9-]+$")]
try:
user = User(
uid = 0,
username="cs",
email="[email protected]",
age=12
)
except ValidationError as e:
print(e)
# post = BlogPost(
# title="Getting started with Python",
# content="python is dynamically typing language.",
# author_id="123",
# )
# print(post)