Rest as a boring servlet

A coworker whipped up a generic REST interface for any Ruby on Rails activerecord (data model).  What he described (in 5 minutes) was a nice implementation.  I wanted see how the generic django REST interface was coded.

http://code.google.com/p/django-rest-interface/

I was pleasantly surprised to realize that they Python developers simply used the normal form processing to handle rest and didn’t invent a new paradigm.

On an early project we tried to implement RESTlet for a java based REST application.  Under load we saw some strange problems and the code was reverted to normal servlets without too much pain.

The beauty of REST is its simplicity, yet there is so much energy being expended to ‘simplify’ it.

The real magic is to standardize on sending XML or JSON rather then url encoded data of an http form POST.

Motocycles and Python turn toil into joy

So today started out great.

Temperatures in the 80′s, so I took the kiddies outside. I put my cellphone on the trunk of my sisters’s car, put the kids on their bikes, put skates on my feet and rolled out to enjoy the day.

Things were going well with my littlest one making turns at each end of the block. My sister pulled up and shouted some encouragement as she drove past. I waved and noticed my black cell phone on the white trunk of her car!

I started to skate hard, but I couldn’t leave my girl in the street. I shuffled the girls back into the house and summoned the boy to watch them and tore off after my sister. I searched all the likely routes she would have taken but could not find it.

I called her and found out where she went and was determined to look again.

At this point I was pretty upset about this and feeling down.

So I got on my motorcycle and started looking for it. I rode a block at a time and dismounted to look. After about 15 minutes I realized it was looking pretty grim. I also realized I was out on my bike on a beautiful day, and any day you ride simply isn’t that bad.

I had a similar occurrence with python the other day. I had to scrape some data out of a nasty, IE only, webapp. It was pretty ugly but I got into writing the app as a pyunit test case and using assertions to ensure I got all the data. The difficult nature of the data coming in made me appreciate just how much you can do with test cases. The scraping task was about as pleasurable as looking for my cellphone across town.

But at least I got to do it in python and enjoy the ride!

csv to xml via python

Today at work our main Flash developer asked me about expanding his skills and learning either Ruby or Python. My personal preference is towards python but ruby has its place. Flash is really doing well in the Java/Enterprise space lately so I went that way.

Most of the Flash backend in our work is xml based so XML was on my mind anyway. Recently we needed to mock up a xml data data file for a project while the real APIs are being completed. We had the data in a csv file so we asked some developers to whip up a xml file.

As I started writing up the ticket I started thinking it would be easier to just do it myself.

Given the speed of python development I was right!.

#!/usr/bin/env python
"""
cvs2xml.py

Created by Aaron Held on 2008-04-11.
Make the xml from the csv using dom and other three letter acronyms
"""import sys,os
import unittes
import csv

from pprint import pprint
import xml.etree.ElementTree as ET

class Cvs2xml:
    def loadCSV(self):
        """Load the data and return a list of maps"""
reader = csv.DictReader(open(r"input.csv",'r'))
        rows = []
for row in reader:
            rows.append(row)
return rows

def mappings(self):
        """
        Map csv file to xml field names
        """
mapping = { 'id':"store_number"
        return mapping

def createXML(self,listofrows):
"""Turn lists into xml
"""
root = ET.Element("stores")
for row in listofrows:
store = ET.SubElement(root, "store")
for xml_field in row.keys():
csv_field = self.mappings().get(xml_field,xml_field)
ET.SubElement(store, xml_field).text = row.get(csv_field,"")
print(ET.tostring(root))
#tree = ET.ElementTree(root)
#tree.write("output.xml")

class Cvs2xmlTests(unittest.TestCase):
def setUp(self):
pass

def testRun(self):
'''Lazy way of main()'''
cvs2xml = Cvs2xml()
rows = cvs2xml.loadCSV()
cvs2xml.createXML(rows)

if __name__ == '__main__':
unittest.main()

Michael Clayton

I watched Michael Clayton today

George Clooney as Michael Clayton

A decent legal / espionage plot. Predictive enough. The lawyer is defending an international chemical company for years.  One day he goes off his anti-depressants and decides to champion the farmers being killed by the chemical additives. Drama ensues when the ‘fixer’ Michael Clayton is set on the case.

But the lawyer who had the breakdown was Michael’s friend, and since Mr. Clayton has a good relationship with his son, so we assume he is not wholly evil. Its a good thing we don’t live in a world with people that would allow this to happen. A quick googling shows that our government recently decided to immediately phase out a harmful pesticide (that has been in use since 1950s) by 2012.

Aaron’s improved ending: Michael takes the money at the end, leaves his stressful life behind and the movie closes with him sipping a drink on th beach.

Technorati: