Adapter Framework¶
Introduction
The Adapter Framework allows Mini-AMF to integrate nicely with other Python libraries. This includes setting up type conversions, class mappings, etc.
Adapters Overview¶
We currently have adapters for some of Python’s extended data structure types:
Extended types are converted to basic data types that AMF can serialize. An
array.array, for instance, will arrive on the far end as a list.
How It Works¶
The adapter framework hooks into Python’s module loader, so you do not need
to explicitly load adapter modules. When you import both miniamf and a
library that Mini-AMF provides adapters for, the adapters will be
activated. You can do this in either order:
import array
import miniamf
or
import miniamf
import array
Building Your Own Adapter¶
Your custom module:
1 2 3 4 5 | # mymodule.py
class CustomClass(object):
def __iter__(self):
return iter([1, 2, 3])
|
Glue code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from miniamf.adapters import register_adapter
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))
register_adapter('mymodule', when_imported)
|