efficiency bugfix in bytes encoding, improve typing doc + bugfix

This commit is contained in:
protolambda 2019-05-27 21:45:47 +02:00
parent 132d3c976a
commit d63b553a2d
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
2 changed files with 11 additions and 3 deletions

View File

@ -14,7 +14,7 @@ def is_basic_type(typ):
def serialize_basic(value, typ):
if is_uint(typ):
return value.to_bytes(uint_byte_size(typ), 'little')
if issubclass(typ, bool):
if is_bool_type(typ):
if value:
return b'\x01'
else:
@ -39,7 +39,7 @@ def serialize(obj, typ):
if is_basic_type(typ):
return serialize_basic(obj, typ)
elif is_list_type(typ) or is_vector_type(typ):
return encode_series(list(obj), [read_elem_typ(typ)]*len(obj))
return encode_series(obj, [read_elem_typ(typ)]*len(obj))
elif is_container_typ(typ):
return encode_series(obj.get_field_values(), typ.get_field_types())
else:

View File

@ -376,12 +376,20 @@ def infer_input_type(fn):
return fn(obj, typ)
return infer_helper
def is_bool_type(typ):
return issubclass(typ, bool)
def is_list_type(typ):
"""
Checks if the given type is a kind of list. Can be bytes.
"""
return (hasattr(typ, '_name') and typ._name == 'List') or typ == bytes
def is_vector_type(typ):
return issubclass(typ, Vector)
"""
Checks if the given type is a kind of vector. Can be BytesN.
"""
return issubclass(typ, Vector) or issubclass(typ, BytesN)
def is_container_typ(typ):
return issubclass(typ, Container)