question about introspection using inspect module

2005-07-07 Thread Benjamin Rutt
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)]
tryingisbuiltin:  []
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:  []
tryingisroutine:  [('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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about introspection using inspect module

2005-07-07 Thread Fernando Perez
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

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


Re: question about introspection using inspect module

2005-07-07 Thread Benjamin Rutt
Fernando Perez [EMAIL PROTECTED] 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.removTAB 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about introspection using inspect module

2005-07-07 Thread Fernando Perez
Benjamin Rutt wrote:

 Fernando Perez [EMAIL PROTECTED] 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.removTAB 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

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


Re: question about introspection using inspect module

2005-07-07 Thread Mike Meyer
Benjamin Rutt [EMAIL PROTECTED] 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.removTAB 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 [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list