Python Easter Eggs

Friday, December 1st, 2006

These are pretty funny, and the first one is an excellent definition of what “pythonic” means.

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

Python Full Text Search Script

Wednesday, August 9th, 2006

Many times in the past when I have been attempting to figure out how someone else’s code worked (or wasn’t working as was more often the case), I have wanted a way to perform full text searches of all the documents in a directory and its subdirectories. Unfortunately, Windows (being utterly lacking in all things helpful) fails to provide such a search mechanism.

Python to the Rescue!

So I wrote up this small little Python script which will recursively search all the text documents in a directory tree. By text documents I mean anything that isn’t binary, so it will search just about any kind of source code.

The syntax is very simplistic, with only two arguments, one of which is optional.

search.py [directory] searchstring

The search string should be in quotes if it contains anything but alphanumeric characters, and you should, of course, escape quotes in the string.

Example Usage

search.py "Hello World"
search.py include "$unknownvariable"
search.py "<table colspan=\"2\">"

Programmer Bragging Rights

Saturday, May 6th, 2006

Have you ever finished writing a huge program, and now you want to know how many lines of code you wrote? Well, about halfway through writing a Python script to do this, I discovered that one already comes with Python that will give you these statistics categorized by file extension.

You can find this script at Tools\Scripts\byext.py in your python installation. If you’re on linux, the script may not come with your distro’s packaged version, in a which case I’ve provided a zip of it.