Mercurial > cpython-lru_cache-weakref
annotate Lib/string.py @ 64166:d0e9febfa00a 3.1
Merged revisions 84172 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines
Restore GIL in nis_cat in case of error.
........
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 19 Aug 2010 09:03:03 +0000 |
| parents | f3c595214abc |
| children | 3c9232a684ae |
| rev | line source |
|---|---|
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
41226
diff
changeset
|
1 """A collection of string constants. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
2 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
3 Public module variables: |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
4 |
|
49804
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
5 whitespace -- a string containing all ASCII whitespace |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
6 ascii_lowercase -- a string containing all ASCII lowercase letters |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
7 ascii_uppercase -- a string containing all ASCII uppercase letters |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
8 ascii_letters -- a string containing all ASCII letters |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
9 digits -- a string containing all ASCII decimal digits |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
10 hexdigits -- a string containing all ASCII hexadecimal digits |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
11 octdigits -- a string containing all ASCII octal digits |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
12 punctuation -- a string containing all ASCII punctuation characters |
|
8f0a175f04be
#4361: fix string.py docstring, clarify that only ASCII characters are in its constants.
Georg Brandl <georg@python.org>
parents:
49734
diff
changeset
|
13 printable -- a string containing all ASCII characters considered printable |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
14 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
15 """ |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
16 |
| 5 | 17 # Some strings for ctype-style character classification |
|
1331
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
18 whitespace = ' \t\n\r\v\f' |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
42142
diff
changeset
|
19 ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' |
|
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
42142
diff
changeset
|
20 ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
18776
22b2f4b436af
Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents:
16657
diff
changeset
|
21 ascii_letters = ascii_lowercase + ascii_uppercase |
| 5 | 22 digits = '0123456789' |
| 23 hexdigits = digits + 'abcdef' + 'ABCDEF' | |
| 24 octdigits = '01234567' | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15610
diff
changeset
|
25 punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
42142
diff
changeset
|
26 printable = digits + ascii_letters + punctuation + whitespace |
| 5 | 27 |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
28 # Functions which aren't available as string methods. |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
29 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
30 # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
31 def capwords(s, sep=None): |
|
56233
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53909
diff
changeset
|
32 """capwords(s [,sep]) -> string |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
33 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
34 Split the argument into words using split, capitalize each |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
35 word using capitalize, and join the capitalized words using |
|
56233
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53909
diff
changeset
|
36 join. If the optional second argument sep is absent or None, |
|
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53909
diff
changeset
|
37 runs of whitespace characters are replaced by a single space |
|
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53909
diff
changeset
|
38 and leading and trailing whitespace are removed, otherwise |
|
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53909
diff
changeset
|
39 sep is used to split and join the words. |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
40 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
41 """ |
|
56233
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53909
diff
changeset
|
42 return (sep or ' ').join(x.capitalize() for x in s.split(sep)) |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
43 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
44 |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
45 # Construct a translation map for bytes.translate |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43241
diff
changeset
|
46 def maketrans(frm: bytes, to: bytes) -> bytes: |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
47 """maketrans(frm, to) -> bytes |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
48 |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
49 Return a translation table (a bytes object of length 256) |
|
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
50 suitable for use in bytes.translate where each byte in frm is |
|
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
51 mapped to the byte at the same position in to. |
|
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
52 The strings frm and to must be of the same length. |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
53 """ |
|
53094
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
49804
diff
changeset
|
54 import warnings |
|
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
49804
diff
changeset
|
55 warnings.warn("string.maketrans is deprecated, use bytes.maketrans instead", |
|
53909
238502460795
#4351: more appropriate DeprecationWarning stacklevels
Philip Jenvey <pjenvey@underboss.org>
parents:
53094
diff
changeset
|
56 DeprecationWarning, 2) |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
57 if len(frm) != len(to): |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
43016
diff
changeset
|
58 raise ValueError("maketrans arguments must have same length") |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
59 if not (isinstance(frm, bytes) and isinstance(to, bytes)): |
|
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
60 raise TypeError("maketrans arguments must be bytes objects") |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
61 L = bytearray(range(256)) |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
62 for i, c in enumerate(frm): |
|
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
43112
diff
changeset
|
63 L[c] = to[i] |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43241
diff
changeset
|
64 return bytes(L) |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
65 |
|
34127
4dcd78f3b5d1
Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents:
33871
diff
changeset
|
66 |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
67 #################################################################### |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
68 import re as _re |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
69 |
|
33471
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
70 class _multimap: |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
71 """Helper class for combining multiple mappings. |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
72 |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
73 Used by .{safe_,}substitute() to combine the mapping and keyword |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
74 arguments. |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
75 """ |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
76 def __init__(self, primary, secondary): |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
77 self._primary = primary |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
78 self._secondary = secondary |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
79 |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
80 def __getitem__(self, key): |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
81 try: |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
82 return self._primary[key] |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
83 except KeyError: |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
84 return self._secondary[key] |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
85 |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
86 |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
87 class _TemplateMetaclass(type): |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
88 pattern = r""" |
|
33588
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
89 %(delim)s(?: |
|
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
90 (?P<escaped>%(delim)s) | # Escape sequence of two delimiters |
|
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
91 (?P<named>%(id)s) | # delimiter and a Python identifier |
|
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
92 {(?P<braced>%(id)s)} | # delimiter and a braced identifier |
|
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
93 (?P<invalid>) # Other ill-formed delimiter exprs |
|
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
94 ) |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
95 """ |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
96 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
97 def __init__(cls, name, bases, dct): |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
98 super(_TemplateMetaclass, cls).__init__(name, bases, dct) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
99 if 'pattern' in dct: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
100 pattern = cls.pattern |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
101 else: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
102 pattern = _TemplateMetaclass.pattern % { |
|
33505
cc0ae03743b8
At the cost of a modest (but useful in its own right) change in the semantics
Barry Warsaw <barry@python.org>
parents:
33497
diff
changeset
|
103 'delim' : _re.escape(cls.delimiter), |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
104 'id' : cls.idpattern, |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
105 } |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
106 cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
107 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
108 |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
109 class Template(metaclass=_TemplateMetaclass): |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
110 """A string class for supporting $-substitutions.""" |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
111 |
|
33505
cc0ae03743b8
At the cost of a modest (but useful in its own right) change in the semantics
Barry Warsaw <barry@python.org>
parents:
33497
diff
changeset
|
112 delimiter = '$' |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
113 idpattern = r'[_a-z][_a-z0-9]*' |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
114 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
115 def __init__(self, template): |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
116 self.template = template |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
117 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
118 # Search for $$, $identifier, ${identifier}, and any bare $'s |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
119 |
|
33479
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
120 def _invalid(self, mo): |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
121 i = mo.start('invalid') |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
122 lines = self.template[:i].splitlines(True) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
123 if not lines: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
124 colno = 1 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
125 lineno = 1 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
126 else: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
127 colno = i - len(''.join(lines[:-1])) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
128 lineno = len(lines) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
129 raise ValueError('Invalid placeholder in string: line %d, col %d' % |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
130 (lineno, colno)) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
131 |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
132 def substitute(self, *args, **kws): |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
133 if len(args) > 1: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
134 raise TypeError('Too many positional arguments') |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
135 if not args: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
136 mapping = kws |
|
33471
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
137 elif kws: |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
138 mapping = _multimap(kws, args[0]) |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
139 else: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
140 mapping = args[0] |
|
33471
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
141 # Helper function for .sub() |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
142 def convert(mo): |
|
33479
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
143 # Check the most common path first. |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
144 named = mo.group('named') or mo.group('braced') |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
145 if named is not None: |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
146 val = mapping[named] |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
147 # We use this idiom instead of str() because the latter will |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
148 # fail if val is a Unicode containing non-ASCII characters. |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
34127
diff
changeset
|
149 return '%s' % (val,) |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
150 if mo.group('escaped') is not None: |
|
33505
cc0ae03743b8
At the cost of a modest (but useful in its own right) change in the semantics
Barry Warsaw <barry@python.org>
parents:
33497
diff
changeset
|
151 return self.delimiter |
|
33479
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
152 if mo.group('invalid') is not None: |
|
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
153 self._invalid(mo) |
|
33773
ab09daac5691
Invalid patterns to substitute and safe_substitute would crash since pattern
Neal Norwitz <nnorwitz@gmail.com>
parents:
33588
diff
changeset
|
154 raise ValueError('Unrecognized named group in pattern', |
|
ab09daac5691
Invalid patterns to substitute and safe_substitute would crash since pattern
Neal Norwitz <nnorwitz@gmail.com>
parents:
33588
diff
changeset
|
155 self.pattern) |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
156 return self.pattern.sub(convert, self.template) |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
157 |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
158 def safe_substitute(self, *args, **kws): |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
159 if len(args) > 1: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
160 raise TypeError('Too many positional arguments') |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
161 if not args: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
162 mapping = kws |
|
33471
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
163 elif kws: |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
164 mapping = _multimap(kws, args[0]) |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
165 else: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
166 mapping = args[0] |
|
33471
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
167 # Helper function for .sub() |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
168 def convert(mo): |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
169 named = mo.group('named') |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
170 if named is not None: |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
171 try: |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
172 # We use this idiom instead of str() because the latter |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
173 # will fail if val is a Unicode containing non-ASCII |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
34127
diff
changeset
|
174 return '%s' % (mapping[named],) |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
175 except KeyError: |
|
33505
cc0ae03743b8
At the cost of a modest (but useful in its own right) change in the semantics
Barry Warsaw <barry@python.org>
parents:
33497
diff
changeset
|
176 return self.delimiter + named |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
177 braced = mo.group('braced') |
|
33482
2623668c5ff5
Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents:
33479
diff
changeset
|
178 if braced is not None: |
|
2623668c5ff5
Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents:
33479
diff
changeset
|
179 try: |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
34127
diff
changeset
|
180 return '%s' % (mapping[braced],) |
|
33482
2623668c5ff5
Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents:
33479
diff
changeset
|
181 except KeyError: |
|
33505
cc0ae03743b8
At the cost of a modest (but useful in its own right) change in the semantics
Barry Warsaw <barry@python.org>
parents:
33497
diff
changeset
|
182 return self.delimiter + '{' + braced + '}' |
|
33479
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
183 if mo.group('escaped') is not None: |
|
33505
cc0ae03743b8
At the cost of a modest (but useful in its own right) change in the semantics
Barry Warsaw <barry@python.org>
parents:
33497
diff
changeset
|
184 return self.delimiter |
|
33479
b87eaa46c9cb
Raymond's good suggestion to re-order the tests in the convert() helper so the
Barry Warsaw <barry@python.org>
parents:
33477
diff
changeset
|
185 if mo.group('invalid') is not None: |
|
33871
5afa5277fcd9
SF patch #1056967, changes the semantics of Template.safe_substitute() to not
Barry Warsaw <barry@python.org>
parents:
33773
diff
changeset
|
186 return self.delimiter |
|
33773
ab09daac5691
Invalid patterns to substitute and safe_substitute would crash since pattern
Neal Norwitz <nnorwitz@gmail.com>
parents:
33588
diff
changeset
|
187 raise ValueError('Unrecognized named group in pattern', |
|
ab09daac5691
Invalid patterns to substitute and safe_substitute would crash since pattern
Neal Norwitz <nnorwitz@gmail.com>
parents:
33588
diff
changeset
|
188 self.pattern) |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
189 return self.pattern.sub(convert, self.template) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
190 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
191 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
192 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
193 ######################################################################## |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
194 # the Formatter class |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
195 # see PEP 3101 for details and purpose of this class |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
196 |
|
49734
b0fcb17f7efa
Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43947
diff
changeset
|
197 # The hard parts are reused from the C implementation. They're exposed as "_" |
|
b0fcb17f7efa
Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43947
diff
changeset
|
198 # prefixed methods of str and unicode. |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
199 |
|
42896
7bf8f2339ec6
PEP 3101: Removed _formatter_xxx routines from sysmodule, and made them unicode methods instead (per GvR suggestion).
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
200 # The overall parser is implemented in str._formatter_parser. |
|
7bf8f2339ec6
PEP 3101: Removed _formatter_xxx routines from sysmodule, and made them unicode methods instead (per GvR suggestion).
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
201 # The field name parser is implemented in str._formatter_field_name_split |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
202 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
203 class Formatter: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
204 def format(self, format_string, *args, **kwargs): |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
205 return self.vformat(format_string, args, kwargs) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
206 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
207 def vformat(self, format_string, args, kwargs): |
|
43112
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43020
diff
changeset
|
208 used_args = set() |
|
43241
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
209 result = self._vformat(format_string, args, kwargs, used_args, 2) |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
210 self.check_unused_args(used_args, args, kwargs) |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
211 return result |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
212 |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
213 def _vformat(self, format_string, args, kwargs, used_args, recursion_depth): |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
214 if recursion_depth < 0: |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
215 raise ValueError('Max string recursion exceeded') |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
216 result = [] |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
217 for literal_text, field_name, format_spec, conversion in \ |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
218 self.parse(format_string): |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
219 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
220 # output the literal text |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
221 if literal_text: |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
222 result.append(literal_text) |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
223 |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
224 # if there's a field, output it |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
225 if field_name is not None: |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
226 # this is some markup, find the object and do |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
227 # the formatting |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
228 |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
229 # given the field_name, find the object it references |
|
43112
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43020
diff
changeset
|
230 # and the argument it came from |
|
43200
463b341a9d63
Removed used_args param from string.Formatter.get_field. It was left in by mistake from an earlier edit.
Eric Smith <eric@trueblade.com>
parents:
43144
diff
changeset
|
231 obj, arg_used = self.get_field(field_name, args, kwargs) |
|
43112
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43020
diff
changeset
|
232 used_args.add(arg_used) |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
233 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
234 # do any conversion on the resulting object |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
235 obj = self.convert_field(obj, conversion) |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
236 |
|
43241
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
237 # expand the format spec, if needed |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
238 format_spec = self._vformat(format_spec, args, kwargs, |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
239 used_args, recursion_depth-1) |
|
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
240 |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
241 # format the object and append to the result |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
242 result.append(self.format_field(obj, format_spec)) |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
243 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
244 return ''.join(result) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
245 |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
246 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
247 def get_value(self, key, args, kwargs): |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
248 if isinstance(key, int): |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
249 return args[key] |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
250 else: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
251 return kwargs[key] |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
252 |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
253 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
254 def check_unused_args(self, used_args, args, kwargs): |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
255 pass |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
256 |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
257 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
258 def format_field(self, value, format_spec): |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
259 return format(value, format_spec) |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
260 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
261 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
262 def convert_field(self, value, conversion): |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
263 # do any conversion on the resulting object |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
264 if conversion == 'r': |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
265 return repr(value) |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
266 elif conversion == 's': |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
267 return str(value) |
|
43241
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
268 elif conversion is None: |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
269 return value |
|
43241
e946073dee38
Changed some ValueError's to KeyError and IndexError.
Eric Smith <eric@trueblade.com>
parents:
43200
diff
changeset
|
270 raise ValueError("Unknown converion specifier {0!s}".format(conversion)) |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
271 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
272 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
273 # returns an iterable that contains tuples of the form: |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
274 # (literal_text, field_name, format_spec, conversion) |
|
42969
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
275 # literal_text can be zero length |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
276 # field_name can be None, in which case there's no |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
277 # object to format and output |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
278 # if field_name is not None, it is looked up, formatted |
|
0e510aff5e9a
Modified parsing of format strings, so that we always return
Eric Smith <eric@trueblade.com>
parents:
42948
diff
changeset
|
279 # with format_spec and conversion and then used |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
280 def parse(self, format_string): |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
281 return format_string._formatter_parser() |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
282 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
283 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
284 # given a field_name, find the object it references. |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
285 # field_name: the field being looked up, e.g. "0.name" |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
286 # or "lookup[3]" |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
287 # used_args: a set of which args have been used |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
288 # args, kwargs: as passed in to vformat |
|
43200
463b341a9d63
Removed used_args param from string.Formatter.get_field. It was left in by mistake from an earlier edit.
Eric Smith <eric@trueblade.com>
parents:
43144
diff
changeset
|
289 def get_field(self, field_name, args, kwargs): |
|
42948
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
290 first, rest = field_name._formatter_field_name_split() |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
291 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
292 obj = self.get_value(first, args, kwargs) |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
293 |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
294 # loop through the rest of the field_name, doing |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
295 # getattr or getitem as needed |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
296 for is_attr, i in rest: |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
297 if is_attr: |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
298 obj = getattr(obj, i) |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
299 else: |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
300 obj = obj[i] |
|
4ecbae07481f
Simplified tuple returned by string._formatter_parser to only have
Eric Smith <eric@trueblade.com>
parents:
42896
diff
changeset
|
301 |
|
43112
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43020
diff
changeset
|
302 return obj, first |
