# [Python-ideas] Literate python?

Bruce Leban bruce at leapyear.org
Thu Mar 10 05:15:46 CET 2011

At the cost of an extraneous # at the beginning you can do something like
this:

#\def\py#1{} \py{
...python...
"""#}
...TeX...
\py{"""
...python...
#}

This isn't completely right since a } in a string or python comment will
mess it up. That can be handled by a slightly more complicated definition
which changes the catcodes of #, " and ' so that they in turn change the
definitions of }, \ and newline. I started to write this but it's
complicated so I'll leave it as an exercise. :-)  (If you can't figure it
out, I'll be happy to help.)

--- Bruce

On Wed, Mar 9, 2011 at 3:34 PM, Mike Meyer <mwm at mired.org> wrote:

> On Tue, 08 Mar 2011 21:01:25 -0500
> Terry Reedy <tjreedy at udel.edu> wrote:
>
> > On 3/8/2011 5:02 PM, Mike Meyer wrote:
> > > Wild idea, swiped directly from haskell/ghc:
> > >
> > > How about making the python interpreter just a little bit smarter,
> >
> > It already is ;-)
> > Though not exactly well known, expression statements that consist of a
> > literal (number or string) are ignored -- except for string literals in
> > docstring position (and then, they are attached as attributes, rather
> > than being in the code object.
>
> [Examples elided]
>
> > > If the first non-white-space character after the shebang line (if
> > > present) is a backslash, then the compiler ignores lines until it sees
> > > a line consisting of \begin{code} (which could be the first line),
> > > then compiles lines until it sees a line consisting of \end{code},
> > > after which it switches back to searching for \begin{code}.
> > So this appears unnecessary. Just use quotes.
>
> That works fine for the '> ' variant. But the point of the \...{code}
> version is that the resulting source could be run through both lpython
> and TeX without preprocessing. How does using quotes play with TeX?
>
> > The main problems is that program editors are generally not smart enough
> > to do auto text wrapping within multiline strings.
>
> Emacs MMM-mode
> (http://www.xemacs.org/Documentation/packages/html/mmm.html) should
> work for this - or the two variants I suggested (switching from Python
> to TeX mode dynamically).
>
>
>        <mike
> --
> Mike Meyer <mwm at mired.org>
> http://www.mired.org/consulting.html
>
> O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110309/f91516fe/attachment.html>