koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame^] | 1 | import abc |
| 2 | from typing import Any, Union, List, Dict |
| 3 | |
| 4 | |
| 5 | class IStorable(metaclass=abc.ABCMeta): |
| 6 | """Interface for type, which can be stored""" |
| 7 | |
| 8 | @abc.abstractmethod |
| 9 | def raw(self) -> Dict[str, Any]: |
| 10 | pass |
| 11 | |
| 12 | @abc.abstractclassmethod |
| 13 | def fromraw(cls, data: Dict[str, Any]) -> 'IStorable': |
| 14 | pass |
| 15 | |
| 16 | |
| 17 | class Storable(IStorable): |
| 18 | """Default implementation""" |
| 19 | |
| 20 | def raw(self) -> Dict[str, Any]: |
| 21 | return self.__dict__ |
| 22 | |
| 23 | @classmethod |
| 24 | def fromraw(cls, data: Dict[str, Any]) -> 'IStorable': |
| 25 | obj = cls.__new__(cls) |
| 26 | obj.__dict__.update(data) |
| 27 | return obj |
| 28 | |
| 29 | |
| 30 | Basic = Union[int, str, bytes, bool, None] |
| 31 | StorableType = Union[IStorable, Dict[str, Any], List[Any], int, str, bytes, bool, None] |