473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

question about introspection using inspect module

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
Jul 21 '05 #1
4 2630
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

Jul 21 '05 #2
Fernando Perez <fp********@gmail.com> 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
Jul 21 '05 #3
Benjamin Rutt wrote:
Fernando Perez <fp********@gmail.com> 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

Jul 21 '05 #4
Benjamin Rutt <ru**@bmi.osu.edu> 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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: It's me | last post by:
I discovered the hardway what inspect.isclass() is doing. Consider this no brainer code: ### import inspect class Abc: def Hello(self): return
8
by: mirandacascade | last post by:
Where in the language would one find the intropsection capability to answer the question: what class am I in? Example: class ExistentialCrisis: def __init__(self, text): self.spam = text...
1
by: Andy Leszczynski | last post by:
Hi, I have a following problem. Let's say there is a module which could be imported or run as __main__. It is going to contain hundreds of classes, something like that: import moduleA from...
4
by: brad tilley | last post by:
How do I import a module and then ask it to show me its methods or other aspects about itself during execution? I'd like to do something such as this: import win32api print win32api.methods()...
3
by: Tool69 | last post by:
Hi, I would like to retrieve all the classes, methods and functions of a module. I've used the inspect module for this, but inside a given class (subclass of some other one), I wanted to...
12
by: Martin Drautzburg | last post by:
I would like to validate sql strings, which are spread all over the code, i.e. I run ("prepare") them against a database to see if it happy with the statements. Spelling errors in sql have been a...
12
by: thomas.guest | last post by:
I'm not making progress with the following and would appreciate any help. Here's an interpreted Python session. .... Traceback (most recent call last): File "<stdin>", line 1, in <module>...
7
by: yagyala | last post by:
Hi. I would like to be able to tell, at run time, how many parameters a function requires. Ideally I would like to be able to tell which are optional as well. I've tried looking at the functions...
4
by: Alex K | last post by:
Hi Guys, What would be the simplest way of enumerating all methods and members (including inherited) of a given object? Thank you. Alex
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.