Skip to main content


Showing posts from November, 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() =…

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.


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 tha…

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 c…

Misspelled Variables

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

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

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/xauthThen I ran:rm /home/jj/.Xauthority
/etc/rc.d/sshd restartTo login from my Mac, I ran:ssh -YA jj@! xterm now works!