473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Printing Docstrings Without Importing

I have a large project that is getting complex, and I would like to
print the docstrings without importing the modules. The only Python
utility I could find references is apparently defunct and hasn't been
updated in 4 years.

I don't care how spartan the output is - it could look exactly like
python's internal docstrings, for all I care. It would be a nice added
bonus if it printed to HTML, but not if it greatly increased the
interface complexity. But I don't want to have to import the module to
run it! I want to just have a function that I pass a list of filenames
to.

Does anyone know of such a utility?

Jul 30 '05 #1
6 3036
On 30 Jul 2005 11:08:29 -0700, "Kamilche" <kl*******@comcast.net> wrote:
I have a large project that is getting complex, and I would like to
print the docstrings without importing the modules. The only Python
utility I could find references is apparently defunct and hasn't been
updated in 4 years.

I don't care how spartan the output is - it could look exactly like
python's internal docstrings, for all I care. It would be a nice added
bonus if it printed to HTML, but not if it greatly increased the
interface complexity. But I don't want to have to import the module to
run it! I want to just have a function that I pass a list of filenames
to.

Does anyone know of such a utility?

No, but here's one I just created (not tested very much):

----< docstr2html.py >--------------------------------------------
# docstr2html.py
"""
Prints html to display docstrings of modules specified on command line,
with optional globbing and html page title default override.

Usage: [python] docstr2html.py [-title "some title"] (file_pattern)+

Note: If redirecting stdout, some windows versions require [python] to be explicit.
"""
import glob, time, compiler

# copied from cgi module:
def escape(s, quote=None):
"""Replace special characters '&', '<' and '>' by SGML entities."""
s = s.replace("&", "&amp;") # Must be done first!
s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")
if quote:
s = s.replace('"', "&quot;")
return s

def htmlhdr(title=None):
if title is None: title = 'doc strings as of %s'%time.ctime()
print '<html><head><title>%s</title></head><body>' %escape(title)

def htmltrlr():
print '</body></html>'

def module_file_docstr2html(modulesourcepath):
print '<br>'
print '<table border="2">'
print ' <tr bgcolor="#99FFFF"><th>%s</th></tr>' % escape(modulesourcepath)
try:
print ' <tr bgcolor="#99FF99"><td><pre>%s</pre></td></tr>' % escape(compiler.parseFile(modulesourcepath).doc)
except Exception, e:
print ' <tr bgcolor="#FFFFFF"><td><font color="#FF0000"><b>Error encountered:<br>%s</b></font></td></tr>' % escape(
'Exception %s: %s' % (e.__class__.__name__, e))
print '</table>'
if __name__ == '__main__':
import sys, os
args = sys.argv[1:]
if not args: raise SystemExit(__doc__)
if args[0] == '-title': args.pop(0); title = args.pop(0)
else: title = None
htmlhdr(title)
for fspec in args:
for path in glob.glob(fspec):
module_file_docstr2html(os.path.abspath(path))
htmltrlr()
------------------------------------------------------------------

Running it (first to get the help response, which is just the doc string ;-)

[20:04] C:\pywk\ut>python docstr2html.py

Prints html to display docstrings of modules specified on command line,
with optional globbing and html page title default override.

Usage: [python] docstr2html.py [-title "some title"] (file_pattern)+

Note: If redirecting stdout, some windows versions require [python] to be explicit.
Then generating the html:

[20:04] C:\pywk\ut>python docstr2html.py docstr2html.py
<html><head><title>doc strings as of Sun Jul 31 20:04:55 2005</title></head><body>
<br>
<table border="2">
<tr bgcolor="#99FFFF"><th>C:\pywk\ut\docstr2html.py</th></tr>
<tr bgcolor="#99FF99"><td><pre>
Prints html to display docstrings of modules specified on command line,
with optional globbing and html page title default override.

Usage: [python] docstr2html.py [-title "some title"] (file_pattern)+

Note: If redirecting stdout, some windows versions require [python] to be explicit.
</pre></td></tr>
</table>
</body></html>

You can use file specs like someplace\*.py (wild cards) and it should do all the files
and put it all in tables in the html output. Note that is uses all file specs as patterns,
and ignores without complaint any that don't match anything.

Regards,
Bengt Richter
Aug 1 '05 #2
This seems to scratch several people's itches.

Care to develop/maintain it ?

Regards,

Fuzzball
http://www.voidspace.org.uk/python

Aug 1 '05 #3
On 1 Aug 2005 06:50:23 -0700, "Fuzzyman" <fu******@gmail.com> wrote:
This seems to scratch several people's itches.

Care to develop/maintain it ?

Are you talking to me? ;-)

