Mega-dir() or my cool snippet of the day
Kevin Jacobs
jacobs at darwin.epbi.cwru.edu
Sun Jun 11 19:31:19 EDT 2000
Hi all,
I'm usually pretty quiet around here, but I feel like making a little
noise. Here is my cool snippet of the day, to enhance the behavior of the
somewhat anemic dir builtin. Very useful when used in conjunction with
rlcompleter.py. e.g.:
import readline, rlcompleter, megadir
rlcompleter.dir = megadir.megadir
readline.parse_and_bind("tab: complete")
megadir.py:
-------------------------------------------------------
# Not the most efficient implementation, but not bad for
# a 10 minute hack.
import types
def unique(seq):
out = []
if len(seq):
out.append(seq[0])
for i in range(1,len(seq)):
if seq[i] != seq[i-1]:
out.append(seq[i])
return out
def megadir(obj = globals()):
ldir = []
for b in getattr(obj, '__bases__', []):
ldir.extend(megadir(b))
if type(obj) == types.InstanceType:
ldir.extend( megadir(obj.__class__) )
ldir.extend(dir(obj))
ldir.sort()
return unique(ldir)
def test():
class A:
def a(): pass
class B:
def b(): pass
class C(A):
def c(): pass
class D(B,C):
def d(): pass
def a(): pass
d = D()
print 'dir =',dir(d)
print 'megadir =',megadir(d)
if __name__ == '__main__':
test()
--
-----------> Kevin Jacobs <-----------|-------> (216) 778-8487 <--------
S.A.G.E. Project Technical Coordinator | Department of Epidemiology
& System Administrator | & Biostatistics
Internet E-mail: jacobs at darwin.cwru.edu | Case Western Reserve University
----------------------------------------------------------------------------
More information about the Python-list
mailing list