Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def dump(obj, fp, **kwargs):
"""
Serialize an object to a file.
:param obj: the object to serialize
:param fp: a file-like object
:param kwargs: keyword arguments passed to :class:`~.CBOREncoder`
"""
CBOREncoder(fp, **kwargs).encode(obj)
(dict, CBOREncoder.encode_map),
(defaultdict, CBOREncoder.encode_map),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(type(re.compile('')), CBOREncoder.encode_regexp),
(('fractions', 'Fraction'), CBOREncoder.encode_rational),
(('email.message', 'Message'), CBOREncoder.encode_mime),
(('uuid', 'UUID'), CBOREncoder.encode_uuid),
(('ipaddress', 'IPv4Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv6Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv4Network'), CBOREncoder.encode_ipnetwork),
(('ipaddress', 'IPv6Network'), CBOREncoder.encode_ipnetwork),
(CBORSimpleValue, CBOREncoder.encode_simple_value),
(CBORTag, CBOREncoder.encode_semantic),
(set, CBOREncoder.encode_set),
(frozenset, CBOREncoder.encode_set),
])
canonical_encoders = OrderedDict([
(float, CBOREncoder.encode_minimal_float),
(dict, CBOREncoder.encode_canonical_map),
(defaultdict, CBOREncoder.encode_canonical_map),
(OrderedDict, CBOREncoder.encode_canonical_map),
(FrozenDict, CBOREncoder.encode_canonical_map),
(set, CBOREncoder.encode_canonical_set),
(frozenset, CBOREncoder.encode_canonical_set),
])
(('fractions', 'Fraction'), CBOREncoder.encode_rational),
(('email.message', 'Message'), CBOREncoder.encode_mime),
(('uuid', 'UUID'), CBOREncoder.encode_uuid),
(('ipaddress', 'IPv4Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv6Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv4Network'), CBOREncoder.encode_ipnetwork),
(('ipaddress', 'IPv6Network'), CBOREncoder.encode_ipnetwork),
(CBORSimpleValue, CBOREncoder.encode_simple_value),
(CBORTag, CBOREncoder.encode_semantic),
(set, CBOREncoder.encode_set),
(frozenset, CBOREncoder.encode_set),
])
canonical_encoders = OrderedDict([
(float, CBOREncoder.encode_minimal_float),
(dict, CBOREncoder.encode_canonical_map),
(defaultdict, CBOREncoder.encode_canonical_map),
(OrderedDict, CBOREncoder.encode_canonical_map),
(FrozenDict, CBOREncoder.encode_canonical_map),
(set, CBOREncoder.encode_canonical_set),
(frozenset, CBOREncoder.encode_canonical_set),
])
def dumps(obj, **kwargs):
"""
Serialize an object to a bytestring.
:param obj: the object to serialize
:param kwargs: keyword arguments passed to :class:`~.CBOREncoder`
:return: the serialized output
(type(None), CBOREncoder.encode_none),
(tuple, CBOREncoder.encode_array),
(list, CBOREncoder.encode_array),
(dict, CBOREncoder.encode_map),
(defaultdict, CBOREncoder.encode_map),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(type(re.compile('')), CBOREncoder.encode_regexp),
(('fractions', 'Fraction'), CBOREncoder.encode_rational),
(('email.message', 'Message'), CBOREncoder.encode_mime),
(('uuid', 'UUID'), CBOREncoder.encode_uuid),
(('ipaddress', 'IPv4Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv6Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv4Network'), CBOREncoder.encode_ipnetwork),
(('ipaddress', 'IPv6Network'), CBOREncoder.encode_ipnetwork),
(CBORSimpleValue, CBOREncoder.encode_simple_value),
(CBORTag, CBOREncoder.encode_semantic),
(set, CBOREncoder.encode_set),
(frozenset, CBOREncoder.encode_set),
])
canonical_encoders = OrderedDict([
(float, CBOREncoder.encode_minimal_float),
(dict, CBOREncoder.encode_canonical_map),
(defaultdict, CBOREncoder.encode_canonical_map),
(OrderedDict, CBOREncoder.encode_canonical_map),
(FrozenDict, CBOREncoder.encode_canonical_map),
(set, CBOREncoder.encode_canonical_set),
(frozenset, CBOREncoder.encode_canonical_set),
break
self._fp_write(encoded)
def encode_boolean(self, value):
self._fp_write(b'\xf5' if value else b'\xf4')
def encode_none(self, value):
self._fp_write(b'\xf6')
def encode_undefined(self, value):
self._fp_write(b'\xf7')
default_encoders = OrderedDict([
(bytes, CBOREncoder.encode_bytestring),
(bytearray, CBOREncoder.encode_bytearray),
(unicode, CBOREncoder.encode_string),
(int, CBOREncoder.encode_int),
(long, CBOREncoder.encode_int),
(float, CBOREncoder.encode_float),
(('decimal', 'Decimal'), CBOREncoder.encode_decimal),
(bool, CBOREncoder.encode_boolean),
(type(None), CBOREncoder.encode_none),
(tuple, CBOREncoder.encode_array),
(list, CBOREncoder.encode_array),
(dict, CBOREncoder.encode_map),
(defaultdict, CBOREncoder.encode_map),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(type(re.compile('')), CBOREncoder.encode_regexp),
except OverflowError:
break
self._fp_write(encoded)
def encode_boolean(self, value):
self._fp_write(b'\xf5' if value else b'\xf4')
def encode_none(self, value):
self._fp_write(b'\xf6')
def encode_undefined(self, value):
self._fp_write(b'\xf7')
default_encoders = OrderedDict([
(bytes, CBOREncoder.encode_bytestring),
(bytearray, CBOREncoder.encode_bytearray),
(unicode, CBOREncoder.encode_string),
(int, CBOREncoder.encode_int),
(long, CBOREncoder.encode_int),
(float, CBOREncoder.encode_float),
(('decimal', 'Decimal'), CBOREncoder.encode_decimal),
(bool, CBOREncoder.encode_boolean),
(type(None), CBOREncoder.encode_none),
(tuple, CBOREncoder.encode_array),
(list, CBOREncoder.encode_array),
(dict, CBOREncoder.encode_map),
(defaultdict, CBOREncoder.encode_map),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(bytearray, CBOREncoder.encode_bytearray),
(unicode, CBOREncoder.encode_string),
(int, CBOREncoder.encode_int),
(long, CBOREncoder.encode_int),
(float, CBOREncoder.encode_float),
(('decimal', 'Decimal'), CBOREncoder.encode_decimal),
(bool, CBOREncoder.encode_boolean),
(type(None), CBOREncoder.encode_none),
(tuple, CBOREncoder.encode_array),
(list, CBOREncoder.encode_array),
(dict, CBOREncoder.encode_map),
(defaultdict, CBOREncoder.encode_map),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(type(re.compile('')), CBOREncoder.encode_regexp),
(('fractions', 'Fraction'), CBOREncoder.encode_rational),
(('email.message', 'Message'), CBOREncoder.encode_mime),
(('uuid', 'UUID'), CBOREncoder.encode_uuid),
(('ipaddress', 'IPv4Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv6Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv4Network'), CBOREncoder.encode_ipnetwork),
(('ipaddress', 'IPv6Network'), CBOREncoder.encode_ipnetwork),
(CBORSimpleValue, CBOREncoder.encode_simple_value),
(CBORTag, CBOREncoder.encode_semantic),
(set, CBOREncoder.encode_set),
(frozenset, CBOREncoder.encode_set),
])
canonical_encoders = OrderedDict([
def __init__(self, fp, datetime_as_timestamp=False, timezone=None,
value_sharing=False, default=None, canonical=False,
date_as_datetime=False):
self.fp = fp
self.datetime_as_timestamp = datetime_as_timestamp
self.timezone = timezone
self.value_sharing = value_sharing
self.default = default
self._canonical = canonical
self._shared_containers = {} # indexes used for value sharing
self._encoders = default_encoders.copy()
if canonical:
self._encoders.update(canonical_encoders)
if date_as_datetime:
self._encoders[date] = CBOREncoder.encode_date
(bool, CBOREncoder.encode_boolean),
(type(None), CBOREncoder.encode_none),
(tuple, CBOREncoder.encode_array),
(list, CBOREncoder.encode_array),
(dict, CBOREncoder.encode_map),
(defaultdict, CBOREncoder.encode_map),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(type(re.compile('')), CBOREncoder.encode_regexp),
(('fractions', 'Fraction'), CBOREncoder.encode_rational),
(('email.message', 'Message'), CBOREncoder.encode_mime),
(('uuid', 'UUID'), CBOREncoder.encode_uuid),
(('ipaddress', 'IPv4Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv6Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv4Network'), CBOREncoder.encode_ipnetwork),
(('ipaddress', 'IPv6Network'), CBOREncoder.encode_ipnetwork),
(CBORSimpleValue, CBOREncoder.encode_simple_value),
(CBORTag, CBOREncoder.encode_semantic),
(set, CBOREncoder.encode_set),
(frozenset, CBOREncoder.encode_set),
])
canonical_encoders = OrderedDict([
(float, CBOREncoder.encode_minimal_float),
(dict, CBOREncoder.encode_canonical_map),
(defaultdict, CBOREncoder.encode_canonical_map),
(OrderedDict, CBOREncoder.encode_canonical_map),
(FrozenDict, CBOREncoder.encode_canonical_map),
(set, CBOREncoder.encode_canonical_set),
(OrderedDict, CBOREncoder.encode_map),
(FrozenDict, CBOREncoder.encode_map),
(type(undefined), CBOREncoder.encode_undefined),
(datetime, CBOREncoder.encode_datetime),
(type(re.compile('')), CBOREncoder.encode_regexp),
(('fractions', 'Fraction'), CBOREncoder.encode_rational),
(('email.message', 'Message'), CBOREncoder.encode_mime),
(('uuid', 'UUID'), CBOREncoder.encode_uuid),
(('ipaddress', 'IPv4Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv6Address'), CBOREncoder.encode_ipaddress),
(('ipaddress', 'IPv4Network'), CBOREncoder.encode_ipnetwork),
(('ipaddress', 'IPv6Network'), CBOREncoder.encode_ipnetwork),
(CBORSimpleValue, CBOREncoder.encode_simple_value),
(CBORTag, CBOREncoder.encode_semantic),
(set, CBOREncoder.encode_set),
(frozenset, CBOREncoder.encode_set),
])
canonical_encoders = OrderedDict([
(float, CBOREncoder.encode_minimal_float),
(dict, CBOREncoder.encode_canonical_map),
(defaultdict, CBOREncoder.encode_canonical_map),
(OrderedDict, CBOREncoder.encode_canonical_map),
(FrozenDict, CBOREncoder.encode_canonical_map),
(set, CBOREncoder.encode_canonical_set),
(frozenset, CBOREncoder.encode_canonical_set),
])
def dumps(obj, **kwargs):
"""