Get List of Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • digitalorganics@gmail.com

    #1

    Get List of Classes

    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

  • Tim Chase

    #2
    Re: Get List of Classes

    > Is there a method or attribute I can use to get a list of[color=blue]
    > classes defined or in-use within my python program? I tried
    > using pyclbr and readmodule but for reason that is dogslow.[/color]

    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.

    [color=blue][color=green][color=darkred]
    >>> def inspect(thing, name = '', indent=0):[/color][/color][/color]
    .... 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(ob jname),
    .... objname, indent+1)
    .... elif isinstance(thin g, object):
    .... print "%s%s is an object" % ("\t" * indent, name)
    ....[color=blue][color=green][color=darkred]
    >>> import m1
    >>> # m1 is a junk module that references module "m2" and has
    >>> # some junk classes in it
    >>> inspect(m1, "m1")[/color][/color][/color]
    Module m1
    m1.M1Class is an object
    m1.M1ObjectClas s 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.M2ObjectC lass is an object
    m1.m2.__builtin s__ 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




    Comment

    • digitalorganics@gmail.com

      #3
      Re: Get List of Classes

      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:[color=blue][color=green]
      > > 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.[/color]
      >
      > 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.
      >
      >[color=green][color=darkred]
      > >>> def inspect(thing, name = '', indent=0):[/color][/color]
      > ... 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(ob jname),
      > ... objname, indent+1)
      > ... elif isinstance(thin g, object):
      > ... print "%s%s is an object" % ("\t" * indent, name)
      > ...[color=green][color=darkred]
      > >>> import m1
      > >>> # m1 is a junk module that references module "m2" and has
      > >>> # some junk classes in it
      > >>> inspect(m1, "m1")[/color][/color]
      > Module m1
      > m1.M1Class is an object
      > m1.M1ObjectClas s 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.M2ObjectC lass is an object
      > m1.m2.__builtin s__ 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[/color]

      Comment

      • Maric Michaud

        #4
        Re: Get List of Classes

        Le lundi 26 juin 2006 17:25, Tim Chase a écrit :[color=blue]
        >  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.[/color]
        Why not :

        In [8]: import types, sys

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

        ?
        Seems rather explicit IMO.

        --
        _____________

        Maric Michaud
        _____________

        Aristote - www.aristote.info
        3 place des tapis
        69004 Lyon
        Tel: +33 426 880 097

        Comment

        • Tim Chase

          #5
          Re: Get List of Classes

          >> I couldn't find any nice method for determining if a[color=blue][color=green]
          >> variable referenced a module other than checking to see if
          >> that item had both a "__file__" and a "__name__" attribute.[/color]
          >
          > Why not :
          >
          > In [8]: import types, sys
          >
          > In [9]: isinstance(sys, types.ModuleTyp e)
          > Out[9]: True[/color]

          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




          Comment

          Working...