My first Chumby app

I managed to get my hands on an Insignia Infocast 8 used at a bargain price on eBay. It’s an 8″ touchscreen device that is a clone of the Chumby. They’re designed to be hackable, so you can SSH into it as a feature. The apps are written in Flash, and I managed to get my first one running yesterday. Of course I’m using Linux tools only, but found that Haxe and Geany do a great job. I’ll roll the code and the steps I took into the post after the jump.

Continue reading

Terminal Life

Terminal-life

After completing my first iteration of Conway’s Game of Life I was unhappy with the performance. After a bit of testing I discovered that the real bottleneck wasn’t in my algorithm, but in the overhead associated with the pygame module that was taking care of the graphics.

 

I just completed a branch of the code that does away with the pygame module. Instead, it runs in a bash shell. The program uses tput to read in lines and columns available in the terminal, and also to move the cursor back to the top left before each new generation is drawn. Of course the limitation here is that it only works on Linux. I was able to maintain the look by using a Unicode character which draws a box. That’s a screenshot of the code running.

Conway’s Game of Life

I finally got around to programming Conway’s Game of Life. I’ve long wanted to give this a try but just today decided to take some time to myself and actually do it. I chose Python, a language I’ve worked with quite a bit but one I’ve never used for GUI programming. I spent the majority of my time trying to figure out how to display the Life grid, and decided to use a package called pygame, which I enjoy quite a bit!

About the code:

The game itself was actually pretty easy to code. I decided to make a multidimensional array as a lookup table. The first dimension is indexed by whether the current cell is dead (0) or alive (1). The second dimension is indexed by the sum of the living cells around the test cell. The return value is status of the test cell after the rules are applied for the next generation. The rest is just iterating through the various buffer arrays and then writing to the display.

The number of cells, cell size, gap between cells, delay between generations, and percent of live cells at genesis are all configurable. The game checks for stagnation at the end of evolution and will change the window title to show how many it took to reach equilibrium.

The pygame package turned out to be very easy to work with. I has an event handler that takes care of the delay time between generations. Take a look at it if you are ever working on a game!

I’d love to hear your thoughts about my code. Check it out and then leave a comment or send me a tweet!

Source Code Repository

Follow Me:

@szczys

What I’ve learned about using the Beautiful Soup Python module

I like to use Beautiful Soup in combination with urllib2 to parse HTML from Python scripts. The problem is that I spend like 30 minutes relearning how to use it every time I do a new project. So, for my own use (and maybe yours) here's my quick tips for syntax.

I always start off the same way, two lines of code to snag and objectify the HTML:

html = urllib2.urlopen("http://hackaday.com/comments/feed").read()
soup = BeautifulSoup(html)

From there it's a matter of working with the 'soup' data object. This one gets an RSS feed of comments. They have are partitioned into <item> tags which you can traverse like this:

soup('item')[0]

Which is an array with an index (this is item 0). But you can also iterate through the list using:

for item in soup('item'):

From there just walk through the tree hierarchy. Here's how you can get the publish date (string surrounded by <pubdate> tags) for the item. Notice that you need to index the pubdate in order to access its string data:

soup('item')[0]('pubdate')[0].string

The part that always confuses me is the need for the index. It identifies which tag you're accessing in case there are multiples in this part of the tree. You can get the number of tags found by wrapping your tag term in the length funtion:

len(soup('item'))

Should always return 15 because that's the number of comments WordPress is set to publish in the RSS feed.

There are other ways to do this using soup.findAll, but I find this one usually works the best.