python 中的模板

发布于 2024-10-25 19:58:04 字数 548 浏览 0 评论 0原文

如何编写一个函数 render_user ,它接受 userlist 返回的元组之一和字符串模板,并返回替换到模板中的数据,例如:

>>> tpl = "<a href='mailto:%s'>%s</a>"
>>> render_user(('[email protected]', 'matt rez', ), tpl)
"<a href='mailto:[email protected]>Matt rez</a>"

任何帮助将不胜感激

How to write a function render_user which takes one of the tuples returned by userlist and a string template and returns the data substituted into the template, eg:

>>> tpl = "<a href='mailto:%s'>%s</a>"
>>> render_user(('[email protected]', 'matt rez', ), tpl)
"<a href='mailto:[email protected]>Matt rez</a>"

Any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-11-01 19:58:04

如果您不需要创建函数,则无需迫切需要创建函数:

>>> tpl = "<a href='mailto:%s'>%s</a>"
>>> s = tpl % ('[email protected]', 'matt rez', )

>>> print s
"<a href='mailto:[email protected]'>matt rez</a>"

如果您使用的是 2.6+,则可以选择使用新的 format 函数及其迷你语言:

>>> tpl = "<a href='mailto:{0}'>{1}</a>"
>>> s = tpl.format('[email protected]', 'matt rez')

>>> print s
"<a href='mailto:[email protected]'>matt rez</a>"

包装在函数中:

def render_user(userinfo, template="<a href='mailto:{0}'>{1}</a>"):
    """ Renders a HTML link for a given ``userinfo`` tuple;
        tuple contains (email, name) """
    return template.format(userinfo)

# Usage:

userinfo = ('[email protected]', 'matt rez')

print render_user(userinfo)
# same output as above

额外信用:

不要使用普通的 tuple 对象,而是尝试使用更强大且人性化的 namedtuplecollections 模块提供。它具有与常规元组相同的性能特征(和内存消耗)。命名元组的简短介绍可以在 PyCon 2011 视频中找到(快进到约 12m): http://blip .tv/file/4883247

No urgent need to create a function, if you don't require one:

>>> tpl = "<a href='mailto:%s'>%s</a>"
>>> s = tpl % ('[email protected]', 'matt rez', )

>>> print s
"<a href='mailto:[email protected]'>matt rez</a>"

If you're on 2.6+ you can alternatively use the new format function along with its mini language:

>>> tpl = "<a href='mailto:{0}'>{1}</a>"
>>> s = tpl.format('[email protected]', 'matt rez')

>>> print s
"<a href='mailto:[email protected]'>matt rez</a>"

Wrapped in a function:

def render_user(userinfo, template="<a href='mailto:{0}'>{1}</a>"):
    """ Renders a HTML link for a given ``userinfo`` tuple;
        tuple contains (email, name) """
    return template.format(userinfo)

# Usage:

userinfo = ('[email protected]', 'matt rez')

print render_user(userinfo)
# same output as above

Extra credit:

Instead of using a normal tuple object try use the more robust and human friendly namedtuple provided by the collections module. It has the same performance characteristics (and memory consumption) as a regular tuple. An short intro into named tuples can be found in this PyCon 2011 Video (fast forward to ~12m): http://blip.tv/file/4883247

雅心素梦 2024-11-01 19:58:04
from string import Template
t = Template("${my} + ${your} = 10")
print(t.substitute({"my": 4, "your": 6}))
from string import Template
t = Template("${my} + ${your} = 10")
print(t.substitute({"my": 4, "your": 6}))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文