def to_json_serializable(self): amap = {} for name, atype in self._fields: value = getattr(self, name) atype = type_mapping(atype) amap[name] = Base.to_json_data(value_type=(value, atype)) return amap
def decode(cls, cursor): index = Uint32.parse_uint32_from_uleb128(cursor) _name, datatype = cls._enums[index] if datatype is not None: value = type_mapping(datatype).decode(cursor) return cls.new_with_index_value(index, value) else: return cls.new_with_index_value(index, None)
def decode(cls, cursor): ret = cls.__new__(cls) ret.__init__() for name, atype in ret._fields: prop = getattr(ret, name) mtype = type_mapping(atype) assert mtype == prop.expected_type value = mtype.decode(cursor) prop.__set__(ret, value) return ret
def to_json_serializable(self): amap = {} for name, atype in self._fields: value = getattr(self, name) if isinstance(value, TypedProperty): amap[name] = None else: atype = type_mapping(atype) amap[name] = atype.to_json_serializable(value) return amap
def pretty_print_obj(cls, obj, buffer, ident): #TODO obj should be instance of cls prefix_blank = ' ' #buffer.write(prefix_blank*ident) buffer.write('{\n') ident_inner = ident+1 for name, atype in obj._fields: value = getattr(obj, name) buffer.write(prefix_blank*ident_inner) cls.pretty_print_field(name, type_mapping(atype), value, buffer, ident_inner) buffer.write(',\n') buffer.write(prefix_blank*ident) buffer.write('}')
def encode(cls, obj): output = b'' for name, atype in obj._fields: value = getattr(obj, name) output += type_mapping(atype).encode(value) return output
def initailize_fields_type(cls): if not cls._initialized: cls._initialized = True for name, atype in cls._fields: setattr(cls, name, TypedProperty(name, type_mapping(atype)))
def _init_with_index_value(self, index, value, datatype): self._index = index self.value_type = type_mapping(datatype) self.value = value
def __init__(self, value=None): if not self.__class__._type: raise TypeError(f'{self.__class__} has no _type defined.') self.__dict__["value_type"] = type_mapping(self.__class__._type) self.value = value
def test_type_mapping(): atype = {Uint16: Uint64} mtype = type_mapping(atype) print(mtype)
def dtype(cls): return type_mapping(cls.delegate_type)