FAQ
Using Python 2.6 on ubuntu 10.04.
inspect module :
I want to 'inspect' a module and get a list of all
functions, classes and global variables in that module.

## A module has been imported, and we call `getmembers'
members = inspect.getmembers(mod)

## While iterating thru `members', we test to see
## if an object is defined in the module.
for m in members:
obj = m[1]
res = inspect.getmodule(obj)
## It appears that getmodule returns None for
## all but functions and classes.

Example, for a module name `mvcInstall', when a class
name `Install' that is defined in the module
is passed as an argument to inspect.getmodule, the
values returned is something like
"<module 'mvcInstall' from
'/home/tim/prj/cgi/libraries/python/mvcInstall.py'>"
Likewise for functions defined in the module.

** But ** when global variables such as strings, booleans,
integers are passed as an argument to getmodule, the
value returned is `None'.

What else can I do here?
thanks
--
Tim
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

Search Discussions

  • Rantingrick at Jul 4, 2011 at 7:40 pm

    On Jul 4, 1:11?pm, Tim Johnson wrote:
    Using Python 2.6 on ubuntu 10.04.
    inspect module :
    I want to 'inspect' a module and get a list of all
    functions, classes and global variables in that module.

    ## A module has been imported, and we call `getmembers'
    members = inspect.getmembers(mod)

    ## While iterating thru `members', we test to see
    ## if an object is defined in the module.
    for m in members:
    ? obj = m[1]
    ? res = inspect.getmodule(obj)
    ## It appears that getmodule returns None for
    ## all but functions and classes.

    Example, for a module name `mvcInstall', when a class
    name `Install' that is defined in the module
    is passed as an argument to inspect.getmodule, the
    values returned is something like
    "<module 'mvcInstall' from
    '/home/tim/prj/cgi/libraries/python/mvcInstall.py'>"
    Likewise for functions defined in the module.

    ** But ** when global variables such as strings, booleans,
    integers are passed as an argument to getmodule, the
    value returned is `None'.

    What else can I do here?
    thanks
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot comhttp://www.akwebsoft.com
    Well if you follow the python style guide (and most accepted styles
    for global notation) then it's a trial exercise. You don't even have
    to import anything!!! :)
    GLOBAL_STR = 'str'
    GLOBAL_FLOAT = 1.33333
    GLoBaL_Bs = ''
    dir()
    ['GLOBAL_FLOAT', 'GLOBAL_STR', 'GLoBaL_Bs', '__builtins__', '__doc__',
    '__name__', '__package__', 'item']
    for item in dir():
    if item.isupper():
    print 'Found Global!', item


    Found Global! GLOBAL_FLOAT
    Found Global! GLOBAL_STR

    ;-)
  • Tim Johnson at Jul 4, 2011 at 8:30 pm

    * rantingrick [110704 12:00]:
    On Jul 4, 1:11?pm, Tim Johnson wrote:

    Well if you follow the python style guide (and most accepted styles
    for global notation) then it's a trial exercise. You don't even have
    to import anything!!! :)
    GLOBAL_STR = 'str'
    GLOBAL_FLOAT = 1.33333
    GLoBaL_Bs = ''
    dir()
    ['GLOBAL_FLOAT', 'GLOBAL_STR', 'GLoBaL_Bs', '__builtins__', '__doc__',
    '__name__', '__package__', 'item']
    for item in dir():
    if item.isupper():
    print 'Found Global!', item
    Thanks for the reply: *but*

    dir(<targetmodule>) will also show globals from other modules imported
    by the target module. So I would need a way to distinguish between
    those imported and those defined in <targetmodule>
    print(dir(targetmodule)) =>
    ['Install', 'TestAddresses', '__builtins__', '__doc__',
    '__file__', '__name__', '__package__', 'chmod', 'consoleMessage',
    'cp', 'debug', 'erh', 'exists', 'halt', 'is_list', 'load',
    'makePath', 'mkdir', 'process', 'sys', 'traceback', 'usingCgi']
    where 'TestAddresses' is a member of an imported module and
    'usingCgi' is the only data variable defined in <targetmodule>

    regards
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Rantingrick at Jul 4, 2011 at 9:30 pm

    On Jul 4, 3:30?pm, Tim Johnson wrote:
    ? Thanks for the reply: *but*
    ? dir(<targetmodule>) will also show globals from other modules imported
    ? by the target module. So I would need a way to distinguish between
    ? those imported and those defined in <targetmodule>
    Okay, then do some processing on the source. You can use regexps or
    the module mentioned earlier by Chris.
  • Tim Johnson at Jul 4, 2011 at 9:59 pm

    * rantingrick [110704 13:47]:
    On Jul 4, 3:30?pm, Tim Johnson wrote:

    ? Thanks for the reply: *but*
    ? dir(<targetmodule>) will also show globals from other modules imported
    ? by the target module. So I would need a way to distinguish between
    ? those imported and those defined in <targetmodule>
    Okay, then do some processing on the source. You can use regexps or
    the module mentioned earlier by Chris.
    I think I'm making this unnecessarily complicated. I had used
    something like:

    from spam import TestAddresses ## just for grins

    Which is something that I almost never do in practice.

    Also, you pointed out the UC naming convention, which I was
    unacquainted with, being self-employed, self-taught and a one-man
    crew who doesn't spend as much time as he should reading PEPs.

    I'm going to go for the predicate test as second argument to
    getmembers with something like:
    def isdata(self,obj,name):
    """Check if an object is of a type that probably means it's data."""
    return (not (inspect.ismodule(obj) or inspect.isclass(obj) or
    inspect.isroutine(obj) or inspect.isframe(obj) or
    inspect.istraceback(obj) or inspect.iscode(obj)))
    and name.issupper()
    ## Untested code

    thanks again
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Steven D'Aprano at Jul 4, 2011 at 11:13 pm

    Tim Johnson wrote:

    dir(<targetmodule>) will also show globals from other modules imported
    by the target module. So I would need a way to distinguish between
    those imported and those defined in <targetmodule>
    Why would you want to do that? Importing *is* a definition in
    <targetmodule>.

    Consider these two code snippets:

    #1
    from math import pi

    #2
    import math
    tau = 2*math.pi
    del math


    Why do you think it is necessary to distinguish pi from tau? Both names are
    local to the current namespace.


    print(dir(targetmodule)) =>
    ['Install', 'TestAddresses', '__builtins__', '__doc__',
    '__file__', '__name__', '__package__', 'chmod', 'consoleMessage',
    'cp', 'debug', 'erh', 'exists', 'halt', 'is_list', 'load',
    'makePath', 'mkdir', 'process', 'sys', 'traceback', 'usingCgi']
    where 'TestAddresses' is a member of an imported module and
    You are mistaken. TestAddresses is *not* a member of an imported module. It
    is a member of the current module, which may or may not happen to point to
    the same object as the other module as well.

    'usingCgi' is the only data variable defined in <targetmodule>
    It seems to me that your approach here is unnecessarily complex and fragile.
    I don't know what problem you are trying to solve, but trying to solve it
    by intraspecting differences that aren't differences is surely the wrong
    way to do it.



    --
    Steven
  • Tim Johnson at Jul 4, 2011 at 11:26 pm
    * Steven D'Aprano [110704 15:18]:
    You are mistaken. TestAddresses is *not* a member of an imported module. It
    is a member of the current module, which may or may not happen to point to
    the same object as the other module as well.
    You are correct. I mispoke or misapplied. See my last post.
    'usingCgi' is the only data variable defined in <targetmodule>
    It seems to me that your approach here is unnecessarily complex and fragile.
    I don't know what problem you are trying to solve, but trying to solve it
    by intraspecting differences that aren't differences is surely the wrong
    way to do it.
    See my last post...
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Steven D'Aprano at Jul 4, 2011 at 11:33 pm

    Tim Johnson wrote:

    It seems to me that your approach here is unnecessarily complex and
    fragile. I don't know what problem you are trying to solve, but trying to
    solve it by intraspecting differences that aren't differences is surely
    the wrong way to do it.
    See my last post...

    Yes, but what are you actually *trying to do*? "Detecting data members" is
    not an end in itself. Why do you think you need to detect data members?


    --
    Steven
  • Tim Johnson at Jul 5, 2011 at 12:01 am

    * Steven D'Aprano [110704 15:48]:
    Tim Johnson wrote:
    It seems to me that your approach here is unnecessarily complex and
    fragile. I don't know what problem you are trying to solve, but trying to
    solve it by intraspecting differences that aren't differences is surely
    the wrong way to do it.
    See my last post...

    Yes, but what are you actually *trying to do*? "Detecting data members" is
    not an end in itself. Why do you think you need to detect data members?
    Steven, I'm building a documentation system. I have my own MVC framework
    and the goal is to have a documentation module for each project.

    Thanks again for the clarifications. I always learn a lot from
    you.
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Chris Angelico at Jul 5, 2011 at 12:08 am

    On Tue, Jul 5, 2011 at 10:01 AM, Tim Johnson wrote:
    ?Steven, I'm building a documentation system. I have my own MVC framework
    ?and the goal is to have a documentation module for each project.
    Is there a reason for not using Doxygen / Autodoc / etc, or at least
    something in the same style? They work from specially-formatted
    comments in the source code, rather than the compiled module object.

    ChrisA
  • Tim Johnson at Jul 5, 2011 at 2:01 am

    * Chris Angelico [110704 16:19]:
    On Tue, Jul 5, 2011 at 10:01 AM, Tim Johnson wrote:
    ?Steven, I'm building a documentation system. I have my own MVC framework
    ?and the goal is to have a documentation module for each project.
    Is there a reason for not using Doxygen / Autodoc / etc, or at least
    something in the same style? They work from specially-formatted
    comments in the source code, rather than the compiled module object.
    I want this as a component of my system. It will be bound to a
    relational data-structure-based 'core' that defines relationships
    between all files in the project.

    :) It's called *collateral*

    And yes, I have worked with comment-based systems, including my
    own, that worked with multi-language projects.

    Best regards
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Ian Kelly at Jul 5, 2011 at 4:28 am

    On Mon, Jul 4, 2011 at 6:01 PM, Tim Johnson wrote:
    Yes, but what are you actually *trying to do*? "Detecting data members" is
    not an end in itself. Why do you think you need to detect data members?
    ?Steven, I'm building a documentation system. I have my own MVC framework
    ?and the goal is to have a documentation module for each project.
    It sounds like what you really want is to detect the names *exported*
    by the module, then. Why not do it the same way Python does it? If
    the module defines an "__all__" attribute, then it is taken to be a
    sequence of strings which are the exported names. Otherwise, the
    exported names are taken to be all the names in the module dict that
    don't begin with an underscore.

    Cheers,
    Ian
  • Tim Johnson at Jul 5, 2011 at 3:30 pm
    * Ian Kelly [110704 20:37]:
    It sounds like what you really want is to detect the names *exported*
    by the module, then. i Yes!
    Why not do it the same way Python does it? If
    the module defines an "__all__" attribute, then it is taken to be a
    sequence of strings which are the exported names. Otherwise, the
    exported names are taken to be all the names in the module dict that
    don't begin with an underscore.
    :) Oh here we go again. Another python feature I didn't know about
    or have forgotten.

    Thanks very much for that.
    Good tip
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Grant Edwards at Jul 5, 2011 at 3:43 pm

    On 2011-07-05, Tim Johnson wrote:
    * Ian Kelly [110704 20:37]:
    It sounds like what you really want is to detect the names *exported*
    by the module, then. i Yes!
    Why not do it the same way Python does it? If
    the module defines an "__all__" attribute, then it is taken to be a
    sequence of strings which are the exported names. Otherwise, the
    exported names are taken to be all the names in the module dict that
    don't begin with an underscore.
    :) Oh here we go again. Another python feature I didn't know about
    or have forgotten.
    You could probably implement at least two more languages using nothing
    but Python features I don't know about or have forgotten. :)

    Yet I still manage to get a lot accomplished using Python.

    --
    Grant Edwards grant.b.edwards Yow! I don't know WHY I
    at said that ... I think it
    gmail.com came from the FILLINGS in
    my rear molars ...
  • Grant Edwards at Jul 5, 2011 at 2:11 pm

    On 2011-07-05, Chris Angelico wrote:
    On Tue, Jul 5, 2011 at 10:01 AM, Tim Johnson wrote:

    Steven, I'm building a documentation system. I have my own MVC
    framework and the goal is to have a documentation module for each
    project.
    Is there a reason for not using Doxygen / Autodoc / etc, or at least
    something in the same style? They work from specially-formatted
    comments in the source code, rather than the compiled module object.
    Because those specially-formatted comments are wrong.

    --
    Grant Edwards grant.b.edwards Yow! I have many CHARTS
    at and DIAGRAMS..
    gmail.com
  • Waldek M. at Jul 5, 2011 at 4:35 pm

    Dnia Tue, 5 Jul 2011 14:11:56 +0000 (UTC), Grant Edwards napisa?(a):
    Because those specially-formatted comments are wrong.
    ... because?
    Not in sarcasm mode; just curious why you don't like them.

    Br.
    Waldek
  • Steven D'Aprano at Jul 5, 2011 at 5:36 pm

    Waldek M. wrote:

    Dnia Tue, 5 Jul 2011 14:11:56 +0000 (UTC), Grant Edwards napisa?(a):
    Because those specially-formatted comments are wrong.
    ... because?
    Not in sarcasm mode; just curious why you don't like them.
    Because unless you are extremely disciplined, code and the comments
    describing them get out of sync. Quote:

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. "
    --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22




    --
    Steven
  • Chris Angelico at Jul 5, 2011 at 10:45 pm

    On Wed, Jul 6, 2011 at 3:36 AM, Steven D'Aprano wrote:

    Because unless you are extremely disciplined, code and the comments
    describing them get out of sync. Quote:

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. "
    --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22
    And yet, as I have found out by trying to embed V8 (Google's
    Javascript engine), those little comments can (a) prove the difference
    between a bug and a misfeature, and (b) be all the documentation there
    is. Okay, it's not QUITE the latter, but most things are not
    documented outside of the source, and I greatly appreciate those
    little comments!

    ChrisA
  • Waldek M. at Jul 6, 2011 at 6:00 am

    Dnia Wed, 06 Jul 2011 03:36:24 +1000, Steven D'Aprano napisa?(a):
    Because unless you are extremely disciplined, code and the comments
    describing them get out of sync. [...]
    True, but that gets far worse with external docs.
    Do you have in mind any better replacement?

    Br.
    Waldek
  • Grant Edwards at Jul 7, 2011 at 2:18 pm

    On 2011-07-06, Waldek M. wrote:
    Dnia Wed, 06 Jul 2011 03:36:24 +1000, Steven D'Aprano napisa?(a):
    Because unless you are extremely disciplined, code and the comments
    describing them get out of sync. [...]
    True, but that gets far worse with external docs. Do you have in mind
    any better replacement?
    You write code that's easy to read, and you provide the language with
    introspection capabilities that allow you do do things like write a
    program that will list the symbols exported by a module.

    --
    Grant Edwards grant.b.edwards Yow! Now that I have my
    at "APPLE", I comprehend COST
    gmail.com ACCOUNTING!!
  • Chris Rebert at Jul 8, 2011 at 12:36 am

    On Thu, Jul 7, 2011 at 7:18 AM, Grant Edwards wrote:
    On 2011-07-06, Waldek M. wrote:
    Dnia Wed, 06 Jul 2011 03:36:24 +1000, Steven D'Aprano napisa?(a):
    Because unless you are extremely disciplined, code and the comments
    describing them get out of sync. [...]
    True, but that gets far worse with external docs. Do you have in mind
    any better replacement?
    You write code that's easy to read
    Reminded me of this excellent presentation:

    "Uncomment Your Code"
    https://docs.google.com/present/view?id=ah82mvnssv5d_162dbgx78gw

    Cheers,
    Chris
  • Grant Edwards at Jul 5, 2011 at 10:39 pm

    On 2011-07-05, Waldek M. wrote:
    Dnia Tue, 5 Jul 2011 14:11:56 +0000 (UTC), Grant Edwards napisa?(a):
    Because those specially-formatted comments are wrong.
    ... because?
    In my experience, they're wrong because somebody changes the code and
    not the comments.
    Not in sarcasm mode; just curious why you don't like them.
    There've been too many times when I couldn't trust them.

    --
    Grant Edwards grant.b.edwards Yow! I'm totally DESPONDENT
    at over the LIBYAN situation
    gmail.com and the price of CHICKEN
    ...
  • Chris Rebert at Jul 4, 2011 at 9:03 pm

    On Mon, Jul 4, 2011 at 11:11 AM, Tim Johnson wrote:
    Using Python 2.6 on ubuntu 10.04.
    inspect module :
    I want to 'inspect' a module and get a list of all
    functions, classes and global variables in that module.
    You meant "first defined in" that module.
    <snip>
    Example, for a module name `mvcInstall', when a class
    name `Install' that is defined in the module
    is passed as an argument to inspect.getmodule, the
    values returned is something like
    "<module 'mvcInstall' from
    '/home/tim/prj/cgi/libraries/python/mvcInstall.py'>"
    Likewise for functions defined in the module.

    ** But ** when global variables such as strings, booleans,
    integers are passed as an argument to getmodule, the
    value returned is `None'.

    What else can I do here?
    Look at the names in the module's import statements using the `ast`
    module, and exclude those from the set of names defined in the module.
    Won't work for `from foo import *`, but that's bad practice and should
    be refactored anyway.

    Cheers,
    Chris
  • Tim Johnson at Jul 4, 2011 at 9:39 pm

    * Chris Rebert [110704 13:16]:

    What else can I do here?
    Look at the names in the module's import statements using the `ast`
    module, and exclude those from the set of names defined in the module.
    Won't work for `from foo import *`, but that's bad practice and should
    be refactored anyway.
    I'm completely new to the `ast' module, so I will have to research
    that one.
    Thanks for the tip
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 4, '11 at 6:11p
activeJul 8, '11 at 12:36a
posts24
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase