Adapter Framework

Introduction

The Adapter Framework allows PyAMF to integrate nicely with other Python libraries. This includes setting up type conversions, class mappings, etc.

Adapters Overview

We currently have adapters for the following packages:

  • Django
  • Google App Engine
  • SQLAlchemy
  • sets module
  • decimal module

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 adding a module loader and finder to sys.meta_path so it can intercept import calls and) to fire a callback when, for example 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:

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 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!

Table Of Contents

Previous topic

Architecture

Next topic

Attribute Control

This Page