Last active
May 12, 2024 04:17
-
-
Save jeromegit/c053e2688299cd510583e117295b355b to your computer and use it in GitHub Desktop.
@dataclass_cheatsheet
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
# 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) |
.
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since I keep on forgetting how to create Python @DataClass with static field and how to reference the class within the class....