-
Notifications
You must be signed in to change notification settings - Fork 0
/
things.py
46 lines (38 loc) · 1.29 KB
/
things.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import abc
import hashlib
import json
import config
class Thing(abc.ABC):
_id: str
@property
def id(self):
if not self._id:
j = json.dumps(self.serialize())
self._id = hashlib.sha256(j.encode("utf-8")).hexdigest()[: config.ID_LENGTH]
return self._id
@abc.abstractmethod
def serialize(self) -> dict:
"""
Serialize this Thing as a dictionary. Should be suitable for JSON dumping. Must not contain this Thing's ID.
Update your parent class' serialize method as opposed to overloading to avoid Boilerplate(tm).
:return: This Thing, serialized to a JSON-suitable dictionary.
"""
...
@staticmethod
@abc.abstractmethod
def deserialize(serialized: dict, original_id: str):
"""
Reconstitute a Thing from its dictionary form and ID calculated at creation.
:param original_id: The Thing's ID, as calculated prior to any editing
:param serialized: The serialized Thing
:return: A deserialized Thing
"""
...
@staticmethod
@abc.abstractmethod
async def retrieve(id_: str) -> "Thing":
"""
Retrieve and return an instance of a Thing from storage by id.
:param id_: The ID of the Thing to retrieve
"""
...