Anything Else

Wednesday, July 27, 2005

mdir for Python

One of the coolest thing about python is the power of dir and help commands, when working with a module you are not familiar with. Offlate I have been a bit annoyed by lack of information displayed by dir though, and that gave rise to mdir. This module tells you about the membets of the object, as well as their class/types, tells you if they are callable, and optionally lets you specify a match string. It can also print the values of variables if you want. Here are some examples:
>>> mdir.mdir(mdir, methodsOnly=True)
["_dec('m', 'mem', 'verbose')", "mdir('m', 'verbose', 'varsOnly', 'methodsOnly', 's')"]
>>> mdir.mdir(mdir, varsOnly=True)
['float:_VER', 'list:__all__', 'dict:__builtins__', 'NoneType:__doc__', 'str:__file__', 'str:__name__']
>>> mdir.mdir(mdir, varsOnly=True, verbose=True)
['float:_VER = 1.0', "list:__all__ = ['mdir']", 'dict:__builtins__ = "{\'IndexError\': <class __doc__ =" None'," __file__ =" 'C:\\\\Documents" __name__ =" 'mdir'"]
>>> class C:
... i = 1
... s = "asd"
...
>>> mdir.mdir(C)
['NoneType:__doc__', 'str:__module__', 'int:i', 'str:s']
>>> mdir.mdir(C, verbose=1)
['NoneType:__doc__ = None', "str:__module__ = '__main__'", 'int:i = 1', "str:s = 'asd'"]
>>> dir(C)
['__doc__', '__module__', 'i', 's']

Enjoy.

Labels: Python Programming Invented Here

If you find this post useful, please conside buying me a pizza!

0 Comments

<< Home