【问题标题】:How do you insert line breaks in strings and translations string to meet the PEP 8 requirements?如何在字符串和翻译字符串中插入换行符以满足 PEP 8 要求?
【发布时间】:2020-01-30 07:08:33
【问题描述】:

我尝试编写符合 PEP8 要求的 Python 代码。 PEP8 代码风格指南说 Python 中的代码行不应超过 79 个字符。在我的代码中,我有很多长字符串,例如:

string_name = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam"

而且我有很多很长的翻译字符串,例如:

string_name = _("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam")

我寻找最好和最快的方法来打破这些字符串,但我没有找到让我满意的答案。例如,我知道我可以像这样手动插入中断:

string_name = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " \
"sed diam nonumy eirmod tempor invidunt ut labore et dolore magna " \
"aliquyam"

但这很不方便。例如,如果我在字符串的第一部分添加一个单词,我必须重写所有其余部分。

我正在寻找更好的选择或可以为我完成此任务的模块。

我希望你能帮助我。

提前谢谢你。

【问题讨论】:

    标签: python python-3.x wxpython translation


    【解决方案1】:
    import textwrap
    
    def pep8_textwrap(var_name, text, parentheses=False):
        '''Wrap text for code to be within 80 characters.'''
    
        # Variable name appended with assignment operator.
        var_name += ' = '
    
        if parentheses:
            var_name += '('
    
        # Indent size to align with right side of assignment.
        indent_size = len(var_name) - 1
    
        # Create a list of wrapped text.
        string_list = textwrap.wrap(text, width=75-indent_size)
    
        # Print the variable name equals without a newline.
        print(var_name, end='')
    
        # The last line so end of iteration will be known.
        last_line = len(string_list)
    
        # Print the wrapped text for pasting into code.
        for current_line, line in enumerate(string_list, 1):
            if current_line == 1:
                if last_line > 1 and not parentheses:
                    print(repr(line + ' '), '\\')
                else:
                    print(repr(line + ' '))
            elif current_line >= last_line:
                if parentheses:
                    print(indent_size * ' ', repr(line) + ')')
                else:
                    print(indent_size * ' ', repr(line))
            else:
                if parentheses:
                    print(indent_size * ' ', repr(line + ' '))
                else:
                    print(indent_size * ' ', repr(line + ' '), '\\')
    
    
    # Text from 1st code example.
    string_name = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam"
    
    # Call the function to print the result.
    pep8_textwrap('string_name', string_name)
    
    # Text from 3rd code example.
    string_name = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " \
    "sed diam nonumy eirmod tempor invidunt ut labore et dolore magna " \
    "aliquyam"
    
    pep8_textwrap('string_name', string_name, True)
    
    # Text from the title.
    string_name = "How do you insert line breaks in strings and translations string to meet the PEP 8 requirements?"
    
    pep8_textwrap('var', string_name, True)
    

    重新创建分配代码的脚本解决方案。 它将结果打印到标准输出。

    将赋值复制到脚本中,并使用参数调用pep8_textwrap 要分配的变量名称,文本的实际变量名称和 可选 True 用于使用括号或省略该参数以使用反斜杠 转义以继续一行到下一行。

    输出:

    string_name = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ' \
                  'diam nonumy eirmod tempor invidunt ut labore et dolore magna ' \
                  'aliquyam'
    string_name = ('Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed '
                   'diam nonumy eirmod tempor invidunt ut labore et dolore magna '
                   'aliquyam')
    var = ('How do you insert line breaks in strings and translations string to '
           'meet the PEP 8 requirements?')
    

    【讨论】:

      【解决方案2】:

      应该做的事情:

      string_name = "Lorem ipsum dolor sit amet," \
      "consetetur sadipscing elitr, sed diam nonumy" \
      "eirmod tempor invidunt ut labore et dolore magna aliquyam"
      

      【讨论】:

      • 这是我知道的一个选项。对不起,我应该在我的问题中写下这个。但是这个选项非常不方便。例如,如果您在字符串的第一部分添加一个单词,您将手动重写所有其余部分。我正在寻找更好的选择。我编辑了我的问题。
      猜你喜欢
      • 2011-10-20
      • 2011-05-04
      • 1970-01-01
      • 2018-01-30
      • 2017-11-14
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多