Archive for November, 2005

Python: Uptime script

Saturday, November 19th, 2005

I’ve been writing Python for a grand total of 2 days. I recently wrote a little Python script to print a Linx or UNIX system’s uptime. This took about 10 minutes to write, so I was pretty pleased. It even runs well on a 75MHz Busybox embedded Linux system. Check it out:

#!/usr/bin/python
import os

#----------------------------------------
# Gives a human-readable uptime string
def uptime():

     try:
         f = open( "/proc/uptime" )
         contents = f.read().split()
         f.close()
     except:
        return "Cannot open uptime file: /proc/uptime"

     total_seconds = float(contents[0])

     # Helper vars:
     MINUTE  = 60
     HOUR    = MINUTE * 60
     DAY     = HOUR * 24

     # Get the days, hours, etc:
     days    = int( total_seconds / DAY )
     hours   = int( ( total_seconds % DAY ) / HOUR )
     minutes = int( ( total_seconds % HOUR ) / MINUTE )
     seconds = int( total_seconds % MINUTE )

     # Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
     string = ""
     if days > 0:
         string += str(days) + " " + (days == 1 and "day" or "days" ) + ", "
     if len(string) > 0 or hours > 0:
         string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", "
     if len(string) > 0 or minutes > 0:
         string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", "
     string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )

     return string;

print "The system uptime is:", uptime()

One interesting thing about this script is its emulation of the C ternary operator using Python’s short-circuited “and” operator. Notice this bit:

(days == 1 and "day" or "days" ) 

Python doesn’t have the C-style ternary operator:

(days == 1 ? "day" : "days")

So the short-circuited “and” is a decent alternative. Instead of ignorantly printing “1 days”, this code will actually print “1 day” and “2 days” like it should.

Python Sockets: Clean, Concise

Saturday, November 19th, 2005

I was recently pleased to find an excuse to code some Python for the first time. I am impressed. Python doesn’t feel like other programming languages. When I write Python I feel like I’m just telling the computer what to do, rather than constantly worrying about essoteric language issues. What I mean is this. While coding Java, I am constantly thinking about class design, exception handling, patterns, etc. While coding C++, I worry a lot about memory management and I spend a lot of time trying to remember funky STL APIs. However, while coding Python the other day, I almost forgot I was programming. It was like I was just telling the computer what to do, line after line. My day’s work culminated with a pleasant experience coding to the Python socket API. Read on for the details.

(more…)

LDS General Conference Podcast

Saturday, November 19th, 2005

New

Please visit the new LDS General Conference Podcast page. The rest of this info is old.

Older Info Below

The LDS church makes its general conference sessions available as downloadable MP3s, which is great, except my iPod treats them as songs. I prefer for it to treat them as podcasts so that I can resume playing the talks where I leave off after each day’s commute to work. To that end, I created a podcast RSS feed for the October 2005 General Conference, downloadable here:

http://thesmithfam.org/podcasts/lds-general-conference.xml

If you’re a podcast addict like myself, you should know what to do with that link. If not, any popular podcasting software should work. Just find an option to “subscribe to a podcast” and use it with the above URL. This should work with amaroK, gtkPod, Juice (formerly iPodder), or any iPod management or podcasting software.

This feed lets you grab each whole session (4 total), but not individual talks. If there is demand, I may hack some python to automagically create a feed for each talk, which would be pretty darn cool too.

Note that I am not actually distributing the content; this podcast simply points to lds.org to download the MP3 files. This should avoid any legal issues, since I have respected all copyrights. I’m not a lawyer, but I’ve read all the fine print on lds.org and can’t find anything prohibiting this, so let me know if you think I’m in violation.