(My news server is having some problem. I saw my post on google groups
but my normal news client isn't seeing it.)

Assuming you are talking to me, there's a bug, naturally, in trying to
escape None as a doc string. It was twenty minutes of hacking and a half hour
of trying to choose html colors, so there's not that much there ;-)
But what did you have in mind? Javascript menu rollovers for popup docs of
functions and classes and methods etc? Full help info access? Optional pdf output?

That would take more than another hour, but I did fix the mentioned bug and put
a table of clickable module names at the top with the file date stamps and paths
so you can navigate down to the spcific module docstring output quickly if you have
a lot of them. Of course, I think I'd put styling in the header rather than hack
more raw html if I were to go another round.

I'll post the latest once I can see my postings in context with my own newsreader again.

Regards,
Bengt Richter
Aug 2 '05 #4
Fuzzyman wrote:
This seems to scratch several people's itches.

As I understand it it is something like generating "pythondoc" like javadoc?
Should be pretty easy to develop something a bit more polished than
Bengt's solution (or based on it of course) with maybe similar to
javadoc framesets for a navigational list etc.

But usn't there something like that from the docutils project (I must
admit I should have googled for it but have not have the time yet).
I was looking for something to but found only epydoc yet with yet
another of its own markup, docutils being something like a standard for
python (at least that's what I thought) would be really nice for it.
chris

sorry if i totally misunderstood the question...
Aug 2 '05 #5

Bengt Richter wrote:
On 1 Aug 2005 06:50:23 -0700, "Fuzzyman" <fu******@gmail.com> wrote:
This seems to scratch several people's itches.

Care to develop/maintain it ?
Are you talking to me? ;-)

(My news server is having some problem. I saw my post on google groups
but my normal news client isn't seeing it.)

Assuming you are talking to me, there's a bug, naturally, in trying to
escape None as a doc string. It was twenty minutes of hacking and a half hour
of trying to choose html colors, so there's not that much there ;-)
But what did you have in mind? Javascript menu rollovers for popup docs of
functions and classes and methods etc? Full help info access? Optional pdf output?

That would take more than another hour, but I did fix the mentioned bug and put
a table of clickable module names at the top with the file date stamps and paths
so you can navigate down to the spcific module docstring output quickly if you have
a lot of them. Of course, I think I'd put styling in the header rather than hack
more raw html if I were to go another round.

I'll post the latest once I can see my postings in context with my own newsreader again.


Hello Brengt,

Sorry - I know it was a very terse reply. The state of automatic API
documentation generating tools is not very good in Python. Epydoc is
the best - but *seems* to be unmaintained. Many people find they can't
use it, because it imports code. Your approach of compiling the code
and introspecting the objects seems like the best alternative.

Some people argue against automatic API documentation *anyway* - but if
you write your docstrings intending them to be useful then it can save
a lot of work. However for a medium sized project, something like
Epydoc that shows the relationship between objects and links the doc
pages appropriately, can be very useful.

This is obviously a lot more work than just generating output for a
single modules.

Anyway - it's a subject that comes up regularly, and I haven't looked
into all the issues. There was some discussion on doc-sig about it a
while back.

All the best,
Fuzzy
http://www.voidspace.org.uk/python
Regards,
Bengt Richter


Aug 3 '05 #6
On 1 Aug 2005 06:50:23 -0700, Fuzzyman <fu******@gmail.com> wrote:
This seems to scratch several people's itches.

Has anyone tried this doxygen filter: http://i31www.ira.uka.de/~baas/pydoxy/

I really like doxygen but am not sure if this is worth the trouble.

jw
Aug 3 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
1
by: Thomas Aanensen | last post by:
Hi all. I'm sending a class over a socket connection, and I want to use this class at the new location. The problem is that I get a "module blah does not exist" error. Is it possible for me...
4
by: Alex Hunsley | last post by:
Very very simple problem here, can't find out how to do it... I want to print something without a new line after it, so I can later output something right after it on the same line. If I do: ...
0
by: Michael Gorski | last post by:
In c# is there a way to print an image without viewing it? I have the location of the file, all I need to do is print the image with no interaction from the user
3
by: Yammy Hung | last post by:
How to print out an integer (int) in C language without using printf()? Thanks in advance!
2
by: LilBuh | last post by:
hi there :) i ve been looking for some time a way to print an html file from vb.net i came up with this code for printing and removing the header and footer from IE then once the printing is done...
6
by: Eric Lilja | last post by:
Hello, I have a the following priority_queue: priority_queue<pair<int, string pq; AFAICT, priority_queues doesn't support iterators. My question: is there a way to print its contents without...
3
by: stmfc | last post by:
how can a multithreaded code can be compiled succesfully without importing Thread class? the example below is taken from marcus green mock exam 1: public class Rpcraven{ public static void...
1
by: dolittle | last post by:
Hi, I'm new to flex and trying to understand a tutorial by reading the code. There is a line: var mics:Array = Microphone.names; Flex docs says that you need to import flash.media.Microphone...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.