Get List of Classes

2006-06-26 Thread digitalorganics
Is there a method or attribute I can use to get a list of classes
defined or in-use within my python program? I tried using pyclbr and
readmodule but for reason that is dogslow. Thanks in advance

DigiO

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get List of Classes

2006-06-26 Thread Tim Chase
 Is there a method or attribute I can use to get a list of
 classes defined or in-use within my python program? I tried
 using pyclbr and readmodule but for reason that is dogslow.

Well, given that so  much in python is considered a class, the 
somewhat crude code below walks an object/module and emits 
details regarding what's going on.  I couldn't find any nice 
method for determining if a variable referenced a module other 
than checking to see if that item had both a __file__ and a 
__name__ attribute.  Likewise, the check for whether something 
is an object is a bit crude.


  def inspect(thing, name = '', indent=0):
... if hasattr(thing, __file__) and hasattr(thing, __name__):
... #assume it's a module
... print %sModule %s % (\t * indent, thing.__name__)
... for subthing in dir(thing):
... objname = ..join([name, 
subthing]).lstrip(.)
... inspect(eval(objname),
... objname, indent+1)
... elif isinstance(thing, object):
... print %s%s is an object % (\t * indent, name)
...
  import m1
  # m1 is a junk module that references module m2 and has
  # some junk classes in it
  inspect(m1, m1)
Module m1
 m1.M1Class is an object
 m1.M1ObjectClass is an object
 m1.__builtins__ is an object
 m1.__doc__ is an object
 m1.__file__ is an object
 m1.__name__ is an object
 Module m2
 m1.m2.M2Class is an object
 m1.m2.M2ObjectClass is an object
 m1.m2.__builtins__ is an object
 m1.m2.__doc__ is an object
 m1.m2.__file__ is an object
 m1.m2.__name__ is an object



You could also filter out builtin object properties by wrapping 
that last print statement in something like

if not name.startswith(_): print ...

which might cut down on some of the noise.

Just a few ideas.

-tkc




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get List of Classes

2006-06-26 Thread digitalorganics
Wow, more than I had asked for, thank you Tim!

I ended up doing this:

def isClass(object):
if 'classobj' in str(type(object)):
return 1
elif 'type' in str(type(object)):
return 1
else:
return 0
def listClasses():
classes = []
for eachobj in globals().keys():
if isClass(globals()[eachobj]):
classes.append(globals()[eachobj])
print eachobj
return classes

Tim Chase wrote:
  Is there a method or attribute I can use to get a list of
  classes defined or in-use within my python program? I tried
  using pyclbr and readmodule but for reason that is dogslow.

 Well, given that so  much in python is considered a class, the
 somewhat crude code below walks an object/module and emits
 details regarding what's going on.  I couldn't find any nice
 method for determining if a variable referenced a module other
 than checking to see if that item had both a __file__ and a
 __name__ attribute.  Likewise, the check for whether something
 is an object is a bit crude.


   def inspect(thing, name = '', indent=0):
 ... if hasattr(thing, __file__) and hasattr(thing, __name__):
 ... #assume it's a module
 ... print %sModule %s % (\t * indent, thing.__name__)
 ... for subthing in dir(thing):
 ... objname = ..join([name,
 subthing]).lstrip(.)
 ... inspect(eval(objname),
 ... objname, indent+1)
 ... elif isinstance(thing, object):
 ... print %s%s is an object % (\t * indent, name)
 ...
   import m1
   # m1 is a junk module that references module m2 and has
   # some junk classes in it
   inspect(m1, m1)
 Module m1
  m1.M1Class is an object
  m1.M1ObjectClass is an object
  m1.__builtins__ is an object
  m1.__doc__ is an object
  m1.__file__ is an object
  m1.__name__ is an object
  Module m2
  m1.m2.M2Class is an object
  m1.m2.M2ObjectClass is an object
  m1.m2.__builtins__ is an object
  m1.m2.__doc__ is an object
  m1.m2.__file__ is an object
  m1.m2.__name__ is an object



 You could also filter out builtin object properties by wrapping
 that last print statement in something like

   if not name.startswith(_): print ...

 which might cut down on some of the noise.
 
 Just a few ideas.
 
 -tkc

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get List of Classes

2006-06-26 Thread Maric Michaud
Le lundi 26 juin 2006 17:25, Tim Chase a écrit :
  I couldn't find any nice
 method for determining if a variable referenced a module other
 than checking to see if that item had both a __file__ and a
 __name__ attribute.
Why not :

In [8]: import types, sys

In [9]: isinstance(sys, types.ModuleType)
Out[9]: True

?
Seems rather explicit IMO.

-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get List of Classes

2006-06-26 Thread Tim Chase
 I couldn't find any nice method for determining if a
 variable referenced a module other than checking to see if
 that item had both a __file__ and a __name__ attribute.

 Why not :
 
 In [8]: import types, sys
 
 In [9]: isinstance(sys, types.ModuleType)
 Out[9]: True

Yes...this is the best way to do it.  I hadn't explored (or even 
noticed, before your reply) the types module, which seems to 
have a large toolset for doing exactly what I wanted to.  Thanks!

A good programming language has users asking how did I miss 
that? rather than why can't I make it do what I want?.  Yet 
another feather in Python's cap.

-tkc




-- 
http://mail.python.org/mailman/listinfo/python-list