Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

0.pydantic.py 579 B

You have to be logged in to leave a comment. Sign In
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
  1. from datetime import datetime
  2. from pydantic import BaseModel
  3. class User(BaseModel):
  4. id: int #required
  5. name = 'John Doe' # defaul a value
  6. signup_ts: datetime | None = None # optional return None
  7. friends: list[int] = [] # default empty list
  8. external_data_1 = {
  9. 'id': '123',
  10. 'signup_ts': '2019-06-01 12:22',
  11. 'friends': [1, 2, '3'],
  12. }
  13. user = User(**external_data_1)
  14. # change type to int
  15. print(user.id)
  16. # convert string to datetime
  17. print(repr(user.signup_ts))
  18. print(user.signup_ts)
  19. #convert vlalue in list to int
  20. print(user.friends)
  21. print(user.dict())
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...