I signed up for the holiday reminders service from ag.com, and I got this reminder in my email today. Funny funny.
Monthly Archives: June 2007
Using dictionaries rather than complex if-elif-else clauses
Lately, I’ve been using dictionaries as a dispatching mechanism. It seems especially elegant when I face some fairly elaborate switching logic.
For example, instead of:
if a == 1 and b == 1:
log("everything worked!")
commit()
elif a == 1 and b == 0:
log("a good, b bad")
report_that_b_failed()
else:
log("a failed")
report_that_a_failed()
Do this:
d = {
(1, 1): commit,
(1, 0): report_that_b_failed,
(0, 0): report_that_a_failed
}
k = (a, b)
f = d[k]
f()
This approach is also really useful when you face the need to change logic based on runtime values. You can imagine that d might be built after parsing an XML file.
How to have a better cygwin terminal
Cygwin is a wonderful tool. However, I don’t really like the default terminal. This article shows how to set up an rxvt terminal instead.
This is a screenshot of the default cygwin bash prompt:
Copying and pasting is not intuitive, you can’t stretch the box horizontally, and most importantly, it breaks the illusion that I’m on a Unix box. But a better terminal option exists! When you install cygwin, you can also install the rxvt package. After installing the rxvt package, try this in a DOS window:
C:\cygwin\bin\rxvt.exe -fg gray -bg MidnightBlue -fn "Lucida Console-14" -e bash --login -i
You should see a terminal like this:
As far as I can tell from my experience, this terminal acts exactly like a regular rxvt terminal on a linux box. I can highlight some text with my mouse, and then paste it by hitting either shift+insert or by hitting the middle mouse button. I can resize the terminal horizontally and vertically.
I set up a taskbar icon so that I can get that rxvt very quickly. The icon before the right-most icon will start an rxvt terminal for me.
Setting it up was really simple. I made a new shortcut by right-clicking on my desktop, then I copied this text:
C:\cygwin\bin\rxvt.exe -fg gray -bg MidnightBlue -fn "Lucida Console-14" -e bash --login -i
into the target field:
Then I hit OK, and then dragged the new shortcut into the taskbar, and that’s it!