musings of a peripatetic

twitterd in Python

So a while ago I decided that twitter is the new finger albeit more palitable for the masses and wrote twitterd in perl. I’ve been using this to update my twitter and facebook status ever since. A couple of months ago I decided to rewrite it in Python. Next stop C?

#!/usr/bin/python
"""
twitterd
Author: Nathan Charles <ncharles at gmail dot com>
Version: 0.1
This program twits the contents of a file when it's time stamp is updated.  
The functionality is similar to that of finger/.plan in days of yore.
Requires:
   pytwitter
   configobj
This program has no warranty to the full extent of the law
"""

import pytwitter
from sys import exit, argv
from os import path
from time import sleep

POLLINT = 60
READLEN = 140

def setEnv():
    from configobj import ConfigObj
    from os import environ
    import getopt
    env = {}
    HOME = environ.get("HOME")
    config = ConfigObj(HOME + '/.twitrc')
    env["watchfile"] = HOME + '/.plan'
    try:
        env["username"] = config['username']
        env["password"] = config['password']
    except:
        #.twitrc doesn't exist not nessarily a problem
        pass

    try:
        env["optlist"], args = getopt.getopt(argv[1:], 'dfh',["username=","password="])
    except getopt.GetoptError:
        usage()
        print "called exception"
        exit(1)
    for o,a in env["optlist"]:
        if o == "--username":
            env["username"]=a
        if o == "--password":
            env["password"]=a

    if not "username" in env:
        print "username is not set"
        exit(1)
    if not "password" in env:
        print "password is not set"
        exit(1)
    return env

def watch(username, password, watchfile):
    """Updates a twitter feed with the contents of a file when the timestamp is updated.
    """
    try:
        timeStampInitial = path.getmtime(watchfile)
    except:
        #file doesn't exist setting to 0 because it might exist in the future
        timeStampInitial = 0
    while 1:
        try:
            timeStampIncrimented = path.getmtime(watchfile)
        except:
            #file still doesn't exist
            timeStampIncrimented = 0
        if (timeStampInitial==timeStampIncrimented):
            sleep(POLLINT)
        else:
            timeStampInitial = timeStampIncrimented
            #update status
            statusTxt = file(watchfile).read(READLEN)
            client = pytwitter.pytwitter(username=username, password=password)
            rc = client.statuses_update(status=statusTxt)
            print rc

def usage():
    """Prints when called with no arguments or with invalid arguments
    """
    print """usage: twitterd [options]
   -d    daemonize
   -f    run in forground
   -h    help

   twitterd must have twitter username/password set. this can be set via .twitrc

   --password    set username
   --username   set password  
    """

if __name__ == "__main__":
    g = setEnv()

    if g["optlist"]:
        for o,a in g["optlist"]:
            if o == '-h':
                usage()
                exit(1)
            if o == '-d':
                #run in background
                import daemon
                daemon.daemonize()
            if o == '-f':
                #run in forground
                print "Running in Foreground"
    else:
        usage()
        exit(1)

    watch(g["username"], g["password"], g["watchfile"])

Low cost data logging with Arduino

I’ve been wanting to play around with an Arduino for a while, so I bought a Duemilanove without a set goal in mind. Recently I’ve been working with Solar thermal and I needed to measure several temperatures simultaneously so decided this would be as good a project as any. I bought some “4700 ohm” thermistors and hooked them up with some other 4700 ohm resistors in a voltage divider configuration. The output of which is hooked up to the analog inputs on the Arduino. I found a Thermistor Example in the Arduino playground which I had to modify somewhat to make work with my configuration. I was confused for about a day as to why my thermistor equation T=\frac{\beta }{\ln \left ( \frac{R_{T}}{R_{25C}}\right )+\frac{\beta}{298.15}}-273.15 was giving me garbage. After doubting my ablity to do math pretty severely I finally figured out that my math was indeed correct and as it turns out the “4700 ohm” thermistor package was incorrectly labeled and were actually only 470 ohm. Anyway, I modified the code so it would work with my thermistors and pass multiple temperatures back across the serial line.

Arduino Thermistor Sketch

Then I wrote some quick and dirty python code to read the values coming off the serial line and write it to a csv file which I could open with OpenOffice.

import serial
import time
ser = serial.Serial('/dev/tty.usbserial-A6008dxP', 9600, timeout=1)
logfile = open('test.csv', 'a')

while 1:   # read a '\n' terminated line 
   line = ser.readline()   # read a '\n' terminated line
   if not line:
      break
   words = line.split()
   now = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime())
   a =  "%s, %s" % (now, line)
   if line.find(',') != -1:
        logfile.write(a)
        logfile.flush()

logfile.close()
ser.close()

The resolution isn’t that great because the inputs are only a 10bit ADCs but it’s more than enough to get trends. Without a more accurate thermometer, I can’t tell for certain how accurate it is, but as far as I can tell it reads faster and more accurately than the mercury thermometer we have. In university I would have done this with a fairly expensive LabVIEW system. The impressive thing is that I can get good enough information for what I need with Python and less than $30US in parts including the Arduino.

On the Death of Newspapers

I have an old soul. At least that’s what one of my bosses at IBM told me once. He made the comment in referance to the fact that I would often bring a paper notebook to our one on one meetings instead of a laptop. I like paper, I like drawing, I like the printed page, I like books. You can’t exactly read Jasper Fforde or Terry Pratchett through the takeoff or landing of a plane with a laptop or a kindle. Sometimes I’ll read a book on my laptop, but that’s only because there isn’t a physical copy handy.

Everybody tells me that newspapers are dying. Which is sad. I can’t complain too much though, because I’m the demographic that’s killing them. I never subscribed. Part of it was that I didn’t really like the local newspapers, (although the Independant is great and it’s free), and part of it is that I’m not a morning person and generally a little rushed in the morning. As result most of my news comes from RSS feeds; sources such as the BBC and NY Times online editions.

Which brings me to my point. I always thought it would be great if I could get a newspaper that was personalized for me. It would aggregate my selection of news sources and simply format, print and deliver. I’ve been sick for the last few days and spent a lot of time on my computer. In that time I learned a bit about LaTeX, which is a way of laying out text. Apparently it’s widely used in the academic world because it’s a way of separating formatting from content and it does professional fonts pretty well. I hacked out a quick and dirty python program to grab an rss feed and turn it into a PDF via LaTeX. It’s not polished but it’s a Proof of Concept. rss2latex.py

I’m not sure I want to avocate printing off your RSS feeds everyday; it’s a waste of a lot of paper. However I think this idea could be developed to format RSS in a more Newspaper sort of format, complete with all my web comic strips on one page! Acrylic’s Times RSS reader tries to form RSS feeds like a Newspaper, and it’s a really nice looking program, however, it doesn’t work very well with web comic strips and when it comes down to it, it’s all about the funnies.