在PyCharm中的Python Heredoc行上删除了尾随空格 [英] Trailing spaces removed on Python heredoc lines in PyCharm

查看:116
本文介绍了在PyCharm中的Python Heredoc行上删除了尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PyCharm社区3.4.1中使用带有unittest2的Python 2.7.我需要将CLI命令的文本输出与字符串的内容进行匹配,以进行自动测试.

I'm using Python 2.7 with unittest2 within PyCharm community 3.4.1. I need to match the textual output of a CLI command with the contents of a string for an automated test.

此命令的输出通常在行尾有尾随空格;如果我在heredoc中用于存储预期文本的行的末尾添加空格,它们会在编辑器中被神秘地删除,并且不会放入文件中.要解决此问题,我必须拆分我的Heredoc,并用空格重新组合它.这非常丑陋,但这是我可以使其正常工作的唯一方法.

The output of this command frequently has trailing spaces at the end of lines; if I add spaces to the ends of the lines in the heredoc that I'm using to store the expected text, they mysteriously get removed in the editor and don't make it to the file. To work around this I have had to split my heredoc and recombine it with spaces; this is very ugly, but it is the only way I can get it to work.

我曾尝试使用Google搜索和寻找解释,但没有找到任何答案.我怀疑这可能是PyCharm的自动格式设置感到困惑,并且将其视为一行Python代码,可以安全删除尾随空格.

I've tried googling and searching for explanations, but I found none; I suspect it might be PyCharm's autoformatting getting confused and treating this like a line of Python code where it would be safe to remove trailing spaces.

这是我的代码;它在unittest2类中:

Here is my code; it is in a unittest2 class:

def test_help_command(self):
    textgot = run_the_help_command()
    partone = """
blah blaah blah blah blah
This line has a space at the end in the help output"""
    parttwo = """
foo foo foo foo foo
This line also ends with a trailing space in the help output"""
    partthree = """
baz baz baz 
"""
    # Recombine parts with spaces
    helptext = partone + " " + parttwo + " " + partthree
    self.assertMultiLineEqual(helptext, textgot, 'Help text')

建议:

  • 如何解决此问题,所以我可以使用单个Heredoc字符串而不是拆分?我有更复杂的示例要测试,这些示例具有大块文本,这会使这种方法非常烦人
  • 比heredocs其他更好的方式来存储字符串
  • 要证明这是PyCharm的错误还是不是PyCharm的错误

推荐答案

这并不是PyCharm中的错误;这是其保存时的条带末尾空格"功能的局限性,该功能不与上下文相关,并且在所有行中都删除了末尾空格.您可以在设置" |关闭"下将其关闭.编辑器一般.

This isn't really a bug in PyCharm; it's a limitation of its "Strip trailing spaces on save" feature, which is not context-sensitive and strips trailing spaces in all lines. You can turn it off under Settings | Editor | General.

或者,您可以在Heredoc字符串中对尾随空格进行编码,方法是将它们替换为一些特殊标记(%20"或类似的东西),然后在执行assertMultilineEqual()调用之前将其替换回去.

Alternatively, you can encode the trailing spaces in your heredoc strings by replacing them with some special marker ("%20" or something like this), and replacing them back before performing the assertMultilineEqual() call.

另一种选择是简单地从CLI命令的输出中删除尾随空格,然后再将其与预期输出进行比较.

Another option is to simply strip the trailing spaces from the output of your CLI command before comparing it with the expected output.

这篇关于在PyCharm中的Python Heredoc行上删除了尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