Last active
September 5, 2019 19:59
-
-
Save FBosler/a52ff26def1056070e4407401fc8537e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import uuid | |
import json | |
class Base(object): | |
def __init__(self,**kwargs): | |
self._id = kwargs.get('_id') or uuid.uuid4().hex[:5] | |
@classmethod | |
def _instanciate_from_db(cls,_id): | |
res = mock_fetch_from_db(table=cls._table,_id=_id) | |
return cls(**res) | |
def _upload_to_db(self): | |
dictified = self._to_dict() | |
dictified['_id'] = self._id | |
mock_upsert_to_db(table=self._table,to_upload=dictified) | |
def _to_dict(self): | |
"Return a dictionary of all the properties of the class that don't start with an _ " | |
return {k:getattr(self,k) for k in [x for x in dir(self) if not x.startswith('_')]} | |
def __repr__(self): | |
return json.dumps(self._to_dict()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment