缩进保留多行字符串的格式

时间:2016-05-28 16:14:28

标签: python format string-formatting

我使用格式字符串(如

)生成一些(C ++)代码
memfn_declaration = '''\
  {doc}
  {static}auto {fun}({formals}){const}
    -> {result};
'''

格式字符串对此很好,但有没有办法让它们保持{doc}的缩进级别?它通常是多线的。

我知道我可以将对应于doc的字符串缩进两个空格,我知道这有很多功能,但这不是我要问的:我正在寻找一些能够在没有调整我传递的字符串的情况下工作。

3 个答案:

答案 0 :(得分:1)

现在您已经发布了自己的答案,并且更多地了解了您想要的内容。我认为通过定义自己的str子类来扩展字符串的格式,通过支持新的转换类型'i'来扩展字符串的方式会更好一些,后面必须跟一个十进制数字表示所需的缩进程度。

这是一个适用于Python 2和DSP的实现。 3:

import re
from textwrap import dedent
try:
    from textwrap import indent
except ImportError:
    def indent(s, prefix):
        return prefix + prefix.join(s.splitlines(True))

class Indentable(str):
    indent_format_spec = re.compile(r'''i([0-9]+)''')

    def __format__(self, format_spec):
        matches = self.indent_format_spec.search(format_spec)
        if matches:
            level = int(matches.group(1))
            first, sep, text = dedent(self).strip().partition('\n')
            return first + sep + indent(text, ' ' * level)

        return super(Indentable, self).__format__(format_spec)

sample_format_string = '''\
  {doc:i2}
  {static}auto {fun}({formals}){const}
    -> {result};
'''

specs = {
    'doc': Indentable('''
        // Convert a string to a float.
        // Quite obsolete.
        // Use something better instead.
    '''),
    'static': '',
    'fun': 'atof',
    'formals': 'const char*',
    'const': '',
    'result': 'float',
}

print(sample_format_string.format(**specs))

输出:

  // Convert a string to a float.
  // Quite obsolete.
  // Use something better instead.
  auto atof(const char*)
    -> float;

答案 1 :(得分:0)

问题在于:

'''\

反斜杠将忽略直到下一个字符的所有空格。删除反斜杠。

答案 2 :(得分:0)

我正在寻找呼叫者网站上轻量级的东西。我可以接受以下折衷:我使用格式字符串允许查询属性(0.foo)或项目(0['foo'])来传递格式字符串中的缩进级别。

import textwrap

class Indent(str):
    def __new__(cls, *args, **kwargs):
        return super(Indent, cls).__new__(cls, *args, **kwargs)

    def __getitem__(self, level):
        first, _, text = textwrap.dedent(self).strip().partition('\n')
        text = textwrap.indent(text, ' ' * level)
        return first + '\n' + text

def indent_doc(d):
    res = dict(d)
    res['doc'] = Indent(d['doc'])
    return res

format = '''
  {doc[2]}
  {static}auto {fun}({formals}){const}
    -> {result};
'''

specs = {
    'doc': '''
    // Convert a string to a float.
    // Quite obsolete.
    // Use something better instead.
    ''',
    'static': '',
    'fun': 'atof',
    'formals': 'const char*',
    'const': '',
    'result': 'float',
}
print(format.format_map(indent_doc(specs)))

给出:

$ python /tmp/foo.py

  // Convert a string to a float.
  // Quite obsolete.
  // Use something better instead.
  auto atof(const char*)
    -> float;

我很乐意阅读有关此事的意见。