marmistrz

Untitled

Aug 25th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. S = TypeVar('S', bound='DictSerializable')
  2.  
  3.  
  4. class DictSerializable(metaclass=ABCMeta):
  5. @abstractmethod
  6. def to_dict(self) -> dict:
  7. "Converts the object to a dict containing only primitive types"
  8.  
  9. @staticmethod
  10. @abstractmethod
  11. def from_dict(data: str) -> S:
  12. "Converts the object to a dict containing only primitive types"
  13.  
  14.  
  15.  
  16.  
  17. class PaymentDetails(DictSerializable):
  18. def __init__(self,
  19. node_info: Optional[Node] = None,
  20. fee: Optional[int] = None,
  21. block_hash: Optional[str] = None,
  22. block_number: Optional[int] = None,
  23. check: Optional[bool] = None,
  24. tx: Optional[str] = None) -> None:
  25. self.node_info = node_info
  26. self.fee = fee
  27. self.block_hash = block_hash
  28. self.block_number = block_number
  29. self.check = check
  30. self.tx = tx
  31.  
  32. def to_dict(self) -> dict:
  33. d = self.__dict__
  34. if self.node_info:
  35. d['node_info'] = self.node_info.to_dict()
  36. return d
  37.  
  38. @staticmethod
  39. def from_dict(data: dict) -> 'PaymentDetails':
  40. det = PaymentDetails()
  41. det.__dict__.update(data)
  42. det.__dict__['node_info'] = Node.from_dict(data['node_info'])
  43. return det
Advertisement
Add Comment
Please, Sign In to add comment