What's the best way to do literate programming in Python on Windows?

Question:

I’ve been playing with various ways of doing literate programming in Python. I like noweb, but I have two main problems with it: first, it is hard to build on Windows, where I spend about half my development time; and second, it requires me to indent each chunk of code as it will be in the final program — which I don’t necessarily know when I write it. I don’t want to use Leo, because I’m very attached to Emacs.

Is there a good literate programming tool that:

  1. Runs on Windows
  2. Allows me to set the indentation of the chunks when they’re used, not when they’re written
  3. Still lets me work in Emacs

Thanks!


Correction: noweb does allow me to indent later — I misread the paper I found on it.

By default, notangle preserves whitespace and maintains indentation when expanding chunks. It can therefore be used with languages like Miranda and Haskell, in which indentation is significant

That leaves me with only the “Runs on Windows” problem.

Asked By: JasonFruit

||

Answers:

I did this:

http://sourceforge.net/projects/pywebtool/

You can get any number of web/weave products that will help you construct a document and code in one swoop.

You can — pretty easily — write your own. It’s not rocket science to yank the Python code blocks out of RST source and assemble it. Indeed, I suggest you write your own Docutils directives to assemble the Python code from an RST source document.

You run the RST through docutils rst2html (or Sphinx) to produce your final HTML report.

You run your own utility on the same RST source to extract the Python code blocks and produce the final modules.

Answered By: S.Lott

You might find noweb 3 easier to build on Windows. It was designed to be more portable than standard noweb.

Answered By: Norman Ramsey

I have written Pweave http://mpastell.com/pweave, that is aimed for dynamic report generation and uses noweb syntax. It is a pure python script so it also runs on Windows. It doesn’t fix your indent problem, but maybe you can modify it for that, the code is really quite simple.

Answered By: Matti Pastell

You could use org-mode and babel-tangle.

That works quite well, since you can give :noweb-ref to source blocks.

Here’s a minimal example: Activate org-babel-tangle, then put this into the file noweb-test.org:

#+begin_src python :exports none :noweb-ref c
abc = "abc"
#+end_src

#+begin_src python :noweb yes :tangle noweb-test.py
def x():
  <<c>>
  return abc

print(x())
#+end_src

You can also use properties of headlines for giving the noweb-ref. It can then even automatically concatenate several source blocks into one noweb reference.

Add :results output to the #+begin_src line of the second block to see the print results under that block when you hit C-c C-c in the block.

See also my last LP tool: https://code.google.com/archive/p/nano-lp/. It does not requires special input format, supports Markdown/MultiMarkdown, reStructuredText, OpenOffice/LibreOffice, Creole, TeX/LaTeX and has super light and clean syntax – no more cryptic literate programs.

Answered By: RandomB

The de-facto standard in the community is IPython notebooks.

Excellent example in which Peter Norvig demonstrates algorithms to solve the Travelling Salesman Problem: https://nbviewer.org/url/norvig.com/ipython/TSP.ipynb

More examples listed at https://github.com/jupyter/jupyter/wiki

Answered By: Colonel Panic

Found this tool to be useful: https://github.com/bslatkin/pyliterate

Answered By: BobC