Python Abuse 18.11.2003

2003-11-18

Opening a file when all you know is the module name

pycode:

#!/usr/bin/python

import os, sys

numargs = len(sys.argv)
if numargs == 1 or numargs > 2:
    print """Usage: %s modulename""" % sys.argv[0]
    sys.exit()

from twisted.python import reflect

obj = reflect.namedAny(sys.argv[1])

fileName = obj.__file__
if fileName[-1] == 'c':
    fileName = fileName[:-1]

os.system("open %s" % fileName)

This takes advantage of the twisted utility function 'namedAny' which takes a dotted name and returns a package, module, class, function or method. This is useful for doing things like:

./openit.py urllib2

When you just want to take a quick look at the source of a module without tediously figuring out where it is located.

This will only work on OS X, which has the "open" command, but could be changed to use the EDITOR environment variable instead.

posted at 14:09:52    #    comment []    trackback []
November
MoTuWeThFrSaSu
      1 2
3 4 5 6 7 8 9
10111213141516
17181920212223
24252627282930
Oct Dec

Nothing quite like the smell of abusive python code in the morning.

XML-Image Letterimage

© 2003, Donovan Preston