Quantcast

Print docstrings to shell

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Print docstrings to shell

Gnarlodious-2
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
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Andre Roberge
On Tuesday, February 1, 2011 8:11:51 PM UTC-4, Gnarlodious wrote:

> 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

Try the following:

============test.py======
import pydoc

'''this is a test'''

class A(object):
        '''docstring'''
        pass

print(pydoc.help(__file__[:-3]))
=============

python test.py

André
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Gnarlodious-2
On Feb 1, 5:30 pm, André Roberge <[hidden email]> wrote:

> ============test.py======
> import pydoc
>
> '''this is a test'''
>
> class A(object):
>         '''docstring'''
>         pass
>
> print(pydoc.help(__file__[:-3]))
> =============
>
> python test.py


OK that works, but only if I cd into the folder of the script. If I
run it from ~ I get the same error. How to get around that prob?

-- Gnarlie
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Andre Roberge
On Tuesday, February 1, 2011 9:05:28 PM UTC-4, Gnarlodious wrote:

> On Feb 1, 5:30 pm, André Roberge <[hidden email]> wrote:
>
> > ============test.py======
> > import pydoc
> >
> > '''this is a test'''
> >
> > class A(object):
> >         '''docstring'''
> >         pass
> >
> > print(pydoc.help(__file__[:-3]))
> > =============
> >
> > python test.py
>
>
> OK that works, but only if I cd into the folder of the script. If I
> run it from ~ I get the same error. How to get around that prob?
>
> -- Gnarlie

=======
import pydoc
import os
import sys

'''this is a test'''

class A(object):
        '''docstring'''
        pass

_path, _file_name = os.path.split(__file__)
_module_name = _file_name[:-3]
sys.path.append(_path)
pydoc.help(_module_name)
=====

Note: I've included an underscore in variables names so that they would not appear.
Note 2: for some reason, which I do not understand, it shows the help twice (i.e. I have to hit "q" twice to make it go away).  Sorry that I can not help with this.

André
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Andre Roberge
On Tuesday, February 1, 2011 9:21:48 PM UTC-4, André Roberge wrote:
SNIP

>
> =======
> import pydoc
> import os
> import sys
>
> '''this is a test'''
>
> class A(object):
> '''docstring'''
> pass
>
> _path, _file_name = os.path.split(__file__)
> _module_name = _file_name[:-3]
> sys.path.append(_path)
> pydoc.help(_module_name)
> =====
>

Actually, one does not need to import pydoc; using help() without importing seems to work just as well (or as badly, as it displays it twice...)
André


> Note: I've included an underscore in variables names so that they would not appear.
> Note 2: for some reason, which I do not understand, it shows the help twice (i.e. I have to hit "q" twice to make it go away).  Sorry that I can not help with this.
>
> André

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Carl Banks-2
In reply to this post by Gnarlodious-2
On Feb 1, 4:11 pm, Gnarlodious <[hidden email]> wrote:

> 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
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Gnarlodious-2
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
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Print docstrings to shell

Steven D'Aprano-11
In reply to this post by Gnarlodious-2
On Tue, 01 Feb 2011 16:11:51 -0800, Gnarlodious wrote:

> 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".


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Loading...