Pretty remote class registration syntax for PyAMF

November 17th, 2009 § 2 comments

Going to transmit an object of some class between your Flex application and Python server (using PyAMF) you first have to register a class alias in both frameworks for proper serialization into AMF and back.

In AS3 you will probably write something like:

package model
{
	[RemoteClass(alias="model.MyClass")]
	public class MyClass { 

		public var a:uint
		public var b:String

	}
}

while the corresponding class on server side and its registration will look like:


class MyClass:
    def __init__(self, *args, **kwargs):
        self.a = args[0]
        self.b = args[1]

pyamf.register_class(MyClass, "model.MyClass")

it seems tempting to have a syntax, for registering server side class in similar way as it is done in Flex:

@RemoteClass(alias="model.MyClass")
class MyClass:
    def __init__(self, *args, **kwargs):
        self.a = args[0]
        self.b = args[1]

The magic is done with a Class Decorator, a quite new feature, presented in Python 2.6.

Here is the class decorator performing registration:

class RemoteClass(object):

    def __init__(self, alias):
        self.alias = alias

    def __call__(self, klass):
        pyamf.register_class(klass, self.alias)
        return klass

Happy coding :)

Tagged , , , ,

§ 2 Responses to Pretty remote class registration syntax for PyAMF"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What's this?

You are currently reading Pretty remote class registration syntax for PyAMF at THE MOCK-TURTLE'S STORY.

meta