def setUp(self): CollectionsTestCase.setUp(self) user_vo = collections.namedtuple('user_vo', 'id name age') miniamf.add_type(user_vo, lambda obj, encoder: obj._asdict()) self.obj = user_vo(1, 'Hadrien', 30) self.orig = self.obj._asdict()
def when_imported(mod): """ This function is called immediately after mymodule has been imported. It configures Mini-AMF to encode a list when an instance of mymodule.CustomClass is encountered. """ import miniamf miniamf.add_type(mod.CustomClass, lambda obj: list(obj))
def test_custom_type(self): def write_as_list(list_interface_obj, encoder): list_interface_obj.ran = True self.assertEqual(id(self.encoder), id(encoder)) return list(list_interface_obj) class ListWrapper(object): ran = False def __iter__(self): return iter([1, 2, 3]) miniamf.add_type(ListWrapper, write_as_list) x = ListWrapper() self.assertEncoded(x, b'\t\x07\x01\x04\x01\x04\x02\x04\x03') self.assertTrue(x.ran)
def test_custom_type(self): def write_as_list(list_interface_obj, encoder): list_interface_obj.ran = True self.assertEqual(id(encoder), id(self.encoder)) return list(list_interface_obj) class ListWrapper(object): ran = False def __iter__(self): return iter([1, 2, 3]) miniamf.add_type(ListWrapper, write_as_list) x = ListWrapper() self.encoder.writeElement(x) self.assertEqual(x.ran, True) self.assertEqual( self.buf.getvalue(), b'\n\x00\x00\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00\x00' b'\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00')
# Copyright (c) The PyAMF Project. # See LICENSE.txt for details. """ C{weakref} support. @since: 0.6.2 """ from __future__ import absolute_import import weakref import miniamf from miniamf.adapters import util def get_referent(reference, **kwargs): return reference() miniamf.add_type(weakref.ref, get_referent) miniamf.add_type(weakref.WeakValueDictionary, util.to_dict) miniamf.add_type(weakref.WeakSet, util.to_list)
""" Adapter for the stdlib C{sets} module. @since: 0.4 """ from __future__ import absolute_import import miniamf def to_sorted_tuple(obj, encoder): return tuple(sorted(obj)) miniamf.add_type(frozenset, to_sorted_tuple) miniamf.add_type(set, to_sorted_tuple) # The sets module was removed in Python 3. try: ModuleNotFoundError except NameError: ModuleNotFoundError = ImportError try: import sets miniamf.add_type(sets.ImmutableSet, to_sorted_tuple) miniamf.add_type(sets.Set, to_sorted_tuple) except ModuleNotFoundError: pass
# Copyright (c) The PyAMF Project. # See LICENSE.txt for details. """ U{array<http://docs.python.org/library/array.html>} adapter module. Will convert all array.array instances to a python list before encoding. All type information is lost (but degrades nicely). @since: 0.5 """ from __future__ import absolute_import import array import miniamf from miniamf.adapters import util miniamf.add_type(array.ArrayType, util.to_list)
# Copyright (c) The PyAMF Project. # See LICENSE.txt for details. """ U{collections<http://docs.python.org/library/collections.html>} adapter module. @since: 0.5 """ from __future__ import absolute_import import collections import miniamf from miniamf.adapters import util miniamf.add_type(collections.deque, util.to_list) miniamf.add_type(collections.defaultdict, util.to_dict) miniamf.add_type(collections.Counter, util.to_dict) miniamf.add_type(collections.OrderedDict, util.to_dict)
Adapter for the U{decimal<http://docs.python.org/library/decimal.html>} module. @since: 0.4 """ from __future__ import absolute_import import decimal import miniamf def convert_Decimal(x, encoder): """ Called when an instance of U{decimal.Decimal<http:// docs.python.org/library/decimal.html#decimal-objects>} is about to be encoded to an AMF stream. @return: If the encoder is in 'strict' mode then C{x} will be converted to a float. Otherwise an L{miniamf.EncodeError} with a friendly message is raised. """ if encoder.strict is False: return float(x) raise miniamf.EncodeError( 'Unable to encode decimal.Decimal instances as there is no way to ' 'guarantee exact conversion. Use strict=False to convert to a float.') miniamf.add_type(decimal.Decimal, convert_Decimal)