incorporating dejaMoo: best of breed bull…

Django on Jython

Absolutely.

It's on!

Leo writes about his experience porting Django to Jython and there's a thread on the django-developers group too.

Funny how things seem to click for a bunch of people all at once - the release of Jython 2.2 seemingly the catalyst.

For my part I've been working on porting sections of Django to Jython on and off for a couple of weeks. Whilst Java is great for paying the bills, developing web applications with it is pretty slow (by comparison).

My stack from bottom up looks something like this MSSQL -> {Spring: [Hibernate , Service tier]} -> Web framework. Yes, it's Enterprisey in the pejorative sense! ;)

One of the web frameworks is actually Flex Data Services, which is great for RIAs but terrible where you want to bang together a quick admin app.

I figured the architectural gaps between Velocity templates and the enterprise business tier I mentioned above could be filled very nicely with parts of Django, such as New Forms: Where Leo adopts a more holistic approach outside of the container, I've been fiddling around with the appealing concept of Django MVC as Struts 3.0!!

Lessons

Today, Django in Jython not only involves not only resolving the differences between CPython and Jython, but also back-porting to Python 2.2. It's easier said than done, but here are a couple of random things I've found.

For django.utils.datastructures the yield keyword is required which in 2.2 is imported from future:

   from __future__ import generators
   

An annoyance is the absence of shallow or deep copying for class instances. In modern CPython, the following is possible:

   >>> import copy
   >>> class A(object):
   ...           var = 10
   ... 
   >>> a = A()
   >>> b = copy.copy(a)
   >>> a.var = 15
   >>> a.var
   15
   >>> b.var
   10
   

But in Jython:

   >>> import copy
   >>> class A(object):
   ...           var = 10
   ... 
   >>> a = A()
   >>> b = copy.copy(a)
   Traceback (innermost last):
     File "<console>", line 1, in ?
     File "/Users/cam/Java/jython/Lib/copy.py", line 78, in copy
   Error: un(shallow)copyable object of type <class '__main__.A'>
   

This becomes important as django.newforms.forms.BaseForm takes a copy of base_fields for the instance to manipulate. The solution is to implement __copy__ on django.newforms.fields.Field and components, something I've part completed.

According to the Jython doc:

The dictionaries used by classes, instances, and modules in Jython are not the same as the dictionaries created by {}.

Instead they are instances of org.python.core.PyStringMap, which doesn't implement the same methods as dict, most notably {}.pop(x).

I got the desired behaviour, (albeit clumsily) like this:

       def __new__(cls, name, bases, attrs):
           fields = [(field_name, attrs.get(field_name)) 
             for field_name, obj in attrs.items() if isinstance(obj, Field)]
          ...
           for n,o in fields:
               attrs.__delitem__(n)
           ...
   

As you've probably guessed, there is quite a bit to do here. Having read Leo's post, I'm not sure whether it's best to pursue Django within the web container, outside of it, or both.

Deploying to the container would broaden the appeal of Django in the enterprise, but I don't know it can be achieved without forking the source. Perhaps the effort would be better spent contributing to the 1.0 release? Certainly Jython will have come along a little further by then too.

Thoughts?

Comments (10) § Posted by in on
AddThis Social Bookmark Button

