Ejemplo n.º 1
0
 def from_obj(cls: Type[V], obj: ObjType) -> V:
     if not isinstance(obj, dict):
         raise ObjParseException(f"obj '{obj}' is not a dict")
     fields = cls.fields()
     for k in obj.keys():
         if k not in fields:
             raise ObjParseException(f"obj '{obj}' has unknown key {k}")
     return cls(**{k: fields[k].from_obj(v) for k, v in obj.items()})
Ejemplo n.º 2
0
 def from_obj(cls: Type[V], obj: ObjType) -> V:
     if not isinstance(obj, (int, str)):
         raise ObjParseException(f"obj '{obj}' is not an int or str")
     if isinstance(obj, str):
         if obj.startswith('0x'):
             return cls.decode_bytes(bytes.fromhex(obj[2:]))
         obj = int(obj)
     return cls(obj)
Ejemplo n.º 3
0
 def from_obj(cls: Type[V], obj: ObjType) -> V:
     if not isinstance(obj, (list, tuple, str)):
         raise ObjParseException(f"obj '{obj}' is not a list, tuple or str")
     if isinstance(obj, str):
         if obj.startswith('0x'):
             return cls.decode_bytes(bytes.fromhex(obj[2:]))
         obj = [c == '1' for c in obj]
     return cls(obj)
Ejemplo n.º 4
0
 def from_obj(cls: Type[V], obj: ObjType) -> V:
     if not isinstance(obj, (list, tuple, str, bytes)):
         raise ObjParseException(
             f"obj '{obj}' is not a list, tuple, str or bytes")
     return cls(obj)
Ejemplo n.º 5
0
 def from_obj(cls: Type[V], obj: ObjType) -> V:
     if not isinstance(obj, bool):
         raise ObjParseException(f"obj '{obj}' is not a bool")
     return cls(obj)
Ejemplo n.º 6
0
 def from_obj(cls: Type[M], obj: ObjType) -> M:
     if not isinstance(obj, (list, tuple)):
         raise ObjParseException(f"obj '{obj}' is not a list or tuple")
     elem_cls = cls.element_cls()
     return cls(elem_cls.from_obj(el) for el in obj)  # type: ignore