Minor improvements to the template code.

* Add comment bars segregating this code from the rest.
* Improve readability of the re pattern with indentation and comments on
  the same line.
* Replace the groupdict() and get() pair with a direct call to group()
  which does the same thing.
This commit is contained in:
Raymond Hettinger 2004-08-26 00:21:13 +00:00
parent a6b68327b2
commit 0d58e2be0b
1 changed files with 15 additions and 18 deletions

View File

@ -79,6 +79,7 @@ def maketrans(fromstr, tostr):
####################################################################
import re as _re import re as _re
class Template(unicode): class Template(unicode):
@ -87,25 +88,20 @@ class Template(unicode):
# Search for $$, $identifier, ${identifier}, and any bare $'s # Search for $$, $identifier, ${identifier}, and any bare $'s
pattern = _re.compile(r""" pattern = _re.compile(r"""
# Match exactly two $'s -- this is the escape sequence (?P<escaped>\${2})| # Escape sequence of two $ signs
(?P<escaped>\${2})| \$(?P<named>[_a-z][_a-z0-9]*)| # $ and a Python identifier
# Match a $ followed by a Python identifier \${(?P<braced>[_a-z][_a-z0-9]*)}| # $ and a brace delimited identifier
\$(?P<named>[_a-z][_a-z0-9]*)| (?P<bogus>\$) # Other ill-formed $ expressions
# Match a $ followed by a brace delimited identifier """, _re.IGNORECASE | _re.VERBOSE)
\${(?P<braced>[_a-z][_a-z0-9]*)}|
# Match any other $'s
(?P<bogus>\$)
""", _re.IGNORECASE | _re.VERBOSE)
def __mod__(self, mapping): def __mod__(self, mapping):
def convert(mo): def convert(mo):
groups = mo.groupdict() if mo.group('escaped') is not None:
if groups.get('escaped') is not None:
return '$' return '$'
if groups.get('bogus') is not None: if mo.group('bogus') is not None:
raise ValueError('Invalid placeholder at index %d' % raise ValueError('Invalid placeholder at index %d' %
mo.start('bogus')) mo.start('bogus'))
val = mapping[groups.get('named') or groups.get('braced')] val = mapping[mo.group('named') or mo.group('braced')]
return unicode(val) return unicode(val)
return self.pattern.sub(convert, self) return self.pattern.sub(convert, self)
@ -121,27 +117,28 @@ class SafeTemplate(Template):
def __mod__(self, mapping): def __mod__(self, mapping):
def convert(mo): def convert(mo):
groups = mo.groupdict() if mo.group('escaped') is not None:
if groups.get('escaped') is not None:
return '$' return '$'
if groups.get('bogus') is not None: if mo.group('bogus') is not None:
raise ValueError('Invalid placeholder at index %d' % raise ValueError('Invalid placeholder at index %d' %
mo.start('bogus')) mo.start('bogus'))
named = groups.get('named') named = mo.group('named')
if named is not None: if named is not None:
try: try:
return unicode(mapping[named]) return unicode(mapping[named])
except KeyError: except KeyError:
return '$' + named return '$' + named
braced = groups.get('braced') braced = mo.group('braced')
try: try:
return unicode(mapping[braced]) return unicode(mapping[braced])
except KeyError: except KeyError:
return '${' + braced + '}' return '${' + braced + '}'
return self.pattern.sub(convert, self) return self.pattern.sub(convert, self)
del _re
####################################################################
# NOTE: Everything below here is deprecated. Use string methods instead. # NOTE: Everything below here is deprecated. Use string methods instead.
# This stuff will go away in Python 3.0. # This stuff will go away in Python 3.0.