6

我正在尝试漂亮地打印一个 HTTP 请求(我在这里嘲笑过)。

from typing import NamedTuple

class RequestMock(NamedTuple):
    method = 'POST'
    url = 'https://bob.com'
    body = 'body1\nbody2'
    headers = {'a': '1', 'b': '2'}

我有一个功能可以做到这一点:

req = RequestMock()

def print1(req):
    headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
    s = '\n'.join([
        f'{req.method} {req.url}',
        headers,
        req.body
    ])
    print(s)

print1(req)
# POST https://bob.com
# a: 1
# b: 2
# body1
# body2

但是当我f-strings为了清晰和易于修改而尝试重写它时,我得到了一些不好的缩进:

# what I want the code to look like
def print2(req):
    headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
    s = f"""
    {req.method} {req.url}
    {headers}
    {req.body}
    """
    print(s)

print2(req)
#     POST https://bob.com
#     a: 1
# b: 2
#     body1
# body2

我知道这是因为我用换行符定义字符串并将它们放在三引号字符串中。有没有一种简单的方法可以使用函数中定义的三引号来获取我正在查看的输出f-string,而不必知道其定义的缩进级别?我玩过textwrap.indent, textwrap.dedent, str.lstrip,re等,但代码不再简单和快速。我想出的最接近的是以下内容,但长度很尴尬,我觉得我在重复自己。

def print3(req):
    headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
    s = textwrap.dedent("""
    {method} {url}
    {headers}
    {body}
    """).strip()
    s = s.format(
        method=req.method,
        url=req.url,
        headers=headers,
        body=req.body,
    )
    print(s)
print3(req)
# POST https://bob.com
# a: 1
# b: 2
# body1
# body2
4

2 回答 2

2

我认为您可以尝试利用隐式字符串连接来获得一个看起来不错的解决方案:

def print4(req):
    headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
    s = (f'{req.method} {req.url}\n'
         f'{headers}\n'
         f'{req.body}')
    print(s)

print4(req)

输出:

POST https://bob.com
a: 1
b: 2
body1
body2

请注意,如果需要,您可以去掉括号并使用反斜杠:

s = f'{req.method} {req.url}\n' \
    f'{headers}\n'              \
    f'{req.body}'

但是,样式指南更喜欢括号而不是反斜杠。


另外的选择:

def print5(req):
    headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
    s = f"""
    {req.method} {req.url}
    {headers}
    {req.body}
    """
    s = '\n'.join(l.lstrip() for l in s.splitlines())
    print(s)
于 2019-01-06T05:56:48.337 回答
1

您可以通过 2 个微小的更改来修复它:

def print6(req, **w):
    headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
    method, url, body = \
        w['method'], w['url'], w['body']
    #   < note the changes belowwwwwwwwwwww >
    s = '\n'.join(line.lstrip() for line in f"""
    {method} {url}
    {headers}
    {body}
    """.split('\n')) # and note this .split('\n') over here
    print(s)
print6(req)
于 2019-01-06T06:00:31.490 回答