Archive for the 'Code and Cruft' Category

Nobody knows what f_fsid is supposed to contain

Thursday, January 5th, 2006

It's not everyday that a man page makes you laugh out loud, but this blurb from the page for statfs did the job for me:

CODE:
  1. Nobody knows what f_fsid is supposed to contain

I really did laugh out loud, until I read below in the page that basically explains that f_fsid just holds different things on different operating systems.

Good for a laugh nonetheless.

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:

PYTHON:
  1. #!/usr/bin/python
  2. import os
  3.  
  4. #----------------------------------------
  5. # Gives a human-readable uptime string
  6. def uptime():
  7.  
  8.      try:
  9.          f = open( "/proc/uptime" )
  10.          contents = f.read().split()
  11.          f.close()
  12.      except:
  13.         return "Cannot open uptime file: /proc/uptime"
  14.  
  15.      total_seconds = float(contents[0])
  16.  
  17.      # Helper vars:
  18.      MINUTE  = 60
  19.      HOUR    = MINUTE * 60
  20.      DAY     = HOUR * 24
  21.  
  22.      # Get the days, hours, etc:
  23.      days    = int( total_seconds / DAY )
  24.      hours   = int( ( total_seconds % DAY ) / HOUR )
  25.      minutes = int( ( total_seconds % HOUR ) / MINUTE )
  26.      seconds = int( total_seconds % MINUTE )
  27.  
  28.      # Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
  29.      string = ""
  30.      if days> 0:
  31.          string += str(days) + " " + (days == 1 and "day" or "days" ) + ", "
  32.      if len(string)> 0 or hours> 0:
  33.          string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", "
  34.      if len(string)> 0 or minutes> 0:
  35.          string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", "
  36.      string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" )
  37.  
  38.      return string;
  39.  
  40. 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:

PYTHON:
  1. (days == 1 and "day" or "days" )

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

C++:
  1. (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...)

C: Power of 2 Array Sizes

Sunday, September 18th, 2005

I just picked up a 10-year old copy of O'Reilly's Practical C, 3rd Edition from the library for some light reading. Mostly, I just wanted to see if I would learn anything (as a guage of my own C proficiency). I'm glad to announce that I learned almost nothing, until, that is, I read page 258, The Power of Powers of 2.

(more...)

Qt: Connecting signals to … signals

Friday, September 16th, 2005

I found out today that Qt's slots/signals architecture is even better than I thought. Normally, developers connect widget signals to widget slots to be notified of events. Today I discovered that signals can actually be connected to other signals, which saved me from writing some really stupid code...

(more...)