Django消息格式

2024-05-28 21:01:04 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有人理解这段代码,以及它将如何被更改为在同一行显示“sender writed:message”,而不是将其拆分到不同的行。如下图所示。我真的不明白他们为什么要用这种格式。在

enter image description here

代码输入实用工具.py在

def format_quote(sender, body):
    """
    Wraps text at 55 chars and prepends each
    line with `> `.
    Used for quoting messages in replies.
    """
    lines = wrap(body, 55).split('\n')
    for i, line in enumerate(lines):
        lines[i] = "> %s" % line
    quote = '\n'.join(lines)
    return ugettext(u"%(sender)s wrote:\n%(body)s") % {
        'sender': sender,
        'body': quote
    }

这是im使用的消息传递应用程序:https://github.com/arneb/django-messages

我会很感激你能帮我看一眼!在


Tags: 代码inpymessagefordef格式line
1条回答
网友
1楼 · 发布于 2024-05-28 21:01:04

这段代码只需要一些文本和发送方的名称:

  1. 包装它(它尝试添加一些'\n来生成文本列而不是行)
  2. 列出行列表
  3. 在每一行前加'> '
  4. 从列表生成文本
  5. 在此文本前面加'someone wrote:\n'

一些来自python的复制粘贴:

>>> message = '1234567890 ' * 7
>>> message
'1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 '
>>> sender = 'john'
>>> sender
'john'
>>> wrap(message, 55)
'1234567890 1234567890 1234567890 1234567890 1234567890\n1234567890 1234567890 '

注意\n

^{pr2}$

我认为这个代码本身不是问题。可能函数被称为产生如此难看的输出的一堆时间。在

相关问题 更多 >

    热门问题