Created
March 3, 2024 07:40
-
-
Save neelabalan/90916273c67260f0cdc9ab6ac5452415 to your computer and use it in GitHub Desktop.
From pybites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from dataclasses import dataclass, field | |
MAX_CONTENT_LENGTH = 280 | |
@dataclass | |
class XPost: | |
content: str | |
author: str | |
timestamp: str | None = field(default=None) | |
hashtags: list[str] = field(default_factory=list) | |
def __post_init__(self): | |
if len(self.content) > MAX_CONTENT_LENGTH: | |
raise ValueError("Content too long") | |
if self.timestamp is None: | |
self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
self.hashtags = [f"#{tag.lstrip('#')}" for tag in self.hashtags] | |
post = XPost(content="Python dataclasses are so nice!", | |
author="bbelderbos", | |
hashtags=["python", "coding", "#tips"]) | |
print(post) | |
# XPost(content='Python dataclasses are so nice!', author='bbelderbos', | |
# timestamp='2024-02-23 14:53:22', hashtags=['#python', '#coding', '#tips']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment