Skip to content

Instantly share code, notes, and snippets.

@neelabalan
Created March 3, 2024 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neelabalan/90916273c67260f0cdc9ab6ac5452415 to your computer and use it in GitHub Desktop.
Save neelabalan/90916273c67260f0cdc9ab6ac5452415 to your computer and use it in GitHub Desktop.
From pybites
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