用于打印输出的合理 python 源代码换行

发布于 2024-07-25 02:54:38 字数 482 浏览 10 评论 0原文

我正在编写一个乳胶文档,需要排版大量的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

≈。彩虹 2024-08-01 02:54:38

您可能想稍微扩展当前的方法,但使用 tokenize 模块标准库来确定在哪里放置换行符。 这样您就可以看到源代码的实际标记(COMMENT、STRING 等),而不仅仅是以空格分隔的单词。

以下是 tokenize 功能的一个简短示例:

>>> from cStringIO import StringIO
>>> from tokenize import tokenize
>>> 
>>> python_code = '''
... def foo(): # This is a comment
...     print 'foo'
... '''
>>> 
>>> fp = StringIO(python_code)
>>> 
>>> tokenize(fp.readline)
1,0-1,1:    NL  '\n'
2,0-2,3:    NAME    'def'
2,4-2,7:    NAME    'foo'
2,7-2,8:    OP  '('
2,8-2,9:    OP  ')'
2,9-2,10:   OP  ':'
2,11-2,30:  COMMENT '# This is a comment'
2,30-2,31:  NEWLINE '\n'
3,0-3,4:    INDENT  '    '
3,4-3,9:    NAME    'print'
3,10-3,15:  STRING  "'foo'"
3,15-3,16:  NEWLINE '\n'
4,0-4,0:    DEDENT  ''
4,0-4,0:    ENDMARKER   ''

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:

>>> from cStringIO import StringIO
>>> from tokenize import tokenize
>>> 
>>> python_code = '''
... def foo(): # This is a comment
...     print 'foo'
... '''
>>> 
>>> fp = StringIO(python_code)
>>> 
>>> tokenize(fp.readline)
1,0-1,1:    NL  '\n'
2,0-2,3:    NAME    'def'
2,4-2,7:    NAME    'foo'
2,7-2,8:    OP  '('
2,8-2,9:    OP  ')'
2,9-2,10:   OP  ':'
2,11-2,30:  COMMENT '# This is a comment'
2,30-2,31:  NEWLINE '\n'
3,0-3,4:    INDENT  '    '
3,4-3,9:    NAME    'print'
3,10-3,15:  STRING  "'foo'"
3,15-3,16:  NEWLINE '\n'
4,0-4,0:    DEDENT  ''
4,0-4,0:    ENDMARKER   ''
掐死时间 2024-08-01 02:54:38

我使用 LaTeX 中的 listings 包来插入源代码; 它具有语法高亮、换行等功能。

将以下内容放在序言中:

\usepackage{listings}
%\lstloadlanguages{Python} # Load only these languages
\newcommand{\MyHookSign}{\hbox{\ensuremath\hookleftarrow}}

\lstset{
    % Language
    language=Python,
    % Basic setup
    %basicstyle=\footnotesize,
    basicstyle=\scriptsize,
    keywordstyle=\bfseries,
    commentstyle=,
    % Looks
    frame=single,
    % Linebreaks
    breaklines,
    prebreak={\space\MyHookSign},
    % Line numbering
    tabsize=4,
    stepnumber=5,
    numbers=left,
    firstnumber=1,
    %numberstyle=\scriptsize,
    numberstyle=\tiny,
    % Above and beyond ASCII!
    extendedchars=true
}

该包具有用于内联代码的挂钩,包括整个文件,将其显示为数字,...

I use the listings package in LaTeX to insert source code; it does syntax highlight, linebreaks et al.

Put the following in your preamble:

\usepackage{listings}
%\lstloadlanguages{Python} # Load only these languages
\newcommand{\MyHookSign}{\hbox{\ensuremath\hookleftarrow}}

\lstset{
    % Language
    language=Python,
    % Basic setup
    %basicstyle=\footnotesize,
    basicstyle=\scriptsize,
    keywordstyle=\bfseries,
    commentstyle=,
    % Looks
    frame=single,
    % Linebreaks
    breaklines,
    prebreak={\space\MyHookSign},
    % Line numbering
    tabsize=4,
    stepnumber=5,
    numbers=left,
    firstnumber=1,
    %numberstyle=\scriptsize,
    numberstyle=\tiny,
    % Above and beyond ASCII!
    extendedchars=true
}

The package has hook for inline code, including entire files, showing it as figures, ...

游魂 2024-08-01 02:54:38

我会在 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文