Comments 10

  1. Marty Alchin wrote:

    Excellent to see another person interested in seeing a working Django on Jython. I'm not really going anywhere with it myself at the moment, partially I'm torn between trying to backport Django to Jython 2.2 (your option, and my original one) and using Jython SVN, which is closer to Python 2.3 (Leo's option, and I dabbled there too). Jython SVN doesn't have the yield problem, but it does still have the PyStringMap issue.

    Ideally, Jython's PyStringMap would support pop(), and the Pylons folks have considered just that, but otherwise, it's a tough call on how best to do it. Leo creates a new dictionary and calls its update() with the PyStringMap. I've also found that it can be as simple as attrs = dict(**attrs) and the rest works fine. The Pylons folks seem to

    But there are many such cases, which penalize CPython users (in terms of performances) in order to support Jython. It remains to be seen whether they'll add up enough to require a separate fork in order to be worthwhile.

    Posted September 6, 2007 at 12:40 a.m.
  2. Cam wrote:

    Hi Marty,

    Thanks for all that great info. What was your experience with Jython SVN? Stable?

    I figure if the aim is to get Django into the enterprise it will need to run on a Jython release, or at least a release candidate.

    c.

    Posted September 6, 2007 at 9:17 a.m.
  3. Marty Alchin wrote:

    Well, I've been having problems with it myself, but I fully expect I'm building it wrong somehow. I expect Leo wouldn't be publishing his findings unless it was a reasonably stable solution.

    Posted September 7, 2007 at 3:28 a.m.
  4. Frank Wierzbicki wrote:

    If you decide to look at Jython's trunk, I just added pop() to PyStringMap today.

    Posted September 7, 2007 at 3:30 a.m.
  5. Marty Alchin wrote:

    Also, I'm looking to set up an aggregate of the various Django on Jython blogs that are running so we can track progress more easily. I notice you have a Django-specific feed set up, but could you set one up for your Jython tag as well?

    Posted September 7, 2007 at 3:38 a.m.
  6. Cam wrote:

    @Frank - Wow! That's very cool. I think I'll check it out ;)

    @Marty - Done. Jython feed

    Posted September 7, 2007 at 9:19 a.m.
  7. Henrik Eriksson wrote:

    If you're looking for something to fill the "gaps between Velocity templates and the enterprise business tier", maybe CherryPy might be a better fit than a full-stack framework like Django. I'm currenty playing with Jython, CherryPy and Velocity with some success at the moment. For info about running in a container, this might be of interest: http://henkenotes.blogspot.com/2006/1...

    Posted September 7, 2007 at 6:45 p.m.
  8. Cam wrote:

    Thanks Henrik. You might be on to something there. I was mostly thinking of Django because I love the framework, and am reasonably familiar with the source. Subscribed to the feed of that blog - ta!

    Posted September 8, 2007 at 11 p.m.
  9. Leo Soto wrote:

    Hey, good to see more people with common goals!

    About the deploy thing, I'm all for getting Django running on Servlet Containers. Modjy should make this easy, keeping us for forking anything.

    Are you looking forward into publishing your changes in some way?

    Posted September 10, 2007 at 5:29 a.m.
  10. Cam wrote:

    @Leo - I haven't got a whole heap to show for my efforts at this point because I was more working towards using parts of Django to glue the enterprisey Java stack together. That aside, I'd love to see Django under Jython. Do you think an official branch is justified. I'd be keen to contribute.

    c.

    Posted September 10, 2007 at 11:08 p.m.

Comments are now closed.

Tweet Tweet

Stuffs

Thanks for dropping in.

This is the personal website of Cam MacRae. Any opinions expressed here are my entirely own, and have jack to do with my employer.

It's the product of a little elbow grease, the news.ycombinator noprocrast feature, and a healthy dose of Django.

A Django site.

Tags

  1. D (1)
  2. SOA (1)
  3. ajax (2)
  4. apollo (1)
  5. architecture (1)
  6. blogs (2)
  7. carsales (1)
  8. collaboration (1)
  9. css (1)
  10. django (9)
  11. duels (1)
  12. email (1)
  13. erlang (3)
  14. findability (1)
  15. flex (3)
  16. folksonomies (1)
  17. funny (2)
  18. geek (20)
  19. google (3)
  20. innovation (1)
  21. iphone (1)
  22. javascript (4)
  23. jython (1)
  24. life (5)
  25. lighttpd (1)
  26. lisp (1)
  27. mac (1)
  28. macbook (1)
  29. marketing (1)
  30. open-source (1)
  31. oracle (2)
  32. python (6)
  33. rails (2)
  34. ruby (1)
  35. silverlight (1)
  36. skitch (1)
  37. startups (4)
  38. tech (21)
  39. twitter (1)
  40. usability (1)
  41. web20 (6)
  42. work (3)
  43. yui (2)
ten1000miles.com | Aussie Blogs |  Feed

Creative Commons License This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.