Trivial Thoughts
Thoughts and discussion on programming projects using the Python language.


Python Sites of Note
Software Development



Recent Posts
 6/15/04
 5/16/04
 5/13/04
 5/10/04
 5/8/04
 5/5/04
 5/5/04
 4/23/04
 9/23/03
 9/22/03
 9/12/03
 9/11/03
 8/21/03
 7/21/03
 7/17/03
 7/10/03
 7/7/03
 7/1/03
 6/26/03
 6/25/03
 6/18/03
 6/15/03
 6/2/03
 5/28/03


Subscribe to "Trivial Thoughts" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Wednesday, May 05, 2004
 

Handling CSV Strings, Redux

I've written several times about the Python csv module and its limited API.  I presented an adaptor class for use when you want to parse a CSV string, without being locked into reading sequentially from a file.  Here's another way of doing it, using Python's standard 'I have a string, but I need a file-like object' adapter, the StringIO module.

import StringIO    # Or use cStringIO, it's faster
import csv
# First, create the StringIO object, then the csv reader object
# using the StringIO object.
sf = StringIO.StringIO()
csvReader = csv.reader(sf)
# Now, write a CSV string out to the StringIO object.
csvData = '1,2,three,"four,five",6'
sf.write(csvData)
# We have to seek the StringIO pseudo-file back to its start.
sf.seek(0,0)
# Now read in the same data via the csv reader object.
parsedData = csvReader.next()

When we look at what parsedData contains, we see a list like so:

[ '1', '2', 'three', 'four,five', '6' ]

Using StringIO rather than the adaptor class I presented earlier is a somewhat more heavy-weight way of solving the problem, but might fit your use better.


9:07:14 PM  comment []    

Python and X10 Home Automation, Part 1

I recently saw an ad from x10.com for a free (you pay shipping) X10 starter kit, including a 'Firecracker' computer interface.  That was a deal I couldn't pass up, so I ordered it through their web site, and 3 days later, the kit arrived.

The kit consists of the CM-17a 'Firecracker' serial computer interface, which transmits via radio, a transceiver module which receives the radio commands from the Firecracker and retransmits them via the X10 protocol over your house wiring, a lamp modules for controlling... lamps, and a PalmPilot-sized hand-held remote control that lets you manually do what the computer interface does.  Oh, and the transceiver module also double as an appliance module, allowing you to control appliances of up to 500 watts.

With the hand-held controller, you can control any X10 modules you have, either the ones that come with the kit, or any add-on modules you may want to buy.  You could go wild, like many do, and completely automate your home -- lights, appliances, garage door, pool heater, ferret feeder, whatever.

But with the computer interface, things get much more interesting.  You can, for example, download from x10.com a free application that duplicates the appearance and functionality of the hand-held controller on your computer screen.  Or, you can download, for $20, an application that fully utilizes your computer and the x10 interface to do full automation.  Want your hot-tub to turn on at a certain time every day?  No problem.  Want your lights to simulate an occupied house while you are on vacation?  Easy.

Naturally, hand an X10 computer interface to a Python programmer, and he'll immediately start writing code for it.  Or that was my intent, anyway.  The first thing I did was google around for any existing Python projects for X10. I found two, Pyxal and Pyx10.  Both projects seem to be unmaintaned.  Pyxal is pure Python, and does not support the recent X10 controllers, like the Firecracker.  Pyx10 uses a wrapper to turn the XAL library into a Python extention module.  It supports recent X10 controllers, including the Firecracker.

I downloaded and examined both.  Pyxal was right out, as it has no Firecracker support (why not add it yourself, you ask?  I'll get to that in a moment...).  Pyx10 and XAL looked good.  After compiling and installing XAL (a snap), I tried compiling Pyx10.  Nope.  The wrapper code for XAL would not compile.  From a quick exam, it looked like it was out-of-sync with XAL.

I could have continued hacking at it to get it to work, but further googling (the trademark police are gonna get me), I found Project WiSH, a project for turning X10 device drivers into... well, Linux device drivers.  Super!  Instead of having to do low-level device handling from my code, I can simply open a linux device driver and write commands to it, just like I was writing text to a file.  And WiSH was a snap to compile and install.  Just make sure you have your kernel source loaded on your machine.  (For the CM-17a 'Firecracker', be sure to download the 1.6.10 version of WiSH.  The later 2.0.1 version does not yet support it.  But both versions support the CM-11a, which is the other modern popular X10 computer interface controller.)

Now, I do my work under Linux, so this is just what the code doctor ordered.  Actually, it's even better than it sounds.  You see, there's this little bit of info about that Firecracker X10 controller...

If you look at one of the other X10 computer interfaces, say the CM-11a that comes with another of the home automation intro packages that x10.com sells, you will see that it is controlled via the computer in a manner rather like an external serial modem.  Connect it to your serial port, and send it strings of ASCII characters.  Not so with the CM-17a 'Firecracker'.  This little guy is a serial pass-thru 'dongle', very small.  From what I can tell from my Google research, you must directly control the radio transmitter in it via bit-tiddling the RTS and DTR lines of the serial port.  You must assemble a 5-byte command via bit masking, then bit-shift it out to the CM-17a by directly controlling the states of the RTS and DTR lines, doing the timing yourself.  There are no smarts.  Ouch.  No wonder this is the bargain-basement controller.

The CM-11a controller has another advantage, too.  It's smart, it has its own processor.  So you don't even need to leave your computer on to do real-time home automation.  Use the scheduling software to send it commands, like 'turn on my security light at local-time dusk, and turn it off at dawn', and the CM-11a will do it, all by itself.

But I don't have the CM-11a.  I have a CM-17a and a Linux box.  Add in the device drivers from Project WiSH, and from a Linux command line, I can execute 'echo "on" >>/dev/x10/a1', and send the 'on' command to the X10 device at house code 'A', unit code '1'.  How cool is that?

OK, how can we combine equal portions of X10, Project WiSH, Linux, Python, and fun?  (OK, fun gets a bigger portion.)

Here's the deal.  I work for a major software house.  We do automated nightly compiles of our code on all of the platforms we support (Linux, various flavors of UNIX, Windoze).  The last thing you want is for some code change you made that day to 'break the build'.  The automated process sends out email giving that night's build status.  If you broke the build, it's supposed to be your first priority to fix it.

I keep forgetting to check my email.  I have many projects, they grab my attention, and it may be hours before I check my mail.  Yes, I have a little task bar thingie that tells me if I get new mail.  I don't look at it if I'm concentrating on a problem.

Python and X10 to the rescue!  (This is a fun solution looking for a problem.)  I now have a Python script that is run via cron every 10 minutes.  It uses the poplib and email modules to grab and parse my email, looking for the specific patterns that a 'you broke the build' message will contain.  If it finds such a message, it opens and writes an 'on' command to the proper X10 device driver, which then turns on the BIG RED ROTATING LIGHT.  I kid you not.

This is so much fun!


2:34:14 PM  comment []    


Click here to visit the Radio UserLand website. © Copyright 2004 Michael Kent.
Last update: 6/15/2004; 4:14:01 PM.
This theme is based on the SoundWaves (blue) Manila theme.
May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun

Previous/Next