用于打印输出的合理 python 源代码换行
我正在编写一个乳胶文档,需要排版大量的 python 源代码。 我正在使用 pygments (python 模块,而不是在线演示)将此 python 封装在乳胶中,效果很好除非是长单行的情况 - 它只是在页面之外继续。 我可以手动换行这些行,只是这对我来说似乎不是那么优雅的解决方案,而且我更喜欢花时间思考疯狂的自动化解决方案而不是重复性任务。
我想要的是某种处理 python 源代码的方法,将行换行到一定的最大字符长度,同时保留功能。 我玩过一些Python,最接近的是在最大行长度之前的最后一个空格中插入 \\\n
- 但是当然,如果这最终以字符串形式结束和评论,事情出了问题。 坦率地说,我不知道如何解决这个问题。
那么,是否有人知道可以处理源代码的模块或工具,这样任何行都不会超过一定的长度——或者至少是开始编写类似代码的好方法?
I am working on a latex document that will require typesetting significant amounts of python source code. I'm using pygments (the python module, not the online demo) to encapsulate this python in latex, which works well except in the case of long individual lines - which simply continue off the page. I could manually wrap these lines except that this just doesn't seem that elegant a solution to me, and I prefer spending time puzzling about crazy automated solutions than on repetitive tasks.
What I would like is some way of processing the python source code to wrap the lines to a certain maximum character length, while preserving functionality. I've had a play around with some python and the closest I've come is inserting \\\n
in the last whitespace before the maximum line length - but of course, if this ends up in strings and comments, things go wrong. Quite frankly, I'm not sure how to approach this problem.
So, is anyone aware of a module or tool that can process source code so that no lines exceed a certain length - or at least a good way to start to go about coding something like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想稍微扩展当前的方法,但使用 tokenize 模块标准库来确定在哪里放置换行符。 这样您就可以看到源代码的实际标记(COMMENT、STRING 等),而不仅仅是以空格分隔的单词。
以下是 tokenize 功能的一个简短示例:
You might want to extend your current approach a bit, but using the tokenize module from the standard library to determine where to put your line breaks. That way you can see the actual tokens (COMMENT, STRING, etc.) of your source code rather than just the whitespace-separated words.
Here is a short example of what tokenize can do:
我使用 LaTeX 中的
listings
包来插入源代码; 它具有语法高亮、换行等功能。将以下内容放在序言中:
该包具有用于内联代码的挂钩,包括整个文件,将其显示为数字,...
I use the
listings
package in LaTeX to insert source code; it does syntax highlight, linebreaks et al.Put the following in your preamble:
The package has hook for inline code, including entire files, showing it as figures, ...
我会在 NetBeans 等编辑器中检查重新格式化工具。
当您重新格式化 java 时,它会正确修复注释内部和外部的行长度,如果将相同的算法应用于 Python,它将起作用。
对于 Java,它允许您设置任何环绕宽度和一堆其他参数。 如果它本身不存在或作为插件存在,我会感到非常惊讶。
仅从描述中无法确定,但值得一试:
http://www.netbeans.org/features/python/" rel="nofollow noreferrer">http://www. netbeans.org/features/python/
I'd check a reformat tool in an editor like NetBeans.
When you reformat java it properly fixes the lengths of lines both inside and outside of comments, if the same algorithm were applied to Python, it would work.
For Java it allows you to set any wrapping width and a bunch of other parameters. I'd be pretty surprised if that didn't exist either native or as a plugin.
Can't tell for sure just from the description, but it's worth a try:
http://www.netbeans.org/features/python/