Skip to content

Instantly share code, notes, and snippets.

@jeromegit
Last active May 12, 2024 04:17
Show Gist options
  • Save jeromegit/c053e2688299cd510583e117295b355b to your computer and use it in GitHub Desktop.
Save jeromegit/c053e2688299cd510583e117295b355b to your computer and use it in GitHub Desktop.
@dataclass_cheatsheet
# Since I keep on forgetting how to create Python @dataclass with static field and how to reference the class within the class....
# Example:
from dataclasses import dataclass
from typing import Dict, ClassVar
@dataclass
class NotionPage:
page_id: str
last_edited_time: str
url: str
title: str
pages_by_key: ClassVar[Dict[str, 'NotionPage']] = {}
def __str__(self):
return f"title:{self.title} | last_edited:{self.last_edited_time} | url:{self.url}"
def record_page_by_key(self) -> bool:
page_key = self.title + ' ' + self.last_edited_time
if page_key in NotionPage.pages_by_key:
return False
else:
NotionPage.pages_by_key[page_key] = self
return True
@staticmethod
def instantiate_page_from_page_info(page_info):
page_id = page_info['id']
last_edited_time = page_info['last_edited_time']
url = page_info['url']
titles = page_info['properties']['Name']['title']
if len(titles) > 1:
print(f'ERROR: {url} has more than one title')
title = page_info['properties']['Name']['title'][0]['plain_text']
return NotionPage(page_id, last_edited_time, url, title)
@jeromegit
Copy link
Author

Since I keep on forgetting how to create Python @DataClass with static field and how to reference the class within the class....

@jeromegit
Copy link
Author

.

@jeromegit
Copy link
Author

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment