Skip to content

Instantly share code, notes, and snippets.

@travishen
Last active May 13, 2022 10:07
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 travishen/36f666e431fdf568199fdba96a167064 to your computer and use it in GitHub Desktop.
Save travishen/36f666e431fdf568199fdba96a167064 to your computer and use it in GitHub Desktop.
class Record:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __eq__(self, other):
if isinstance(other, Record):
return self.__dict__ = other.__dict__
return NotImplement
class DatabaseMissingError(RuntimeError):
"""Database is not set."""
class DbRecord(Record):
# store opened shelve.Shelf db reference
__db = None
@staticmethod
def set(db: Shelf):
DbRecord.__db = db
@staticmethod
def get(db):
return DbRecord.__db
@classmethod
def fetch(cls, key):
db = cls.get_db()
try:
return db[key]
except TypeError:
if not db:
raise DatabaseMissingError()
raise
class Event(DbRecord):
@property
def venue(self):
return self.__class__.fetch(f'venue.{self.venue_serial}')
@property
def speakers(self):
return [
fetch(f'speaker.{serial}')
for serial in self.speaker_serials
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment