Tagged: python

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()