在 Windows 上用 Python 进行文学编程的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1267280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:58:51  来源:igfitidea点击:

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

pythonwindowsliterate-programmingnoweb

提问by JasonFruit

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.

我一直在尝试各种用 Python 进行文学编程的方法。我喜欢noweb,但我有两个主要问题:第一,它很难在 Windows 上构建,我在 Windows 上花费了大约一半的开发时间;其次,它要求我缩进每一块代码,因为它会出现在最终程序中——我在编写它时不一定知道。我不想使用 Leo,因为我非常喜欢 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
  1. 在 Windows 上运行
  2. 允许我在使用时设置块的缩进,而不是在写入时设置
  3. 仍然让我在 Emacs 中工作

Thanks!

谢谢!



Correction: nowebdoesallow me to indent later --- I misread the paper I found on it.

更正:noweb确实允许我稍后缩进——我误读了我在上面找到的论文。

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

默认情况下,notangle在扩展块时保留空格并保持缩进。因此,它可以与 Miranda 和 Haskell 等语言一起使用,其中缩进很重要

That leaves me with only the "Runs on Windows" problem.

这让我只剩下“在 Windows 上运行”的问题。

采纳答案by S.Lott

I did this:

我这样做了:

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

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.

您可以获得任意数量的 web/weave 产品,它们将帮助您一次性构建文档和代码。

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.

你可以——很容易——编写你自己的。从 RST 源中提取 Python 代码块并对其进行组装并不是一门高深的科学。实际上,我建议您编写自己的 Docutils 指令来从 RST 源文档中组装 Python 代码。

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

您通过 docutils rst2html(或 Sphinx)运行 RST 以生成最终的 HTML 报告。

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

您在同一个 RST 源上运行您自己的实用程序以提取 Python 代码块并生成最终模块。

回答by Matti Pastell

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.

我写了 Pweave http://mpastell.com/pweave,它旨在生成动态报告并使用 noweb 语法。它是一个纯 python 脚本,因此它也可以在 Windows 上运行。它不能解决你的缩进问题,但也许你可以为此修改它,代码真的很简单。

回答by Colonel Panic

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

社区中的事实上的标准是 IPython 笔记本。

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

Peter Norvig 演示解决旅行商问题的算法的绝佳示例:http: //nbviewer.ipython.org/url/norvig.com/ipython/TSPv3.ipynb

More examples listed at https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks

https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks 中列出的更多示例

回答by Arne Babenhauserheide

You could use org-mode and babel-tangle.

你可以使用 org-mode 和 babel-tangle。

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

这很有效,因为您可以将 :noweb-ref 赋予源代码块。

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

这是一个最小的示例:激活 org-babel-tangle,然后将其放入文件中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.

您还可以使用标题的属性来提供 noweb-ref。然后它甚至可以自动将多个源块连接成一个 noweb 引用。

Add :results outputto the #+begin_srcline of the second block to see the print results under that block when you hit C-c C-cin the block.

添加:results output#+begin_src第二个块的行以查看当您点击该块时该块下的打印结果C-c C-c

回答by Norman Ramsey

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

您可能会发现noweb 3更容易在 Windows 上构建。它被设计为比标准 noweb 更便携。

回答by Paul-AG

See also my last LP tool: http://code.google.com/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.

另请参阅我的最后一个 LP 工具:http: //code.google.com/p/nano-lp。它不需要特殊的输入格式,支持 Markdown/MultiMarkdown、reStructuredText、OpenOffice/LibreOffice、Creole、TeX/LaTeX,并且拥有超轻量级和干净的语法——不再需要晦涩难懂的文字程序。

回答by BobC

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

发现这个工具很有用:https: //github.com/bslatkin/pyliterate