Entries for April 2005
'switch' in Python and other stuff
So, Rob Malda (aka CmdrTaco) of Slashdot fame spoke here last night, which was interesting. It wasn't a big event, but the number of available seats in the room was in the single-digits. Some parts of the presentation were boring, but by the end the atmosphere was more interactive and casual. Anyway, on to the topic at hand.
Early yesterday morning I posted a recipe for a new way of making readable 'switch' constructions in the Python Cookbook. (For those not in the know, Python lacks a switch/case construct.) I also brought it up in the 'switch' statement discussion on python.devel, and between these two sources it eventually made its way to Daily Python-URL and comp.lang.python through other means. It seems to have been well-received among fellow Pythonistas compared to other alternatives, the most popular (before this point) being conditions used as keys in a dictionary where the values are lambdas (or named functions) containing the appropriate case suite. While that solution is clever, it's absolutely hideous in practice (even just to describe, as you can see). I listed all known alternatives in the discussion of the recipe itself, and someone posted a comment with an Exception-based solution as well.
The method employed in my recipe allows for syntax like:
# v is the 'switched' variable
for case in switch(v):
if case(1):
...
break # just like the real thing!
if case('two'):
...
# no break, fall-through works!
if case(100):
...
break
if case(): # used in place of 'default'
...
Another novel method I tested, but quickly discarded, was to use switch.__call__ in place of switch.match, which would allow for syntax like:
# v is the 'switched' variable
case = switch(v)
if case(1):
...
if case(2):
....
This saves some typing and indentation, but isn't nearly as readable in my opinion. The indentation doesn't match C's 'switch' construction, you can't use 'break' (although you could make a switch.exit function to use in place), and assignment is much less clear than the 'for ... in' construct used in the real recipe (even if neither adhere to semantic usage).
People generally have fun mucking around in Python, so I thought I'd post this little thought process on the heels of the recipe's introduction...
Oh yeah — I've got a new (Python) project up my sleeves, and it's turning out to be my best (and most marketable) one yet! Made a proof-of-concept last weekend, so I'm now in the process of refactoring the prototype code for deployment.
Freelancing for the Summer
Since it looks like I won't be in Cambridge for the summer, my current plan is to try my hand at freelancing for my whole income. This is actually kinda risky considering that I already end up scraping the bottom of the barrel when it comes time to pay tuition every semester, but I think I can make it work. During a three-week period last summer I spent a few hours each night doing graphic design, and managed to make more than $300 from it — and a lot of those were contests, so I worked on a lot of stuff with no reward. I'm pretty confident that if I devote all my time to freelancing this summer I can match the $400/week that would be provided by most other full-time jobs. I can even do some web programming if the need arises.
The hardest part is getting clients. About half of the income from graphic design last summer was from people coming to me by reference or reputation, the other half was from open contests. Ben has sent a few references my way just this semester, but I often do free work for campus locals. Sometimes it seems like everyone needs a logo or two. Patty and I have been discussing the idea of starting a design company, with her on photography and fashion and me on illustration and web design.
So I guess our first job is to get noticed.
If you know anyone interested, here's some of my work to take a look at:
- Portfolio (lots of web company logos from last summer)
- Windows XP icons
- Computer game graphics (used by Sea3D, free multiplayer Settlers of Catan)
More recently, here's what I've done in the past month. These images are pretty big, so your browser might resize 'em:
- Chauncey Peppertooth (a band logo)
- Linux Installfest flyers one, two, three, and desktop
As you can see, I specialize mostly in vector art, but I can probably be convinced to write some mean Python for money as well. I can be reached at exogen@gmail.com.
