Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Get __doc__ from main module in imported module.

Reply
Thread Tools

Get __doc__ from main module in imported module.

 
 
David
Guest
Posts: n/a
 
      02-11-2010
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?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-11-2010
* 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
 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      02-11-2010
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do directly imported variables behave differently than thoseattached to imported module? Dun Peal Python 10 05-03-2011 10:11 PM
Minor bug in tempfile module (possibly __doc__ error) James T. Dennis Python 9 05-11-2007 07:25 AM
"Variable ... is not imported..." using an imported variable from a module Volker Nicolai Perl Misc 9 07-04-2005 08:34 AM
Get importer module from imported module dody suria wijaya Python 5 02-08-2005 06:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57