If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Monday, August 14, 2006

Better dir

The builtin dir command is very useful. However it does not help that much when an object has many attributes.

I have a little function called la in my $HOME/.pythonrc.py

def la(obj, key=None, ignore_case=1):
'''List all attributes of object'''
import re
if key:
if ignore_case:
flags = re.I
else:
flags = 0
func = re.compile(key, flags).search

else:
func = None
print "\n".join(filter(func, dir(obj)))


This prints the attributes of an object one per line and also gives you the ability to search for specific items.

>>> from user import la
>>> import wx
>>> la(wx, "open")
ART_FILE_OPEN
ART_FOLDER_OPEN
EVT_MENU_OPEN
ID_OPEN
OPEN
Process_Open
wxEVT_MENU_OPEN
>>>


(Sun Aug 27 14:03:24 JDT 2006: Added regular expression to "la")
Post a Comment

Blog Archive