Making the Zen of Python more useful

Richie Hindle richie at entrian.com
Fri Apr 2 09:52:56 EST 2004


[Andrew]
> Any thoughts [on how to get the output of "import this" programmatically]?

Fresh from the "Well it works but you'd never use it" factory:

import os, tokenize, token
libDir = os.path.split(tokenize.__file__)[0]
thisPathname = os.path.join(libDir, 'this.py')
for tokenType, tokenString, (startRow, startCol), (endRow, endCol), line \
    in tokenize.generate_tokens(open(thisPathname, 'r').readline):
    if tokenType == token.STRING:  # It had better be the first string!
        print tokenString.decode('rot13')
        break
else:
    print "Someone edited this.py!"

-- 
Richie Hindle
richie at entrian.com





More information about the Python-list mailing list