Python and Literate Programming

Tim Evans tre17 at student.canterbury.ac.nz
Thu Nov 4 07:36:04 EST 1999


"Philip H. Jensen" <pjensen at columbus.rr.com> writes:

> Greetings.  I'm a recent convert to Python, and wish I had looked into
> it years ago.  As I said in a posting on my local Linux UG's mailing
> list, "I like everything about Python, whereas there are many things
> about Perl that I don't like."
> 
> Incidentally, I'm also a booster for Modula-3, and of course Python
> has well-known and intentional similarities to M3.  But one of my other
> enthusiasms is Literate Programming, and I'm wondering about the
> possibilities for it in Python.
> 
> Documentation texts can be easily accomodated, as can pretty-printing.

Cross-referencing on the other hand you will probably have to abandon
(at least for most stuff).  Dynamic object oriented languages make it
practically impossible.

> But for me, the core of LP, which I use (e.g. in CWEB) even if I never
> run the program through CWEAVE, is >> factoring <<.
> 
> Here's how it might look in a Python-friendly form.
> 
> 	if not dictionary.has_key(word):
> 	    <Inform the user that |word| is not present in
> 		the dictionary, and take action as directed>
> 
> then elsewhere in the program:
> 
> 	<Inform the user that |word| is not present...>:
> 	    code...
> 

For just this functionality, you could try using the `noweb' tool.  It 
manages to not break pythons indentation much at all.  For example,
the input:

<<example outer block>>=
for p in parrots:
    <<complain if the parrot p is dead>>

<<check if the parrot p is alive>>=
if p.perched and not p.nailed:
    print "It's f****** stuffed it!"

Will actually be correctly turned into:

for p in parrots:
    if p.perched and not p.nailed:
        print "It's f****** stuffed it!"

I'm not sure if other tools will do this correctly.  Noweb has the
disadvantages that it's written in icon, and that its LaTeX output is
not great, but it isn't all bad.

> ***
> Now, personally I would like to see this in the language itself,
> but that seems unlikely.  What would be essential, though would be
> 
>   1) a hook whereby "import foo" would see if there was a "foo.pyw"
>      more recent than "foo.py", and if so, would invoke a pytangle
>      program, and
>   2) a means for file and line information to be passed along, so
>      that error messages would refer to the .pyw source.
> 
> Any interest?  If I myself were to work on patches along these
> lines, could they become candidates for inclusion in the mainstream
> source tree?
> 
>     (I have some other Python development ideas,
>      but will save them for later postings.)
>   Phil Jensen
>   pjensen at columbus.rr.com 

--
<half-way-through-an-all-nighter-to-finish-a-literate-
 -programming-cs-honours-projectly>-yours
Tim Evans




More information about the Python-list mailing list