Here is a full program.

""" List classes, methods, functions and function/method args
in a module

Created: Anand B Pillai

"""

from types import *
import inspect

def analyze_func(obj, method=False):

    if method:
        print 'Method: %s' % obj.__name__
    else:
        print 'Function: %s' % obj.__name__

    arginfo = inspect.getargspec(obj)
    args = arginfo[0]
    argsvar = arginfo[1]

    if args:
        if args[0] == 'self':
            print '\t%s is an instance method' % obj.__name__
            args.pop(0)
        print '\tMethod Arguments: %s' % ' '.join(args)
        if arginfo[3]:
            dl = len(arginfo[3])
            al = len(args)
            defargs = args[al-dl:al]
            print 'Default arguments:',zip(defargs, arginfo[3])

    if arginfo[1]:
        print '\t Positional Args Param: %s' % arginfo[1]
    if arginfo[2]:
        print '\t Keyword Args Param: %s' % arginfo[2]


def analyze_klass(obj):

    print 'Class: %s' % obj.__name__
    for name in obj.__dict__:
        item = getattr(obj, name)
        if type(item) is MethodType:
            analyze_func(item, True)



def classify(module):

    print 'Module: %s' % module.__name__

    for name in dir(module):
        obj = getattr(module, name)
        if type(obj) in (ClassType, TypeType):
            analyze_klass(obj)
        elif type(obj) is FunctionType:
            analyze_func(obj)


if __name__ == "__main__":
    import test
    classify(test)

On Thu, Mar 13, 2008 at 2:59 PM,  <[EMAIL PROTECTED]> wrote:
> In addition to all the nice tips provided earlier... my 0.2cents !
>
>  If your module has doc strings then you can use something like:
>
>  ~~~~~~~~~~~~~
>  import os
>  help(os)
>  ~~~~~~~~~~~~~
>
>  --
>  seShadri
>
>
>
>  -----Original Message-----
>  From: [EMAIL PROTECTED]
>  [mailto:[EMAIL PROTECTED] On Behalf Of Pradeep Kishore
>  Gowda
>  Sent: Thursday, March 13, 2008 2:41 PM
>  To: Bangalore Python Users Group - India
>  Subject: Re: [BangPypers] Finding methods in a python script
>
>  Ah! i forgot about the function parameter list.. Will look into it.
>
>  +Pradeep
>
>  On 3/13/08, Pradeep Kishore Gowda <[EMAIL PROTECTED]> wrote:
>  > On 3/13/08, Heshan Suriyaarachchi <[EMAIL PROTECTED]> wrote:
>  >  > Hi
>  >
>  > >    I am talking about a scenario like this
>  >  >
>  >  > class A:
>  >  >     def foo(var1,var2):
>  >  >         return 'Hello'
>  >  >     def echo():
>  >  >         return 'testing'
>  >  >
>  >  >
>  >  > class B:
>  >  >     def bar(car):
>  >  >          val = car
>  >  >         return car
>  >  >
>  >  > What I need is to find out the classes and the methods of each
>  > class with  > their parameters.
>  >  > i.e. class A
>  >  >           foo(var,var)
>  >  >           echo()
>  >  >
>  >  > when using dir() it does not show the methods and the method
>  parameters .
>  >
>  >
>  >
>  > ## foo2.py
>  >  class A:
>  >         pass
>  >
>  >  class ABC:
>  >         pass
>  >
>  >  def foo():
>  >         return 'Hello'
>  >
>  >  def bar():
>  >         return 899
>  >
>  >  PI = 22/7
>  >
>  >  ## print_methods.py
>  >  import types
>  >  import foo2
>  >
>  >  # In python everything is available as a dict  all =  foo2.__dict__
>  >
>  >  # run through the items and check whether they are classes are
>  > functions  for i in all.keys():
>  >         if type(all[i]) == types.ClassType:
>  >                 print i, 'is a class'
>  >         elif type(all[i]) == types.FunctionType:
>  >                 print i, 'is a function'
>  >
>  >  # of course this gives only the top level functions and classes.
>  >
>  >  # Traverse the Tree to get the classes and methods like you wanted.
>  >
>  >  # Pradeep
>  >
>  >
>  >  --
>  >
>  > Home - http://btbytes.com
>  >  Heart  - http://sampada.net
>  >  Yummy! - http://konkanirecipes.com
>  >
>
>
>  --
>  Home - http://btbytes.com
>  Heart  - http://sampada.net
>  Yummy! - http://konkanirecipes.com
>  _______________________________________________
>  BangPypers mailing list
>  BangPypers@python.org
>  http://mail.python.org/mailman/listinfo/bangpypers
>  _______________________________________________
>  BangPypers mailing list
>  BangPypers@python.org
>  http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
-Anand
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to