Table of Contents
Adapter Framework
Introduction
The Adapter Framework allows PyAMF to integrate nicely with other Python packages. This includes setting up type conversions, class mappings, etc.
We currently have adapters for the following packages:
- Django
- Google App Engine
- sets module
- decimal module (where available)
New in PyAMF 0.4:
How It Works
The adapter framework works silently in the background. This means that the user does not need to specifically import the Django adapter module within PyAMF, it is all handled in the background. It works by using a custom version of the Importing module (including unit tests) to fire a callback when the django module is imported and accessed.
It is important to note that PyAMF does not load all the modules when registering its adapters and therefore it doesn't load modules that you don't use in your program.
So, code like this works:
from django import http import pyamf
As well as:
import pyamf from django import http
The adapter framework makes it easy to add other packages to the list, as PyAMF matures.
Building Your Own Adapter
Your custom module:
# mymodule.py class CustomClass(object): def __iter__(self): return iter([1, 2, 3])
Glue code:
from pyamf.adapters import register_adapter def when_imported(mod): """ This function is called immediately after mymodule has been imported. It configures PyAMF to encode a list when an instance of mymodule.CustomClass is encountered. """ import pyamf pyamf.add_type(mod.CustomClass, lambda obj: list(obj)) register_adapter('mymodule', when_imported)
And you're done!
What Next?
Contributions (including unit tests) are always welcome!
