Archive for July, 2007

Douglas Crockford’s Javascript Pattern

1

I keep forgetting this and having to look for it, so I’m recording it here for easy future access:

myModule = function () {

    var myPrivateVar;
    var myPrivateMethod = function () { }

    return  {
        myPublicProperty: "something",
        myPublicMethod: function () { }
    };
}();

Wanted: Your Old Phone

0

As you may know I treated my cell phone to a 45 minute swim, which, surprisingly, rendered it dead. As a result I’m now sporting a blackberry and a sidekick, the latter strictly for use as a cell phone (no web access). While the extra weight of these two is keeping me in shape, I’m thinking I could axe one of them and go small.

If you have a smallish phone that’ll work with T-Mobile that you don’t need let me know.

Pownce Invites Available

4

I have a few invites for Pownce, if you’re interested (and if I know you) let me know and I’ll invite you.

Alright, I want an iPhone

3

I played with an iPhone yesterday. After my experiences with the MacBook Pro I was prepared to dislike it. I was hoping to dislike it.

I like it. Dammit. It’s really very nice.

What sold me immediately was the display. It’s beautiful, very readable, almost like paper. When it displays buttons you almost feel like they’re real physical buttons.

The pinch-zoom fanciness is nice and works. The keyboard I’m not into; I fat fingered every sentence. The camera was a lot lower quality than I expected. The controls generally are not intuitive, although they’re miles better than most other phones.

The thing that hits you is that it has a real browser. Not a crappy toy browser, a real browser capable of displaying just about any site. The zoom-in zoom-out works surprisingly well, and overall the web is quite usable.

This really is new. The sidekick has a workable browser; every other phone I’ve tried is a joke. Web browsing on the iPhone is usable almost to the point you can forget you’re on a mobile phone.

The monthly fees are just silly. I’m paying $40 to Tmobile for 1000 minutes / month. I can get unlimited internet on the sidekick for another $20. I’d have to pay well over $100/month for the equivalent with the iPhone.

Anyway, I’m sold. As soon as it drops a few hundred dollars in price, becomes usable on any 3G network, and loses the ridiculous monthly pricing, I’m getting one.

Baby Rayyan Update and Pictures

0

Baby and mother are doing well, we’re back home. Family and friends have been coming to visit, it’s been great to see everybody.

He has a name now: Rayyan Behrooz Darugar.

Me and Roohi with Rayyan

Initially he looked like a round Mr. Magoo. He’s starting to look human now.

In other news, took the boys to the pool. Jian can almost swim the width of the pool now, and Kamran’s doing quite well too. I’ll probably get them some lessons this summer; in 3 months they’l be swimming much better then me.

Pictures on flickr.

Sweet Sleep

1

I went to take a “nap” at about 10:30pm last night in preparation for the night’s baby changing duty. I woke up about 3 minutes later to find daylight.

Sleep is a sweet, sweet thing.

Simple Multi-Dimensional Dictionaries in Python

8

I needed several multi-dimensional hashes (or dictionaries as python calls them) and got tired of the long way of doing it, so I ended up coding a simple solution. Turns out this is already solved in Python 2.5 with defaultdict, but I didn’t know that at the time and this ended up being simple and small, so I’m sticking with it for now.

A multi-dimensional dictionary (MDD from here on) is a dictionary whose values are themselves dictionaries. For example, you could store car details in a MDD such that the first dimension is the make and the inner dimension is the model: car_details['Make']['Model']

car_details['Ford']['Mustang'] = "something"
car_details['Toyota']['Corolla'] = "something else"

The class that allows us to simplify building MDDs is a dictionary with a default. This class returns a default value when you lookup a key that’s not set in the dictionary. So if the default is “x” and you lookup a random key that’s not set in the dict you get “x” back.

We can create multi-dimensional dictionaries with this by setting the default value for the outer dimensions to return a dictionary. This way you can forget about the check-if-key-exists-set-if-not business for the outer dimensions and deal only with the innermost dimension.

Here’s my implementation of the dictionary with default. Python 2.5 has this built in with defaultdict, and there’s a recipe on the Python cookbook that may be a better implementation (I haven’t looked at it very much):

class Ddict(dict):
    def __init__(self, default=None):
        self.default = default

    def __getitem__(self, key):
        if not self.has_key(key):
            self[key] = self.default()
        return dict.__getitem__(self, key)

Not a whole lot that’s interesting there. The only thing to pay attention to is that you define the default as a function that creates a value as opposed to the value itself. This seems a little odd but makes sense if you think of how assignment works with arrays and hashes – namely that if we used self[key] = self.default instead of self[key] = self.default() we’d end up with all of our default keys pointing to the same array or hash:

>>> x = []
>>> y = x
>>> y.append('a')
>>> x
['a']

Ok, now to declare and use the MDD:

>>> car_details = Ddict( dict )
>>> car_details['Ford']['Mustang'] = "red"
>>> car_details['Ford']['Taurus'] = "blue"
>>> car_details['Toyota']['Corolla'] = "white"
>>> car_details
{'Toyota': {'Corolla': 'white'}, 'Ford': {'Mustang': 'red', 'Taurus': 'blue'}}

Here are a couple of more examples:

by_user = Ddict( list )   # 1D dictionary with array values
by_user_cmd = Ddict( lambda: Ddict( list ) )     # 2D dictionary with array values
user_totals = Ddict( lambda: Ddict( lambda: 0.0 ) )    # 2D dictionary with float values
user_cmd_totals = Ddict( lambda: Ddict( lambda: Ddict( lambda: 0.0 ) ) )    # 3D dictionary with float values

The lambda business, btw, is simply a way of turning what comes after it into a function. Instead of lambda: Ddict( list ) you could write a function that returns a Ddict( list ) using def, but that’s be more work and longer.

We Have Baby

2

Our third son was just born. Happy and healthy, no name yet. Will write more later.

Perlbal vs. Lighttpd for static content serving?

1

I’m thinking of running Perlbal for load balancing, and I see that it can also act as a Web server for static content. So I’m wondering: for static content, is it better to run Perlbal proxying to an http server (say lighttpd), or to have Perlbal serve the static content itself?

In production my static content will be on its own separate subdomain (static.something.com). Am I better off running lighttpd to serve this content (just lighttpd, no Perlbal in front of it) or will Perlbal (just Perlbal, no lighttpd behind it) do the job? Anybody know of any good Perlbal vs. Lighttpd benchmarks for static content serving?

Facebook Traction In Action: FaceDouble

3

My friend Alex created a Facebook app for his celebrity look-alike startup, FaceDouble. FaceDouble lets you submit photos via the Web, cell phone, or Facedouble to see what celebrity you look like, as well as vote on submissions from others. Take a look, it’s a lot of fun to play with. His Facebook app is at http://apps.facebook.com/facedouble .

Facedouble - celebrity look-alike

In all it took him 7 days to get the Facebook app created (he already had his regular site up and running). He submitted it for approval on Saturday, got it approved on Sunday, and today (Monday) he’s on top of the mobile and dating categories. He went from zero users to 500-odd user in about a day, and seems to be adding about a user per 1-2 minutes, although it slows down significantly during US night hours.

To be sure it’s not simply putting the app on Facebook that’s driving the usage – the stand-alone app itself has to be compelling. On Facebook, the app is tailored to take advantage of the social aspects – it’s integrated with Facebook albums and photos, allowing you to Facedouble your friends and send them the results all within the app.

His take on Facebook vs Myspace: he spent close a month getting his Myspace page working “fighting the formatting – you spend all your time trying to work around the restrictions and road-blocks”. He’s not terribly pleased with the results – he had to create a bot to accept all the friend requests, and the take-up is not nearly as fast as with Facebook. Part of the problem – “the Myspace users don’t understand you can use the product as opposed to just look at the page.”

He’s quite happy with the Facebook API:

Facebook’s done a smart job with the APIs – they’re giving you exactly what you need to get your work done. The combination of Flash, iframes, and FBML gives you just enough to let you do anything you want to. There hasn’t been a single situation where I’ve run into a roadblock that there wasn’t a reasonable solution for.

He’s also pleased with another aspect of FBML:

You get the Facebook look and feel without effort. Your app feels integrated into Facebook, as opposed to a separate, disjoint app.

He does have some concerns about the unchecked growth of apps on Facebook. From his post on the “Dear Facebook: Stop Becoming Myspace” group:

Facebook applications aren’t inherently bad, they just don’t have enough peer review, and the novelty is making many of my friends expose me to content that isn’t interesting at all. It’s similar to all garbage I get from those same friends over email. I came across this site:

http://www.facereviews.com

The facebook directory is just garbage and is so full of junk now, that its lost its value. I’m probably only going to add apps that have been reviewed properly by third parties. (I use the same strategy before I install any s/w on my desktop computer too, same concept really)

I’ll be posting updates on how the app grows. Let me know if you have particular questions or areas of interesting and I’ll look into them.

Now he just needs to create an iPhone app to get the maximum buzz!