Archive for February, 2008

Python wedding vows

Kevin Day, February 24th, 2008

I was inspired by this valentine’s day card, and a quote by Blake Ross in Founders at Work. So this wasn’t my original idea, but I still consider my implementation to be heartfelt and sincere.

I asked Kristen if I could use these vows in the wedding, but she didn’t seem thrilled about it. I’ll have to work on her some more.

conditions = ["better", "worse", "richer", "poorer", "sickness", "health"]
while(True):
    for time in [time for time in life if time in conditions]:
        have = True
        hold = True
        love = True
        cherish = True
    if death:
        break

Google apps and Microsoft’s file format

Kevin Day, February 21st, 2008

Yet another post about a Google app and Joel on Software… anyways…

I don’t know if it’s my imagination, but Google Spreadsheets always seems to be a little bit faster and snappier each time I use it. Right now I’m working from a coffee shop with spotty internet connection, but it’s working as good as ever.

That made me think about how Google must know absolutely every tiny trick about optimizing Javascript in ways that no other company can even fathom. It seems like a good parallel to how Joel says Microsoft was light-years ahead of most other software companies for knowing how to write the fastest code possible on the hardware of the time.

Maybe in 10 years we’ll see Google release their specifications for how they store and update a spreadsheet and complain about how complex it is. However it’ll be that way because it includes hacks to work around IE6’s bugs.

Wanted: Smarter Gmail filtering

Kevin Day, February 20th, 2008

A while ago when I first saw Gmail’s option for “Filter messages like these,” I thought that it would use Bayesian statistics to automatically determine which category an email falls into. I was disappointed that it just starts the filter menu with the “To” field pre-populated.

Joel Spolsky mentioned that his company’s software filters email that way and it works effectively.

It just seems like a lot more Google-like way of organizing things rather than filtering based on To/From addresses.

Car wrecks and high school physics

Kevin Day, February 15th, 2008

Did you realize that increasing your driving speed from 60 mph to 80 mph nearly doubles your kinetic energy? I never thought about it, but it’s simple KE = 1/2 mv^2.

Try doing billiard ball collision problems with double the energy and see how they go. Not that car accidents are elastic collisions, but that energy has to go somewhere.

PyGTK Hello World program for images

Kevin Day, February 2nd, 2008

I’m learning PyGTK to create GUIs with python, but it’s been slow going. My current application primarily uses images, but most of the beginning tutorials for PyGTK use buttons for their “hello world” applications. The PyGTK tutorial is thorough, but I find it easier to work with lots of smaller examples that aren’t as in-depth.

Below is my simple “hello world” application for working with images. When it runs, it displays the first image of a directory. When you click on the image, the next image in the directory is displayed. I kept it basic so that it would be easy to pick up on the PyGTK stuff without getting caught up in other details.

Here’s the code, and below it is a description:

#!/usr/bin/env python
# hello_world_image.py

import pygtk
pygtk.require('2.0')
import gtk

class image:
    def __init__(self):
        self.window = gtk.Window()
        self.event_box = gtk.EventBox()
        self.image = gtk.Image()
        self.image_num = 0
        self.image.set_from_file(self.make_image_name(self.image_num))
        self.event_box.connect("button_release_event", self.change_image)
        self.event_box.add(self.image)
        self.window.add(self.event_box)
        self.window.show_all()

    def make_image_name(self, image_number):
        return "photos/img_" + str(image_number) + ".jpg"

    def change_image(self, widget, event):
        self.image_num += 1
        self.image.set_from_file(self.make_image_name(self.image_num))
        return True

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    image()
    main()

To add an image to a PyGTK application, just create an Image widget with gtk.Image() and tell it where to find the image with the set_from_file method.

The trick is that Image widgets do not react to events (button clicks, key strokes, etc.) so you have to put the Image widget in an Event Box widget. The event box doesn’t take up space in the GUI; it’s just an invisible container that will emit a signal when an event occurs. In this application, the change_image callback function is bound to the event box, but it still operates directly on the Image widget.

For this program to work it assumes that the images are in a folder named “photos” which is in the same directory the application is running in. Also, the images have to be named sequentially: img_0, img_1, img_2, etc. This is how I plan to use the program, but you could just as easily read the contents of the folder into a list and use that for the image names.

Here’s my directory of images:
Image Directory

And here’s my working program:
Hello World w Images

If you’re wondering, it’s not my dog. It’s just an image I found on Flickr.

Hope this little program helps other people starting off with PyGTK.

Bring them the game to them and like score a lot of touchdowns

Kevin Day, February 1st, 2008

I’ve already broken my New Year’s Resolution to write a blog post every week, but I’ve been sick and haven’t had the energy to do anything blog-worthy.

I’m better now, and here’s something funny to make up for the lull. It’s from We The Robots, a comic I just discovered and is a close second to xkcd for my favorite online comic strip. Click on the image to see the full version if it’s too small to read.

We the robots

But seriously, if you relate to Chad Jeffries as well as I do, try finding a Toastmasters group in your area. It’s a fun way to improve your communication skills.