Print docstrings to shell

G

Gnarlodious

Can I run a script in bash and print out its docstrings to the bash
shell? I tried this at the end:

print(help(__file__))

Runnig the script:
python ~/Sites/Sectrum/Harmonics.py

but all it spit out was:

no Python documentation found for '~/Sites/Sectrum/Harmonics.py'

However in the interactive shell it prints out the class structure
nicely. What I really want to see is this output in the bash window.

-- Gnarlie
 
C

Carl Banks

Can I run a script in bash and print out its docstrings to the bash
shell? I tried this at the end:

print(help(__file__))

Runnig the script:
python ~/Sites/Sectrum/Harmonics.py

but all it spit out was:

no Python documentation found for '~/Sites/Sectrum/Harmonics.py'

However in the interactive shell it prints out the class structure
nicely. What I really want to see is this output in the bash window.

The help() function prints the documentation itself itself (piping it
to a pager if possible). It doesn't return the help text.

If that's what you want, then probably the most foolproof way is:

help(sys.modules[__name__])

This'll work whether it's a module or script. If you just want to
print the documentation and bypass the pager, then I think something
like this will do it:

import pydoc
print pydoc.render_doc(sys.modules[__name__])


Carl Banks
 
G

Gnarlodious

Thank you for the help, I learned a few things. The André solution
renders the colors but needs q-q to quit. The Carl solution 1 prints
colors and requires q to quit. The Carl solution 2 prints colorlessly,
it looks good for exporting to a file. Everything I need.

-- Gnarlie
http://Gnarlodious.com
 
S

Steven D'Aprano

Can I run a script in bash and print out its docstrings to the bash
shell? I tried this at the end: [...]
However in the interactive shell it prints out the class structure
nicely. What I really want to see is this output in the bash window.

Use the Source, Luke :)

See what help() does, and modify it appropriately. See the site and pydoc
modules for more information.

But there's no need to reinvent the wheel. pydoc already does what you
want (or at least what I think you want). Just run "pydoc name" from
bash. So long as name is a module in the python path (in other words, so
long as "import name" will work), then pydoc will see it too. If you want
to write the output out to a file, use "pydoc -w name".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top