Skip to content

Instantly share code, notes, and snippets.

@Ananto30
Last active March 24, 2020 15:42
Show Gist options
  • Save Ananto30/0c1e7f6c67d21046783042bc69ffbe25 to your computer and use it in GitHub Desktop.
Save Ananto30/0c1e7f6c67d21046783042bc69ffbe25 to your computer and use it in GitHub Desktop.
import msgpack
class Btypes:
def pack(self):
return msgpack.packb(Btypes.get_all_vars(self))
@classmethod
def unpack(cls, d):
return cls(**msgpack.unpackb(d, raw=False))
@staticmethod
def get_all_vars(obj):
values = vars(obj)
for k, v in values.items():
if isinstance(v, Btypes):
values[k] = vars(v)
return values
class Etypes(Btypes):
def __init__(self, operation: str, request, response=None):
self.operation = operation
self.request = request
self.response = response
class TypeCheckFailedException(Exception):
pass
def type_check(o, t):
if not isinstance(o, t):
raise TypeCheckFailedException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment