Nested dictionaries in Python

This isn’t exactly ground-breaking, but rather just a handy tip. If you’re looking to create nested dictionaries in Python, like a “multi-dimensional hash,” or just a “dict of dicts,” there is a very simple method. All you need to do is use the defaultdict class in the collections module. Defaultdict objects can specify what factory (method) to use when creating new elements in the dictionary. If you pass in “dict” as the argument, all members of your dictionary will themselves be dictionaries. Of course, you could pass in another type, such as list. For example, this will make a two-level dictionary:

>>> from collections import defaultdict
>>> double_dict = defaultdict(dict)
>>> double_dict['foo']['bar'] = 42
>>> double_dict['fun']['ball'] = 77
>>> double_dict['fun']
{'ball': 77}

What if you want an unlimited number of dicts within a dict? Well, all you need is a simple function to create a defaultdict(dict) object on the fly, like this:

>>> def make_infinite_dict():
 return defaultdict(make_infinite_dict)

>>> inf_dict = make_infinite_dict()
>>> inf_dict[1][2][3][4][5] = 100
>>> inf_dict[1][2][3][4][5]
100

Like I said, nothing magical, but it could come in handy!

  • email
  • Digg
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon
 

Green text, black background

I tend to do my development using vim, in a terminal window connected to a remote server that looks very much like the production environment. Thankfully, I am not the only one who works this way. Perhaps I’m not crazy after all?

Green text. Black background. I’ll tell you why right now. I’m an old school DOS guy. My first word processor was Wordstar and that’s the word processing program I came to associate with the fugue-like state of maximum productivity: the Zone. This is why I continue to favor colored text on a black background in my current favorite editor, Textmate. The coloring reminds me of an primal safe place where the tool is serving its purpose — to get the hell out of the way so I can go be exponentially more productive.

This is why, as engineers, we stick with something that works for us. This is why the ancient likes of vi and Emacs continue to flourish. Once we find a tool that works for us, once we’ve chosen that tool, it becomes ours and remains ours. It allows us to get foamy.

I’ve had similar experiences with Dreamweaver and other WYSIWYG tools, where they are just too helpful and end up jumbling up my carefully formatted code. To be honest, I never even really liked working in Eclipse, either. It’s just too distracting, and again, too “helpful” for my taste. But like Rands says, to each his own.

via Rands In Repose

  • email
  • Digg
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon
 

Rethinking “control” in software engineering

I just read a short but interesting article by Tom DeMarco on the concepts of metrics and control in software engineering. Here’s the bottom line that really resonated with me:

This leads us to the odd conclusion that strict control is something that matters a lot on relatively useless projects and much less on useful projects.

That might not sound intuitive at first, but it makes sense after reading what he has to say.

The article (PDF) is available here: http://www2.computer.org/cms/Computer.org/ComputingNow/homepage/2009/0709/rW_SO_Viewpoints.pdf.

  • email
  • Digg
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon
 

Actually making money with open source

No, I’m not talking about how to earn money by working for Canonical. This is a post about actually designing a new piece of currency using open source software, especially Python. A quick summary: The Netherlands wanted to commemorate its rich history in the field of architecture, so they held a contest for the creation of a new coin. Using a bunch of open source software and many hours of hard work, both on the artistic and engineering side of things, Stani Michiels was able to come up with a truly inspiring design. Please read his blog post for some great, large pictures and a detailed overview of the process he used.

  • email
  • Digg
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon
 

Python 2.6

If you’re a Python developer, you should really read about what’s new in Python 2.6, which was just released a few days ago. There are a number of significant changes and additions, so this is a long document, but it’s worth going through. Most importantly, 2.6 is a stepping stone to the upcoming 3.0 release, so it’s a great way to get used to the future of Python while still having the choice to use older syntax.

  • email
  • Digg
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon