[Python-checkins] python/dist/src/Lib sre.py, 1.49, 1.50 sre_constants.py, 1.34, 1.35 sre_parse.py, 1.60, 1.61 string.py, 1.71, 1.72

bwarsaw at users.sourceforge.net bwarsaw at users.sourceforge.net
Wed Aug 25 04:22:32 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23842/Lib
Modified Files:
	sre.py sre_constants.py sre_parse.py string.py 
Log Message:
PEP 292 classes Template and SafeTemplate are added to the string module.
This patch includes test cases and documentation updates, as well as NEWS file
updates.
This patch also updates the sre modules so that they don't import the string
module, breaking direct circular imports.
Index: sre.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre.py,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -d -r1.49 -r1.50
--- sre.py	7 Aug 2004 17:41:54 -0000	1.49
+++ sre.py	25 Aug 2004 02:22:29 -0000	1.50
@@ -105,9 +105,6 @@
 
 __version__ = "2.2.1"
 
-# this module works under 1.5.2 and later. don't use string methods
-import string
-
 # flags
 I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
 L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
@@ -201,7 +198,7 @@
 s[i] = "\000円"
 else:
 s[i] = "\\" + c
- return _join(s, pattern)
+ return pattern[:0].join(s)
 
 # --------------------------------------------------------------------
 # internals
@@ -213,10 +210,6 @@
 
 _MAXCACHE = 100
 
-def _join(seq, sep):
- # internal: join into string having the same type as sep
- return string.join(seq, sep[:0])
-
 def _compile(*key):
 # internal: compile pattern
 cachekey = (type(key[0]),) + key
Index: sre_constants.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre_constants.py,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- sre_constants.py	17 Oct 2003 22:13:16 -0000	1.34
+++ sre_constants.py	25 Aug 2004 02:22:29 -0000	1.35
@@ -217,12 +217,11 @@
 SRE_INFO_CHARSET = 4 # pattern starts with character from given set
 
 if __name__ == "__main__":
- import string
 def dump(f, d, prefix):
 items = d.items()
 items.sort(key=lambda a: a[1])
 for k, v in items:
