gpt4 book ai didi

python - 在 Python 中子类化 string.Template 的示例?

转载 作者:太空狗 更新时间:2023-10-29 17:15:09 26 4
gpt4 key购买 nike

我一直没能找到在 Python 中子类化 string.Template 的好例子,尽管我在文档中看到了很多这样的引用。

网上有这方面的例子吗?

我想将 $ 更改为不同的字符,并可能更改标识符的正则表达式。

最佳答案

来自 python docs :

Advanced usage: you can derive subclasses of Template to customize the placeholder syntax, delimiter character, or the entire regular expression used to parse template strings. To do this, you can override these class attributes:

  • delimiter – This is the literal string describing a placeholder introducing delimiter. The default value $. Note that this should not be a regular expression, as the implementation will call re.escape() on this string as needed.

  • idpattern – This is the regular expression describing the pattern for non-braced placeholders (the braces will be added automatically as appropriate). The default value is the regular expression [_a-z][_a-z0-9]*.

例子:

from string import Template

class MyTemplate(Template):
delimiter = '#'
idpattern = r'[a-z][_a-z0-9]*'

>>> s = MyTemplate('#who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes $what'

在 python 3 中:

New in version 3.2.

Alternatively, you can provide the entire regular expression pattern by overriding the class attribute pattern. If you do this, the value must be a regular expression object with four named capturing groups. The capturing groups correspond to the rules given above, along with the invalid placeholder rule:

  • escaped – This group matches the escape sequence, e.g. $$, in the default pattern.
  • named – This group matches the unbraced placeholder name; it should not include the delimiter in capturing group.
  • braced – This group matches the brace enclosed placeholder name; it should not include either the delimiter or braces in the capturing group.
  • invalid – This group matches any other delimiter pattern (usually a single delimiter), and it should appear last in the regular expression.

例子:

from string import Template
import re

class TemplateClone(Template):
delimiter = '$'
pattern = r'''
\$(?:
(?P<escaped>\$) | # Escape sequence of two delimiters
(?P<named>[_a-z][_a-z0-9]*) | # delimiter and a Python identifier
{(?P<braced>[_a-z][_a-z0-9]*)} | # delimiter and a braced identifier
(?P<invalid>) # Other ill-formed delimiter exprs
)
'''

class TemplateAlternative(Template):
delimiter = '[-'
pattern = r'''
\[-(?:
(?P<escaped>-) | # Expression [-- will become [-
(?P<named>[^\[\]\n-]+)-\] | # -, [, ], and \n can't be used in names
\b\B(?P<braced>) | # Braced names disabled
(?P<invalid>) #
)
'''

>>> t = TemplateClone("$hi sir")
>>> t.substitute({"hi": "hello"})
'hello sir'

>>> ta = TemplateAlternative("[-hi-] sir")
>>> ta.substitute({"hi": "have a nice day"})
'have a nice day sir'
>>> ta = TemplateAlternative("[--[-hi-]-]")
>>> ta.substitute({"hi": "have a nice day"})
'[-have a nice day-]'

显然也可以省略任何正则表达式组 escapednamedbracedinvalid 禁用它。

关于python - 在 Python 中子类化 string.Template 的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1336786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com