Get __doc__ from main module in imported module.

Discussion in 'Python' started by David, Feb 11, 2010.

  1. David

    David Guest

    I have a module toolkit.py with some functions I use often. One of
    these functions displays a usage message (__doc__).

    def usage(messages=[], exit=-1):
    """Print the doc string as wells as any useful messages."""
    print(__doc__)
    for message in messages:
    print("\033[91m{}\033[0m".format(message))
    if exit >= 0:
    sys.exit(exit)

    I import this module into another module, with the doc string I want
    to display. However, calling usage prints toolkit's doc string
    (None), not the current module's doc string.

    How can I access the top level module's doc string from toolkit?
    David, Feb 11, 2010
    #1
    1. Advertising

  2. * David:
    > I have a module toolkit.py with some functions I use often. One of
    > these functions displays a usage message (__doc__).
    >
    > def usage(messages=[], exit=-1):
    > """Print the doc string as wells as any useful messages."""
    > print(__doc__)
    > for message in messages:
    > print("\033[91m{}\033[0m".format(message))


    Consider using a terminal control library such as 'curses' instead of embedding
    escape sequences directly in the text, since escape sequences vary from terminal
    to terminal.


    > if exit >= 0:
    > sys.exit(exit)
    >
    > I import this module into another module, with the doc string I want
    > to display. However, calling usage prints toolkit's doc string
    > (None), not the current module's doc string.


    The function doesn't know where it's called from.

    You have to tell it, or dynamically create a module-specific function.

    One way to tell it is to pass the current module as an argument.


    > How can I access the top level module's doc string from toolkit?


    Like

    import __main__
    print_usage( __main__ )

    with suitable definition of 'usage'.


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Feb 11, 2010
    #2
    1. Advertising

  3. David

    David Guest

    > > How can I access the top level module's doc string from toolkit?
    >
    > Like
    >
    >      import __main__
    >      print_usage( __main__ )
    >
    > with suitable definition of 'usage'.
    >
    > Cheers & hth.,
    >
    > - Alf


    Problem solved!
    Thanks for the other tips too.
    David, Feb 11, 2010
    #3
    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. dody suria wijaya

    Get importer module from imported module

    dody suria wijaya, Feb 7, 2005, in forum: Python
    Replies:
    5
    Views:
    382
    dody suria wijaya
    Feb 8, 2005
  2. James T. Dennis
    Replies:
    9
    Views:
    316
    Dennis Lee Bieber
    May 11, 2007
  3. Dun Peal
    Replies:
    10
    Views:
    440
    Chris Rebert
    May 3, 2011
  4. Volker Nicolai
    Replies:
    9
    Views:
    898
    Fabian Pilkowski
    Jul 4, 2005
  5. snakeinmyboot
    Replies:
    8
    Views:
    102
    David
    Aug 9, 2013
Loading...

Share This Page