Thursday, November 27, 2008

Python: Class Methods Make Good Factories

Alex Martelli explained something to me a while back. One of the best uses of class methods is as constructors. For instance, if you want to have multiple constructors, but don't want to rely on one method that simply accepts different sorts of arguments, then use different class methods. The datetime module does this; it has class methods like fromordinal and fromtimestamp to create new datetime instances.

My first thought was that you could just as well use standalone factory functions. However, he brought up a good point. If I use a factory function, the class name is hard coded in the factory function. It can't easily return an instance of some subclass of the class. That's not the case with class methods.

Let me show you what I mean:
class MyClass:

def __init__(self):
# This is the "base" constructor.

def one_constructor(klass, foo):
# This is one special constructor.
self = klass() = foo
return self

def another_constructor(klass, bar):
# This is another special constructor.
# ...

class MySubclass(MyClass):
# This does some necessary customizations.

obj = MySubclass.one_constructor('foo')
Here I am instantiating an instance of MySubclass, but I am using the class method one_constructor from the superclass as the constructor.

If you've followed me so far, then perhaps you can imagine why Java's "public static void main" sometimes makes sense for Python too.

Auto: Square Pistons

(Disclaimer: I am mostly ignorant of auto tech.)

Why must pistons be round? I'm guessing that it's because it's easy to machine something really accurately if its round, and there's probably also something to be said for even pressure distribution. However, I'm thinking that if you used a square piston with rounded corners, you could get a larger "cylinder" to fit in the same block without compromising the thickness of the walls.

Also, why must ports be round? I can imagine ports that are triangles with rounded corners. This could be used to tune how much air is allowed in or out as the piston is going up and down. This would be a tunable, just like a camshaft.

Friday, November 21, 2008


I tried out PC-BSD 7.0.1 under VMware Fusion on my MacBook.

From the guide:
PC-BSD is basically FreeBSD with [a modern version of KDE,] a nice installer, some pre-configuration, kernel tweaks, PBI package management, a couple pre-selected packages and some handy (GUI) utilities to make PC-BSD suitable for desktop use.
I worked on FreeBSD GUIs (both desktop and Web user interfaces) for five years. Let me tell you, I'm thankful that PC-BSD finally happened! For some reason, FreeBSD developers tend to either despise GUIs or own a Mac. Hence, it seemed to me that FreeBSD's GUI support actually got worse over the years. It's about time someone finally came along and "pulled an Ubuntu"!

Overall, I was pretty impressed. It reminds me of the early days of Ubuntu where you could see the potential, but you could also see some places that needed some polish. Here are some things I found worthy of note:

KDE looks really nice these days! It seemed a little unstable, but that could just be my setup.

I wasn't able to get the same resolution I can get in Linux under VMware, i.e. 1280x800. VMware has a ton of kernel modules for Linux that I'm guessing simply aren't available for FreeBSD. Hence, I was stuck at 1024x768.

The fonts don't look so hot on my laptop. The anti-aliasing looks wrong. There is some discussion about why this is the case here.

PC-BSD supports FreeBSD's normal packaging system and the ports system, but it also has a packaging system called PBI (Push Button Installer or PC-BSD Installer). These packages work a lot like installing a Windows application. You can find them by going to You download them and then double click to install them. Uninstalling them is a lot like uninstalling a program on Windows. They even get installed into a /Programs directory instead of integrated into the normal hierarchy.

The installer gives you the opportunity to install a bunch of these PBIs. They all tend to be large applications like Firefox and I even noticed Opera in the list. I installed pretty much everything except for Opera. I was surprised to see that it took 5.7 gigs of disk space.

In general, looking at / is a bit strange. Aside from /Programs, there's also /PCBSD and a few other surprises.

By default, the system comes with sshd installed and running, but a firewall blocking access to it (Packet Filter from OpenBSD). This is actually the opposite of Ubuntu which does not come with sshd installed, nor does it autoconfigure a firewall.

I don't use FreeBSD much these days, but if you're one of the hand full of people besides me who actually care about FreeBSD on the desktop, this is a really cool development :)

Thursday, November 20, 2008

VMware Euphoria

I've been playing around with VMware since about 2000, but I've never had a computer powerful enough to really run it. Yesterday, I bought another 1gig stick of RAM for my MacBook, which puts me at 2gigs. That's not a heck of a lot, but it's enough.

I now have OS X, Ubuntu, and NetBSD running full screen on different OS X Spaces. I setup VMware Fusion to allow Ubuntu to use both CPUs and 1gig of RAM, whereas I only allocated 1 CPU and 256mb of RAM for NetBSD. OS X does fine with whatever the other two don't use. Ubuntu now has enough horsepower that I can even play the video game I wrote at full speed.

With a simple hot key, I can be in OS X, Ubuntu, or NetBSD. Even better: I can shut the lid of my laptop, and all three suspend without crashing. They all share my Mac's wireless connection, which tends to be pretty stable. If something is giving me a hard time installing under MacPorts, I can just install it on Ubuntu.

Being a minimalist, I only have one computer, so it's kind of hard to play around with fringe OSs, which I used to love doing. That's about to change. Next up, MINIX 3 and pc-bsd!

Oh my gosh that's cool!!!

Tuesday, November 18, 2008

Misspelled Variables

Care to guess what happens when you execute the following PHP?
define('FOO', 'Hi');
It prints 'FO'.

I do believe PHP got this from Perl:
print FOO . "FOO"; # Prints FOOFOO
It works even if you're strict:
perl -w
use strict;
print FOO . "FOO"; # Prints FOOFOO
Ruby behaves differently depending on whether you try to print an undefined variable/method or an undefined attribute:
>> print a
NameError: undefined local variable or method `a' for main:Object
from (irb):1
>> print @a
nil=> nil
Python raises an exception:
>>> print a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
In a compiled language, these sorts of errors would be caught at compile time. However, a compiled language would never let me do something like:
>>> var_name = 'a'
>>> locals()[var_name] = 'yep'
>>> print a
This example is a bit contrived, but I've definitely done things like it.

Personally, I like the flexibility of a scripting language. I like it even more when there's a tool like pychecker that can catch these sorts of errors. However, just because a scripting language doesn't have a compilation step that can catch stupid spelling mistakes doesn't mean it should accept them at runtime. I'd much rather deal with an exception than spend half an hour fighting a bug caused by a spelling error!

As a general rule, I think software should fail fast rather than glossing over bugs that will surely cause trouble later. I can handle the exception if I need to, but what I can't handle is a silent bug.

Monday, November 17, 2008

AI: Thankful for Bad AI

Imagine if the first computers man was able to create worked in pretty much the same way the human brain works. Imagine if they were pretty decent at reasoning, and terrible at calculating things quickly without error. Image that instead of having a quest for artificial intelligence, we had a quest for a "really fast, really accurate data cruncher." It'd be a different world. It definitely makes me grateful that we have humans *and* computers, each very useful in their own way.

The question of whether computers can think is like the question of whether submarines can swim -- Edsger W. Dijkstra

NetBSD: X11 Forwarding over SSH

I installed NetBSD 4.0.1 under VMware Fusion 2.0.1 on my OS X 10.5 box, and I had a heck of a time getting X11 forwarding working. I was getting the sshd configuration slightly wrong. Anyway, on the server I edited /etc/ssh/sshd_config:
X11Forwarding yes
X11DisplayOffset 10
# X11UseLocalhost yes
XAuthLocation /usr/X11R6/bin/xauth
Then I ran:
rm /home/jj/.Xauthority
/etc/rc.d/sshd restart
To login from my Mac, I ran:
ssh -YA jj@
Viola! xterm now works!