def serialize(self, obj): if not is_bool(obj): raise SerializationError('serialize only boolean, {}'.format(type(obj).__name__)) if obj is True: return b'\x01' elif obj is False: return b'\x00' else: raise SerializationError('serialize only boolean, {}'.format(obj))
def serialize(self, obj): if not is_integer(obj): raise SerializationError('serialize only integer, {}'.format( type(obj).__name__)) if obj > 0: data = int_to_big(obj) elif obj == 0: data = b'\x00' else: raise SerializationError( 'Cannot serialize, deny of integer, {}'.format(obj)) return self.has_size(data)
def serialize(self, obj): if len(self) != len(obj): raise SerializationError('object length does not matched, ' 'Serialize({}), Object({})'.format( len(self), len(obj))) for value, props in zip(obj, self): yield props.serialize(value)
def serialize(self, obj): is_valid = any((self.size and len(obj) == 0, len(obj) == self.size)) if not is_valid: raise SerializationError( 'invalid elements, expected size {}, got {}, {}'.format( self.size, len(obj), obj)) for element in obj: yield self.props.serialize(element)
def has_size(self, data): size = len(data) is_valid = any((self.minsize <= size <= self.maxsize, self.allowed and size == 0)) if not is_valid: raise SerializationError( 'serialize invalid size, expected size {}, got {}'.format( self.minsize, len(data))) return data
def __init__(self, *args, **kwargs): if kwargs: data_space = self._merge(args, kwargs) else: data_space = args if len(self._meta.names) != len(data_space): raise SerializationError("Expected args({}), " "Got args({})".format( len(self.__slots__), len(data_space))) for attr, value in zip(self.__slots__, data_space): setattr(self, attr, value)
def serialize(self, obj): if not is_str(obj): raise SerializationError('serialize only string, {}'.format( type(obj).__name__)) return self.has_size(obj).encode(self.encoding)
def serialize(self, obj): if not is_bytes(obj): raise SerializationError('serialize only bytes-string, {}'.format(type(obj).__name__)) return self.has_size(obj)