Re: module docstring, documentation,anything? please note is the module type/object NOT some module

Discussion in 'Python' started by Maric Michaud, Jun 24, 2006.

  1. Le Samedi 24 Juin 2006 04:41, Jorge Vargas a écrit :
    > On 6/23/06, Jorge Vargas <> wrote:
    > > Hi everyone I have wasted 2 hours trying to figure out how to get a
    > > reference to the module object. that is the one returned by
    > >
    > > __import__ or when you call a module for name like
    > >
    > > >>> import sys
    > > >>> sys

    > >
    > > <module 'sys' (built-in)>



    In [16]: from types import ModuleType

    In [17]: print ModuleType.__doc__
    module(name[, doc])

    Create a module object.
    The name must be a string; the optional doc argument can have any type.

    In [18]: ModuleType is type(sys)
    Out[18]: True

    > >
    > > the reason I want this is that I want to introspec my module
    > >
    > > so I need some way to indentify what elements are inside the module, to
    > > execute something like:
    > >
    > > ""for all the classes defined in this module do <something>"

    >


    Like for any other object, use the __dict__ attribute :

    In [19]: sys.__dict__.items()[:3]
    Out[19]:
    [('setrecursionlimit', <built-in function setrecursionlimit>),
    ('getfilesystemencoding', <built-in function getfilesystemencoding>),
    ('stdout', <open file '<stdout>', mode 'w' at 0xa7ddc068>)]


    Also, you''ll need these :

    In [59]: from types import ClassType

    In [60]: import subprocess

    In [61]: subprocess.__dict__['Popen']
    Out[61]: <class 'subprocess.Popen'>

    In [62]: isinstance(subprocess.__dict__['Popen'], type)
    Out[62]: True

    In [63]: isinstance(subprocess.__dict__['Popen'], ClassType)
    Out[63]: False

    In [64]: ClassType # old-style class
    Out[64]: <type 'classobj'>



    putting all togetther :


    In [75]: def find_classes(mod) :
    ....: for k, v in mod.__dict__.iteritems() :
    ....: if isinstance(v, type) : print 'new style class : ', k
    ....: elif isinstance(v, ClassType) : print 'old style class : ', k
    ....:
    ....:

    In [76]: find_classes(subprocess)
    new style class : Popen

    In [77]: find_classes(sys)

    In [78]: find_classes(os)
    old style class : _Environ
    new style class : stat_result
    old style class : error
    new style class : statvfs_result


    > I found an ugly hack to the docs help(type(sys))


    This is not ugly, people used to do "type('')" rather than "import types;
    types.StringType", in prior versions of python (2.1).


    --
    _____________

    Maric Michaud
    _____________

    Aristote - www.aristote.info
    3 place des tapis
    69004 Lyon
    Tel: +33 426 880 097
    Maric Michaud, Jun 24, 2006
    #1
    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. Bruce Grubb
    Replies:
    0
    Views:
    380
    Bruce Grubb
    Apr 27, 2004
  2. bdb112
    Replies:
    3
    Views:
    251
    Chris Rebert
    Mar 9, 2009
  3. Dr. Phillip M. Feldman

    How to create a docstring for a module?

    Dr. Phillip M. Feldman, Dec 6, 2009, in forum: Python
    Replies:
    13
    Views:
    549
    Albert van der Horst
    Dec 18, 2009
  4. Arnaud Delobelle
    Replies:
    0
    Views:
    184
    Arnaud Delobelle
    Sep 15, 2011
  5. Roman Ziak

    how to make a post-it type note ?

    Roman Ziak, Mar 29, 2006, in forum: Javascript
    Replies:
    2
    Views:
    78
    Roman Ziak
    Mar 30, 2006
Loading...

Share This Page