question about introspection using inspect module

Discussion in 'Python' started by Benjamin Rutt, Jul 7, 2005.

  1. I'm trying to learn about introspection in Python. my ultimate goal
    is to be able to build a module "text database" of all modules that
    are in the sys.path, by discovering all candidate modules (I've
    already done that), importing all of them, and then introspecting on
    each module to discover its functions, globals and classes. But for
    now I am having a problem with the latter.

    I would like to import a module and figure out the names of its
    defined functions, globals, and classes. Here's my attempt, file
    foo.py, which has a single function, class, and global defined:

    #!/usr/bin/env python

    def somefunction(i):
    i = i + 1

    class someclass:
    def __init__(self):
    self.x = 0
    self.y = 1

    someglobal = 1.2

    if __name__ == "__main__": # when run as a script
    import foo
    import inspect
    from inspect import *
    isfuncs = filter(lambda x: re.match("^is", x) and x, dir(inspect))
    print isfuncs
    print filter(lambda x: re.match("some", x[0]) and x[0], getmembers(foo))
    for f in isfuncs:
    exec('print "trying %20s: ",; print getmembers(foo, %s)' % (f, f))

    the output of running it as a script is the following:

    ['isbuiltin', 'isclass', 'iscode', 'isdatadescriptor', 'isframe', 'isfunction', 'ismethod', 'ismethoddescriptor', 'ismodule', 'isroutine', 'istraceback']
    [('someclass', <class foo.someclass at 0x40058ddc>), ('somefunction', <function somefunction at 0x40066a74>), ('someglobal', 1.2)]
    trying isbuiltin: []
    trying isclass: [('someclass', <class foo.someclass at 0x40058ddc>)]
    trying iscode: []
    trying isdatadescriptor: []
    trying isframe: []
    trying isfunction: [('somefunction', <function somefunction at 0x40066a74>)]
    trying ismethod: []
    trying ismethoddescriptor: []
    trying ismodule: []
    trying isroutine: [('somefunction', <function somefunction at 0x40066a74>)]
    trying istraceback: []

    I was trying to use inspect.getmembers(foo, <PRED>) with an
    appropriate predicate ("is" function). it looks like I am able to
    discover that 'someclass' is a class, and that 'somefunction' is a
    function (and also a routine, apparently). However, I cannot seem to
    discover that 'someglobal' is a global. How to do so? Thanks,
    --
    Benjamin Rutt
     
    Benjamin Rutt, Jul 7, 2005
    #1
    1. Advertising

  2. Benjamin Rutt wrote:

    > I'm trying to learn about introspection in Python. my ultimate goal
    > is to be able to build a module "text database" of all modules that
    > are in the sys.path, by discovering all candidate modules (I've
    > already done that), importing all of them, and then introspecting on
    > each module to discover its functions, globals and classes. But for
    > now I am having a problem with the latter.


    I certainly don't want to discourage you from learning about python
    introspection, it's one of the most fun aspects of the language. But just as
    an FYI, the pydoc system already does much of what you have in mind, at least
    if I'm reading your description correctly:

    planck[/tmp]> pydoc -p 12345
    pydoc server ready at http://localhost:12345/


    Just point your favorite webbrowser to that URL (use any port number you want,
    and which isn't already in use).

    Cheers,

    f
     
    Fernando Perez, Jul 7, 2005
    #2
    1. Advertising

  3. Fernando Perez <> writes:

    > I certainly don't want to discourage you from learning about python
    > introspection, it's one of the most fun aspects of the language. But just as
    > an FYI, the pydoc system already does much of what you have in mind, at least
    > if I'm reading your description correctly:
    >
    > planck[/tmp]> pydoc -p 12345
    > pydoc server ready at http://localhost:12345/


    thanks, I'm aware of that actually, and seeing all the information
    available there was inspiring to me.

    what I am actually trying to do is to build a database of Python
    modules. so then later, I can write a tool in my favorite editor
    (Emacs) to invoke some forms of completion against this database
    (e.g. os.remov<TAB> or socket.<TAB> to see a list of all socket module
    definitions).

    I was browsing pydoc.py but at first glance was having trouble
    separating what in the code what is for the GUI, what is for the Web
    server, and what does the introspection. I have actually borrowed the
    ModuleScanner class already, to build the list of modules. It is just
    inspecting those modules further where I'm having trouble.

    thanks,
    --
    Benjamin Rutt
     
    Benjamin Rutt, Jul 7, 2005
    #3
  4. Benjamin Rutt wrote:

    > Fernando Perez <> writes:
    >
    >> I certainly don't want to discourage you from learning about python
    >> introspection, it's one of the most fun aspects of the language. But just
    >> as an FYI, the pydoc system already does much of what you have in mind, at
    >> least if I'm reading your description correctly:
    >>
    >> planck[/tmp]> pydoc -p 12345
    >> pydoc server ready at http://localhost:12345/

    >
    > thanks, I'm aware of that actually, and seeing all the information
    > available there was inspiring to me.


    OK, you never know :)

    > what I am actually trying to do is to build a database of Python
    > modules. so then later, I can write a tool in my favorite editor
    > (Emacs) to invoke some forms of completion against this database
    > (e.g. os.remov<TAB> or socket.<TAB> to see a list of all socket module
    > definitions).


    well, I have no idea if this will be of any use, but it might:

    http://cedet.sourceforge.net/

    I use their speedbar quite a bit, but it sounds from the description like they
    have some other fancier tools. I'd be quite curious to know if they play
    nicely with python (they mention C++ explicitly), and how much value they add.
    Let me know if you are familiar with them, or if you end up investigating
    these further.

    Cheers,

    f
     
    Fernando Perez, Jul 7, 2005
    #4
  5. Benjamin Rutt

    Mike Meyer Guest

    Benjamin Rutt <> writes:
    > what I am actually trying to do is to build a database of Python
    > modules. so then later, I can write a tool in my favorite editor
    > (Emacs) to invoke some forms of completion against this database
    > (e.g. os.remov<TAB> or socket.<TAB> to see a list of all socket module
    > definitions).


    The problem with that is that Python is dynamic, so the list of
    completions may change over time. Not very likely, I know, but still...

    Have you considered writing this tool in Python, with Pymacs <URL:
    http://pymacs.progiciels-bpi.ca/ >? That way, you could get the list
    at runtime with a dir(module).

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jul 8, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Smith

    Introspection at the module level?

    Roy Smith, Mar 6, 2004, in forum: Python
    Replies:
    6
    Views:
    295
    Roy Smith
    Mar 7, 2004
  2. Andy Leszczynski
    Replies:
    1
    Views:
    321
    Michele Simionato
    Apr 28, 2005
  3. Darran Edmundson

    smart logging and the "inspect" module ...

    Darran Edmundson, May 28, 2005, in forum: Python
    Replies:
    1
    Views:
    287
    George Sakkis
    May 28, 2005
  4. Tool69

    question with inspect module

    Tool69, Feb 20, 2007, in forum: Python
    Replies:
    3
    Views:
    283
    Gabriel Genellina
    Feb 21, 2007
  5. deluxstar
    Replies:
    5
    Views:
    876
    Terry Reedy
    Sep 25, 2010
Loading...

Share This Page