[Python-checkins] python/nondist/sandbox/string/string __init__.py,
1.2, 1.3 template.py, 1.1, 1.2
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Thu Aug 12 23:20:28 CEST 2004
Update of /cvsroot/python/python/nondist/sandbox/string/string
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7407
Modified Files:
__init__.py template.py
Log Message:
Capitalized the class names.
Fixed a broken example
Cleaned-up the namespace.
Added __slots__ for compactness.
Doctest the example.
Index: __init__.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/string/string/__init__.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** __init__.py 10 Aug 2004 22:27:46 -0000 1.2
--- __init__.py 12 Aug 2004 21:20:24 -0000 1.3
***************
*** 24,31 ****
'digits', 'hexdigits', 'octdigits', 'punctuation', 'printable',
# PEP 292
! 'template', 'safe_template'
]
! from template import template, safe_template
from deprecated import *
--- 24,31 ----
'digits', 'hexdigits', 'octdigits', 'punctuation', 'printable',
# PEP 292
! 'Template', 'SafeTemplate'
]
! from template import Template, SafeTemplate
from deprecated import *
Index: template.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/string/string/template.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** template.py 10 Aug 2004 22:27:46 -0000 1.1
--- template.py 12 Aug 2004 21:20:24 -0000 1.2
***************
*** 8,12 ****
Two subclasses of the unicode type are provided:
! template - PEP 292 'dollar' strings with the following substitution rules:
1. $$ is an escape; it is replaced with a single $
--- 8,12 ----
Two subclasses of the unicode type are provided:
! Template - PEP 292 'dollar' strings with the following substitution rules:
1. $$ is an escape; it is replaced with a single $
***************
*** 26,39 ****
http://www.python.org/doc/current/ref/identifiers.html
! safe_template - Like templates, except that KeyErrors will never be raised
when a placeholder is missing from the interpolation dictionary.
! You can also derive your own classes from template to define different
! substitution rules. UTSL for details.
Examples:
! >>> from string import template
! >>> x = template('$who owes me $what')
>>> print x
$who owes me $what
--- 26,39 ----
http://www.python.org/doc/current/ref/identifiers.html
! SafeTemplate - Like Template, except that KeyErrors will never be raised
when a placeholder is missing from the interpolation dictionary.
! You can also derive your own classes from Template to define different
! substitution rules.
Examples:
! >>> from string import Template
! >>> x = Template('$who owes me $what')
>>> print x
$who owes me $what
***************
*** 42,61 ****
>>> import re
! >>> class mstring(template):
... pattern = re.compile(
... r'(\${2})|\$(mm:[_a-z]\w*)|\$({mm:[_a-z]\w*})', re.IGNORECASE)
...
>>> x = mstring('$who owes me $mm:what')
! >>> print x % {'who': 'Tim', 'mm:what', 'nothing'}
$who owes me nothing
"""
! __all__ = ['template', 'safe_template']
import re
!
! class template(unicode):
"""A string class for supporting $-substitutions."""
# Search for $$, $identifier, or ${identifier}
--- 42,61 ----
>>> import re
! >>> class mstring(Template):
... pattern = re.compile(
... r'(\${2})|\$(mm:[_a-z]\w*)|\$({mm:[_a-z]\w*})', re.IGNORECASE)
...
>>> x = mstring('$who owes me $mm:what')
! >>> print x % {'who':'Tim', 'mm:what':'nothing'}
$who owes me nothing
"""
! __all__ = ['Template', 'SafeTemplate']
import re
! class Template(unicode):
"""A string class for supporting $-substitutions."""
+ __slots__ = []
# Search for $$, $identifier, or ${identifier}
***************
*** 74,78 ****
! class safe_template(template):
"""A string class for supporting $-substitutions.
--- 74,78 ----
! class SafeTemplate(Template):
"""A string class for supporting $-substitutions.
***************
*** 81,84 ****
--- 81,85 ----
case, you will get the original placeholder in the value string.
"""
+ __slots__ = []
def __mod__(self, mapping):
***************
*** 98,99 ****
--- 99,106 ----
return '${' + braced + '}'
return self.pattern.sub(convert, self)
+
+ del re
+
+ if __name__ == '__main__':
+ import doctest
+ print doctest.testmod()
More information about the Python-checkins
mailing list