예제 #1
0
    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))
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
파일: list.py 프로젝트: endlessgate/bitLabs
    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)
예제 #5
0
    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
예제 #6
0
    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)
예제 #7
0
 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)
예제 #8
0
 def serialize(self, obj):
     if not is_bytes(obj):
         raise SerializationError('serialize only bytes-string, {}'.format(type(obj).__name__))
     return self.has_size(obj)