Archive for the 'python' Category

PureMVC Python port has been released

[PureMVC - Python]

I am very excited about the potential of this port. As far as I can tell, there isn't a whole lot out there for MVC and Python. There are a handful of server/web MVC frameworks. Django is the one that I have experience with, and it is a fantastic framework. As is true when comparing PureMVC to most frameworks, however, the PureMVC for Python is dead simple. It is a straight port, implementing PureMVC in the same fashion as we see in the original AS3 version. Structurally it differs from Actionscript - it is Python after all - but fundamentally it is the same.

Many thanks to Toby de Havilland for overcoming some of the initial issues with the port, and getting it up and running. Thanks also to Nathan Levesque for running point on the project and getting the ball rolling with the port

Here it is running the Employee Admin demo on XP and OSX:

pmvc_python.jpg pmvc_py_mac.jpg.

pyAMF - Communication with Django from Flex

Up until this point I have been using Django AMF for my remote services. It was working fairly well on my windows machine, but when I tried to deploy my application to my Ubuntu slice, it started kicking up weird errors. I'd spoken with one of the pyAMF developers in a Reddit thread, and they seemed like a motivated group, so I decided to give pyAMF a go.

*sigh*

Now I have new errors to deal with. Not a problem though, signed up to their mailing list and Thijs and Nick were extremely helpful in hunting down the bugs and squishing them dead. pyAMF is still in alpha, but they are moving over to beta soon. It is working well now, so give it a try if you need communication between Flash/Flex/AIR and Python.

The Gateway

They have a couple of examples on the pyAMF site, but here are a couple things that I learned. In my amfgateway.py, which is the equivelant of a Django views.py, I had to register my models so that pyAMF would send and recieve typed custom objects. It ended up looking something like this:

amfgateway.py

import pyamf
from pyamf.remoting.djangogateway import DjangoGateway
from django.contrib.auth.models import Userpyamf.register_class( User, 'django.contrib.auth.models.User' )
 
def get_users(request):
    return User.objects.all()
 
sessionGateway = DjangoGateway({'ve.get_users'  : get_users})  # could include other functions as well

I am using the Django auth structure to authenticate users in my application, so that is the bare bones of what you need to get that going. Here's what the urls.py looks like:

urls.py

from django.conf.urls.defaults import *urlpatterns = patterns(' ',
     (r'^ve/gateway/', 've.clients.amfgateway.sessionGateway'),
)

As you can see, it is extremely simple. It is setup in the same fashion you would use to link up a Django view.

In Flex, I've set up a RemoteServiceDelegate which is extending the Seasar RemotingService [the site is in japanese]. This is actually a holdover from Django AMF, but it works well, so I am going to continue to use it. Here's the code:

RemoteServiceDelegate.as

package com.visualempathy.session.model.delegate
{
 import mx.controls.Alert;
 import mx.rpc.events.FaultEvent;	import org.seasar.flex2.rpc.remoting.RemoteService;
 
  public class RemoteServiceDelegate extends RemoteService
  {
 	public static const gatewayURL:String = "http://myserver.com/ve/gateway/"
 	public function RemoteServiceDelagate()
        {
 		super();
 		useAMF0 = false;
 		gatewayUrl = gatewayURL;
 		destination = 've';
                addEventListener(FaultEvent.FAULT, handleDataFault);
 	}
 
 	private function handleDataFault( fe:FaultEvent ):void
 	{
 		Alert.show( fe.fault.faultDetail );
 	}
 }
}

Using this setup, the destination in the remoting service is used like this:

 sessiongateway = DjangoGateway({'ve.get_users' : ve.get_users
've.get_orders': ve.get_orders})

where 've', the destination in the RemotingService class precedes the method name in the DjangoGateway argument. This can be added to as much as needed as demonstrated above.