- f.write("#define %s_%s %s\n" % (prefix, string.upper(k), v))
+ f.write("#define %s_%s %s\n" % (prefix, k.upper(), v))
 f = open("sre_constants.h", "w")
 f.write("""\
 /*
Index: sre_parse.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre_parse.py,v
retrieving revision 1.60
retrieving revision 1.61
diff -u -d -r1.60 -r1.61
--- sre_parse.py	26 Mar 2004 23:24:00 -0000	1.60
+++ sre_parse.py	25 Aug 2004 02:22:29 -0000	1.61
@@ -12,8 +12,7 @@
 
 # XXX: show string offset and offending character for all errors
 
-# this module works under 1.5.2 and later. don't use string methods
-import string, sys
+import sys
 
 from sre_constants import *
 
@@ -63,13 +62,6 @@
 "u": SRE_FLAG_UNICODE,
 }
 
-# figure out best way to convert hex/octal numbers to integers
-try:
- int("10", 8)
- atoi = int # 2.0 and later
-except TypeError:
- atoi = string.atoi # 1.5.2
-
 class Pattern:
 # master pattern object. keeps track of global attributes
 def __init__(self):
@@ -233,7 +225,7 @@
 def _group(escape, groups):
 # check if the escape string represents a valid group
 try:
- gid = atoi(escape[1:])
+ gid = int(escape[1:])
 if gid and gid < groups:
 return gid
 except ValueError:
@@ -256,13 +248,13 @@
 escape = escape[2:]
 if len(escape) != 2:
 raise error, "bogus escape: %s" % repr("\\" + escape)
- return LITERAL, atoi(escape, 16) & 0xff
+ return LITERAL, int(escape, 16) & 0xff
 elif escape[1:2] in OCTDIGITS:
 # octal escape (up to three digits)
 while source.next in OCTDIGITS and len(escape) < 5:
 escape = escape + source.get()
 escape = escape[1:]
- return LITERAL, atoi(escape, 8) & 0xff
+ return LITERAL, int(escape, 8) & 0xff
 if len(escape) == 2:
 return LITERAL, ord(escape[1])
 except ValueError:
@@ -284,12 +276,12 @@
 escape = escape + source.get()
 if len(escape) != 4:
 raise ValueError
- return LITERAL, atoi(escape[2:], 16) & 0xff
+ return LITERAL, int(escape[2:], 16) & 0xff
 elif escape[1:2] == "0":
 # octal escape
 while source.next in OCTDIGITS and len(escape) < 4:
 escape = escape + source.get()
- return LITERAL, atoi(escape[1:], 8) & 0xff
+ return LITERAL, int(escape[1:], 8) & 0xff
 elif escape[1:2] in DIGITS:
 # octal escape *or* decimal group reference (sigh)
 if source.next in DIGITS:
@@ -298,7 +290,7 @@
 source.next in OCTDIGITS):
 # got three octal digits; this is an octal escape
 escape = escape + source.get()
- return LITERAL, atoi(escape[1:], 8) & 0xff
+ return LITERAL, int(escape[1:], 8) & 0xff
 # got at least one decimal digit; this is a group reference
 group = _group(escape, state.groups)
 if group:
@@ -503,9 +495,9 @@
 source.seek(here)
 continue
 if lo:
- min = atoi(lo)
+ min = int(lo)
 if hi:
- max = atoi(hi)
+ max = int(hi)
 if max < min:
 raise error, "bad repeat interval"
 else:
@@ -617,7 +609,7 @@
 raise error, "unknown group name"
 else:
 try:
- condgroup = atoi(condname)
+ condgroup = int(condname)
 except ValueError:
 raise error, "bad character in group name"
 else:
@@ -730,7 +722,7 @@
 if not name:
 raise error, "bad group name"
 try:
- index = atoi(name)
+ index = int(name)
 except ValueError:
 if not isname(name):
 raise error, "bad character in group name"
@@ -754,7 +746,7 @@
 break
 if not code:
 this = this[1:]
- code = LITERAL, makechar(atoi(this[-6:], 8) & 0xff)
+ code = LITERAL, makechar(int(this[-6:], 8) & 0xff)
 if code[0] is LITERAL:
 literal(code[1])
 else:
@@ -793,4 +785,4 @@
 raise IndexError
 except IndexError:
 raise error, "empty group"
- return string.join(literals, sep)
+ return sep.join(literals)
Index: string.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/string.py,v
retrieving revision 1.71
retrieving revision 1.72
diff -u -d -r1.71 -r1.72
--- string.py	15 Dec 2003 18:49:19 -0000	1.71
+++ string.py	25 Aug 2004 02:22:29 -0000	1.72
@@ -35,10 +35,116 @@
 
 # Case conversion helpers
 # Use str to convert Unicode literal in case of -U
+# Note that Cookie.py bogusly uses _idmap :(
 l = map(chr, xrange(256))
 _idmap = str('').join(l)
 del l
 
+# Functions which aren't available as string methods.
+
+# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
+# See also regsub.capwords().
+def capwords(s, sep=None):
+ """capwords(s, [sep]) -> string
+
+ Split the argument into words using split, capitalize each
+ word using capitalize, and join the capitalized words using
+ join. Note that this replaces runs of whitespace characters by
+ a single space.
+
+ """
+ return (sep or ' ').join([x.capitalize() for x in s.split(sep)])
+
+
+# Construct a translation string
+_idmapL = None
+def maketrans(fromstr, tostr):
+ """maketrans(frm, to) -> string
+
+ Return a translation table (a string of 256 bytes long)
+ suitable for use in string.translate. The strings frm and to
+ must be of the same length.
+
+ """
+ if len(fromstr) != len(tostr):
+ raise ValueError, "maketrans arguments must have same length"
+ global _idmapL
+ if not _idmapL:
+ _idmapL = map(None, _idmap)
+ L = _idmapL[:]
+ fromstr = map(ord, fromstr)
+ for i in range(len(fromstr)):
+ L[fromstr[i]] = tostr[i]
+ return ''.join(L)
+
+
+
+import re as _re
+
+class Template(unicode):
+ """A string class for supporting $-substitutions."""
+ __slots__ = []
+
+ # Search for $,ドル $identifier, ${identifier}, and any bare $'s
+ pattern = _re.compile(r"""
+# Match exactly two $'s -- this is the escape sequence
+(?P<escaped>\${2})|
+# Match a $ followed by a Python identifier
+\$(?P<named>[_a-z][_a-z0-9]*)|
+# Match a $ followed by a brace delimited identifier
+\${(?P<braced>[_a-z][_a-z0-9]*)}|
+# Match any other $'s
+(?P<bogus>\$)
+""", _re.IGNORECASE | _re.VERBOSE)
+
+ def __mod__(self, mapping):
+ def convert(mo):
+ groups = mo.groupdict()
+ if groups.get('escaped') is not None:
+ return '$'
+ if groups.get('bogus') is not None:
+ raise ValueError('Invalid placeholder at index %d' %
+ mo.start('bogus'))
+ val = mapping[groups.get('named') or groups.get('braced')]
+ return unicode(val)
+ return self.pattern.sub(convert, self)
+
+
+class SafeTemplate(Template):
+ """A string class for supporting $-substitutions.
+
+ This class is 'safe' in the sense that you will never get KeyErrors if
+ there are placeholders missing from the interpolation dictionary. In that
+ case, you will get the original placeholder in the value string.
+ """
+ __slots__ = []
+
+ def __mod__(self, mapping):
+ def convert(mo):
+ groups = mo.groupdict()
+ if groups.get('escaped') is not None:
+ return '$'
+ if groups.get('bogus') is not None:
+ raise ValueError('Invalid placeholder at index %d' %
+ mo.start('bogus'))
+ named = groups.get('named')
+ if named is not None:
+ try:
+ return unicode(mapping[named])
+ except KeyError:
+ return '$' + named
+ braced = groups.get('braced')
+ try:
+ return unicode(mapping[braced])
+ except KeyError:
+ return '${' + braced + '}'
+ return self.pattern.sub(convert, self)
+
+
+
+# NOTE: Everything below here is deprecated. Use string methods instead.
+# This stuff will go away in Python 3.0.
+
 # Backward compatible names for exceptions
 index_error = ValueError
 atoi_error = ValueError
@@ -336,40 +442,6 @@
 """
 return s.capitalize()
 
-# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
-# See also regsub.capwords().
-def capwords(s, sep=None):
- """capwords(s, [sep]) -> string
-
- Split the argument into words using split, capitalize each
- word using capitalize, and join the capitalized words using
- join. Note that this replaces runs of whitespace characters by
- a single space.
-
- """
- return join(map(capitalize, s.split(sep)), sep or ' ')
-
-# Construct a translation string
-_idmapL = None
-def maketrans(fromstr, tostr):
- """maketrans(frm, to) -> string
-
- Return a translation table (a string of 256 bytes long)
- suitable for use in string.translate. The strings frm and to
- must be of the same length.
-
- """
- if len(fromstr) != len(tostr):
- raise ValueError, "maketrans arguments must have same length"
- global _idmapL
- if not _idmapL:
- _idmapL = map(None, _idmap)
- L = _idmapL[:]
- fromstr = map(ord, fromstr)
- for i in range(len(fromstr)):
- L[fromstr[i]] = tostr[i]
- return join(L, "")
-
 # Substring replacement (global)
 def replace(s, old, new, maxsplit=-1):
 """replace (str, old, new[, maxsplit]) -> string


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /