def validate_value(cls, value): if not is_text(value): raise EncodingTypeError( "Value of type {} cannot be encoded by {}".format( type(value), cls.__name__, ))
def validate_value(self, value): cls = type(self) if not self.type_check_fn(value): raise EncodingTypeError( "Value of type {0} cannot be encoded by {1}".format( type(value), cls.__name__, ) ) illegal_value = ( self.illegal_value_fn is not None and self.illegal_value_fn(value) ) if illegal_value: raise IllegalValue( 'Value {} cannot be encoded by {}'.format(repr(value), cls.__name__) ) lower_bound, upper_bound = self.bounds_fn(self.value_bit_size) if value < lower_bound or value > upper_bound: raise ValueOutOfBounds( "Value {0} cannot be encoded in {1} bits. Must be bounded " "between [{2}, {3}]".format( repr(value), self.value_bit_size, lower_bound, upper_bound, ) )
def encode(cls, value): if not is_text(value): raise EncodingTypeError( "Value of type {0} cannot be encoded by TextStringEncoder". format(type(value), )) value_as_bytes = codecs.encode(value, 'utf8') return super().encode(value_as_bytes)
def validate_value(cls, value): if not is_address(value): raise EncodingTypeError( "Cannot encode value {0} of type {1} using {2}".format( repr(value), type(value), cls.__name__, ))
def encode_elements(cls, value): if not is_list_like(value): raise EncodingTypeError( "Cannot encode value of type {0} using array encoder. Must be " "a list-like object such as an array or tuple".format( type(value), )) encoded_elements = b''.join((cls.item_encoder(item) for item in value)) return encoded_elements
def validate_value(self, value): if not is_list_like(value): raise EncodingTypeError( "Cannot encode value of type {0} using array encoder. Must be " "a list-like object such as an array or tuple".format( type(value), )) for item in value: self.item_encoder.validate_value(item)
def encode(cls, value): if not is_text(value): raise EncodingTypeError( "Value of type {} cannot be encoded by {}".format( type(value), cls.__name__, )) value_as_bytes = codecs.encode(value, 'utf8') return super().encode(value_as_bytes)
def validate_value(self, value): if not is_list_like(value): raise EncodingTypeError( "Cannot encode value of type {0} using tuple encoder. Must be " "a list-like object such as an array or tuple".format( type(value), )) if len(value) != len(self.encoders): raise ValueOutOfBounds( "Expected value with length {0}. Provided value has {1} " "elements".format(len(self.encoders), len(value))) for item, encoder in zip(value, self.encoders): encoder.validate_value(item)
def validate_value(self, value): if not is_bytes(value): raise EncodingTypeError( "Value of type {0} cannot be encoded by {1}".format( type(value), type(self).__name__, )) if len(value) > self.value_bit_size // 8: raise ValueOutOfBounds( "String {0} exceeds total byte size for bytes{1} encoding". format( value, self.value_bit_size // 8, ))
def encode(cls, value): if not is_bytes(value): raise EncodingTypeError( "Value of type {0} cannot be encoded by ByteStringEncoder". format(type(value), )) encoded_size = encode_uint_256(len(value)) if not value: padded_value = b'\x00' * 32 else: padded_value = zpad_right(value, ceil32(len(value))) encoded_value = encoded_size + padded_value return encoded_value
def validate_value(cls, value): if not cls.type_check_fn(value): raise EncodingTypeError( "Value of type {0} cannot be encoded by {1}".format( type(value), cls.__name__, )) lower_bound, upper_bound = cls.bounds_fn(cls.value_bit_size) if value < lower_bound or value > upper_bound: raise ValueOutOfBounds( "Value '{0}' cannot be encoded in {1} bits. Must be bounded " "between [{2}, {3}]".format( value, cls.value_bit_size, lower_bound, upper_bound, ))