In 2008, over one thousand people experienced the world’s first virtual web conference. Together, we created a new type of conference that is environmentally-friendly, affordable, and interactive.

In 2009, we are going to take it one step further.


Python, the learn-at-home language

One of the things that I love about Python is that it has all the documentation you ever need (all right, almost) in the code itself. Many moons ago, the very first framework I wrote in ActionScript (Flash 5) used the same technique by placing documentation on the activation objects of functions (and it would be cool to see that practice make a comeback in AS3.)

In Python, to find out what properties an object has, you just ask for a listing. The following, for example, shows you all properties and methods on the os module.

import os
dir(os)

In fact, I was doing this just today as I wanted to find out which functions are available for working with folders for the automatic restore feature I'm building for my Google App Engine backup solution.

Of course, that brings back a lot of items.

But have no fear, because we have regular expressions. I've grown to loooooove regular expressions thanks to being finally forced to learn them and use them daily in Django. I'm probably don't write the most concise ones but I've gotten to the point where I use them all the time and they simplify my life to no end.

So, to see just the methods that have "dir" in them:

import re
r = r'(^.*?dir.*?$)'
rc = re.compile(r)
matches = map(rc.match, dir(os))
[x.groups() for x in matches if not x == None]

And Bob's your uncle.

(Oh yeah, and list comprehensions rock too!)

And the coolest thing is that since Python is interpreted, I'm doing all this in the excellent IPython shell and using the language to learn the language and as a reference.

The more I use Python, the more I love it. They really did everything right. And that success, no doubt, is firmly planted in the unerring focus and the core values instilled in the language by Guido -- you're a freakin' genius, dude, and it gives me no end of confidence in Google App Engine that you're on the project! :)

Creative Commons LicenseThe Python, the learn-at-home language article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.

Post Metadata

Date
August 6th, 2008

Author
Aral

Category


6 Comments


  1. Mike Thompson

    You may be interested in this alternative one liner:

    [item for item in dir(os) if "dir" in item]

    Keep up the interesting work you’re doing with GAE


  2. Oooh, of course, I still forget that strings are just lists of numbers (and, hey, I love my regular expressions) :)

    Muchas dankes! :)


  3. You may also try this one-liner in IPython:

    os.*dir*?

    A bit shorter :)

    Cheers,

    f


  4. Hey Fernando,

    Ooooh, I liiike! :) Doesn’t appear work in all situations (e.g., combined with lookup, etc.) but a very handy shortcut when peeking through the object hierarchy.

    Thanks very much for sharing.


  5. @Fernando: wow, “?” rocks. it’s one of IPython goodies, isn’t it?

    @Aral: you mentioned on Twitter that you like “inspect” (which I have not used yet). how / what are you using it for?


  6. @Uldis: yes, ‘?’ is an ipython goodie, amongst a bunch of others :) Try foo?? (two marks) for extra info.


Leave a Reply