Implemented data models for post
Browse files- postly/common/__init__.py +0 -0
- postly/common/models.py +24 -0
postly/common/__init__.py
ADDED
|
File without changes
|
postly/common/models.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class StrictPost(BaseModel):
|
| 7 |
+
content: str
|
| 8 |
+
timestamp: int
|
| 9 |
+
topics: List[str]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class Post:
|
| 14 |
+
content: str
|
| 15 |
+
timestamp: int
|
| 16 |
+
topics: List[str]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
if __name__ == "__main__":
|
| 20 |
+
# this should be OK, as not strictly typed
|
| 21 |
+
Post(content=1, timestamp=1, topics=["1"])
|
| 22 |
+
|
| 23 |
+
# this should result in a validation error, as pydantic enforces strict typing on runtime
|
| 24 |
+
StrictPost(content=1, timestamp=1, topics=["1"])
|