在Python中将string.Template子类化的示例? [英] Example of subclassing string.Template in Python?

查看:170
本文介绍了在Python中将string.Template子类化的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我在文档中已经看到很多关于这样做的参考,但我仍找不到Python中子类化string.Template的好例子.

I haven't been able to find a good example of subclassing string.Template in Python, even though I've seen multiple references to doing so in documentation.

网络上有任何示例吗?

我想将$更改为其他字符,并可能更改标识符的正则表达式.

I want to change the $ to be a different character and maybe change the regex for identifiers.

推荐答案

来自python 文档:

高级用法:您可以导出 要自定义的Template的子类 占位符语法,定界符 字符,或整个常规 用于解析模板的表达式 字符串.为此,您可以覆盖 这些类的属性:

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:

  • 定界符–这是描述占位符的文字字符串 引入定界符.默认值 价值$.请注意,这不应该是 正则表达式,例如 实现将调用re.escape() 根据需要在此字符串上输入.

  • 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 –这是描述正则表达式的正则表达式 非花括号占位符(花括号 将自动添加为 合适的).默认值为 正则表达式[_a-z] [_ a-z0-9] *.

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中:


In python 3:

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:

  • 已转义–该组匹配转义序列,例如$$,采用默认格式.
  • named –该组与不带括号的占位符名称匹配;它不应在捕获组中包含定界符.
  • 花括号–该组与花括号括起来的占位符名称匹配;它不应在捕获中包括定界符或大括号 组.
  • invalid –该组与任何其他定界符模式(通常是单个定界符)匹配,并且应出现在常规中的最后 表达.
  • 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来禁用它.

Apparently it is also possible to just omit any of the regex groups escaped, named, braced or invalid to disable it.

这篇关于在Python中将string.Template子类化的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