[Python-il] Statically analyzing a Python module to find assignments

cool-RR cool-rr at cool-rr.com
Sat Apr 16 12:43:35 IDT 2011


On Fri, Apr 15, 2011 at 9:45 PM, cool-RR <cool-rr at cool-rr.com> wrote:

> Hello folks,
>
> I want to let people who write GarlicSim simpacks write an `__init__.py`
> module like this:
>
>     '''
>     This is the simpack's docstring.
>
>     Bla bla bla.
>     '''
>
>     name = 'Name of the simpack'
>
>     tags = ['list', 'of', 'simpack-tags']
>
>     __version__ = '0.9.3'
>
> And I want GarlicSim to be able to get all of these things: The docstring,
> the name, the tags and the version. And I want to show them in this widget<http://dl.dropbox.com/u/1927707/SimpackSelectionDialog.png>.
> But I want to do it without importing the module, because `__init__.py`
> might import the entire package which can be heavy. (I want to do this
> process for many heavy simpacks.)
>
> We can assume that the user doesn't do anything computationally trickier
> then a simple literal assignment.
>
> I heard that the `ast` module does things like this. But, I also want to be
> able to do this process on compiled files, e.g. `__init__.pyc` or
> `__init__.pyo`, and I don't know how to do this with the `ast` module.
>
> Can the `ast` module do this on both source and compiled files? How is it
> done? Otherwise, is there a more fitting tool than `ast`?
>
>
> (By the way, if anyone cares, I have recently open-sourced GarlicSim's GUI,
> so now GarlicSim is completely open-source.)
>
>
> Thanks,
> Ram.
>

I just thought of a crazy way to do this: Import the `__init__.py` module
while temporarily overriding the `__import__` hook so any imports made by
the `__init__.py` module will just return mock objects. You think it will
work?


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://hamakor.org.il/pipermail/python-il/attachments/20110416/dda3e834/attachment.htm>


More information about the Python-il mailing list