您如何在字符串和翻译字符串中插入换行符以满足PEP 8的要求?

问题描述 投票:0回答:2

我尝试编写符合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
2个回答
0
投票

那应该做的事:

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

0
投票
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?')
© www.soinside.com 2019 - 2024. All rights reserved.