annotate Lib/string.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents acbfc69b55f3
children 5e3c61cb7014
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30633
c1a672ee1530 tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents: 28575
diff changeset
1 """A collection of string operations (most are no longer used).
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
30633
c1a672ee1530 tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents: 28575
diff changeset
3 Warning: most of the code you see here isn't normally used nowadays.
c1a672ee1530 tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents: 28575
diff changeset
4 Beginning with Python 1.6, many of these functions are implemented as
c1a672ee1530 tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents: 28575
diff changeset
5 methods on the standard string object. They used to be implemented by
c1a672ee1530 tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents: 28575
diff changeset
6 a built-in module called strop, but strop is now obsolete itself.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
7
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
8 Public module variables:
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
9
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
10 whitespace -- a string containing all characters considered whitespace
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
11 lowercase -- a string containing all characters considered lowercase letters
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
12 uppercase -- a string containing all characters considered uppercase letters
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
13 letters -- a string containing all characters considered letters
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
14 digits -- a string containing all characters considered decimal digits
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
15 hexdigits -- a string containing all characters considered hexadecimal digits
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
16 octdigits -- a string containing all characters considered octal digits
14670
8db8606d84ea Fix serious typo!
Fred Drake <fdrake@acm.org>
parents: 14665
diff changeset
17 punctuation -- a string containing all characters considered punctuation
8db8606d84ea Fix serious typo!
Fred Drake <fdrake@acm.org>
parents: 14665
diff changeset
18 printable -- a string containing all 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
19
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
20 """
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
21
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # 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
23 whitespace = ' \t\n\r\v\f'
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 lowercase = 'abcdefghijklmnopqrstuvwxyz'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 letters = lowercase + uppercase
18776
22b2f4b436af Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents: 16657
diff changeset
27 ascii_lowercase = lowercase
22b2f4b436af Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents: 16657
diff changeset
28 ascii_uppercase = uppercase
22b2f4b436af Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents: 16657
diff changeset
29 ascii_letters = ascii_lowercase + ascii_uppercase
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 digits = '0123456789'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 hexdigits = digits + 'abcdef' + 'ABCDEF'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 octdigits = '01234567'
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15610
diff changeset
33 punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
14665
6fcf5a70533c Richard Mortier <rmm1002@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
34 printable = digits + letters + punctuation + whitespace
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # Case conversion helpers
25786
af20aeaaa982 Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
37 # Use str to convert Unicode literal in case of -U
af20aeaaa982 Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
38 l = map(chr, xrange(256))
af20aeaaa982 Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
39 _idmap = str('').join(l)
af20aeaaa982 Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents: 23645
diff changeset
40 del l
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
42 # 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
43
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
44 # 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
45 def capwords(s, sep=None):
56229
acbfc69b55f3 Merged revisions 75070 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50000
diff changeset
46 """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
47
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
48 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
49 word using capitalize, and join the capitalized words using
56229
acbfc69b55f3 Merged revisions 75070 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50000
diff changeset
50 join. If the optional second argument sep is absent or None,
acbfc69b55f3 Merged revisions 75070 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50000
diff changeset
51 runs of whitespace characters are replaced by a single space
acbfc69b55f3 Merged revisions 75070 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50000
diff changeset
52 and leading and trailing whitespace are removed, otherwise
acbfc69b55f3 Merged revisions 75070 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50000
diff changeset
53 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
54
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
55 """
56229
acbfc69b55f3 Merged revisions 75070 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50000
diff changeset
56 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
57
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
58
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
59 # Construct a translation string
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
60 _idmapL = None
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
61 def maketrans(fromstr, tostr):
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
62 """maketrans(frm, to) -> string
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
63
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
64 Return a translation table (a string of 256 bytes long)
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
65 suitable for use in string.translate. The strings frm and to
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
66 must be of the same length.
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
67
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
68 """
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
69 if len(fromstr) != len(tostr):
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
70 raise ValueError, "maketrans arguments must have same length"
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
71 global _idmapL
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
72 if not _idmapL:
48018
49726dca16d8 Replace all map(None, a) with list(a).
Georg Brandl <georg@python.org>
parents: 45381
diff changeset
73 _idmapL = list(_idmap)
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
74 L = _idmapL[:]
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
75 fromstr = map(ord, fromstr)
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
76 for i in range(len(fromstr)):
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
77 L[fromstr[i]] = tostr[i]
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
78 return ''.join(L)
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
79
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
80
34127
4dcd78f3b5d1 Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents: 33871
diff changeset
81
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
82 ####################################################################
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
83 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
84
33471
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
85 class _multimap:
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
86 """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
87
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
88 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
89 arguments.
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
90 """
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
91 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
92 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
93 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
94
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
95 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
96 try:
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
97 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
98 except KeyError:
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
99 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
100
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
101
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
102 class _TemplateMetaclass(type):
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
103 pattern = r"""
33588
68d11e439a0b Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents: 33505
diff changeset
104 %(delim)s(?:
68d11e439a0b Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents: 33505
diff changeset
105 (?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
106 (?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
107 {(?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
108 (?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
109 )
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
110 """
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
111
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
112 def __init__(cls, name, bases, dct):
41287
12c70558074e Add a type.__init__() method that enforces the same signature as
Guido van Rossum <guido@python.org>
parents: 41281
diff changeset
113 super(_TemplateMetaclass, cls).__init__(name, bases, dct)
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
114 if 'pattern' in dct:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
115 pattern = cls.pattern
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
116 else:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
117 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
118 'delim' : _re.escape(cls.delimiter),
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
119 'id' : cls.idpattern,
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
120 }
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
121 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
122
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
123
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
124 class Template:
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
125 """A string class for supporting $-substitutions."""
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
126 __metaclass__ = _TemplateMetaclass
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
127
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
128 delimiter = '$'
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
129 idpattern = r'[_a-z][_a-z0-9]*'
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
130
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
131 def __init__(self, template):
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
132 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
133
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
134 # 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
135
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
136 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
137 i = mo.start('invalid')
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
138 lines = self.template[:i].splitlines(True)
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
139 if not lines:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
140 colno = 1
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
141 lineno = 1
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
142 else:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
143 colno = i - len(''.join(lines[:-1]))
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
144 lineno = len(lines)
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
145 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
146 (lineno, colno))
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
147
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
148 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
149 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
150 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
151 if not args:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
152 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
153 elif kws:
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
154 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
155 else:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
156 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
157 # 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
158 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
159 # 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
160 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
161 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
162 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
163 # 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
164 # fail if val is a Unicode containing non-ASCII characters.
39003
18989d09d584 Fix bug in passing tuples to string.Template. All other values (with working
Thomas Wouters <thomas@python.org>
parents: 34127
diff changeset
165 return '%s' % (val,)
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
166 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
167 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
168 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
169 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
170 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
171 self.pattern)
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
172 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
173
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
174 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
175 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
176 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
177 if not args:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
178 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
179 elif kws:
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
180 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
181 else:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
182 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
183 # 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
184 def convert(mo):
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
185 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
186 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
187 try:
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
188 # 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
189 # will fail if val is a Unicode containing non-ASCII
39003
18989d09d584 Fix bug in passing tuples to string.Template. All other values (with working
Thomas Wouters <thomas@python.org>
parents: 34127
diff changeset
190 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
191 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
192 return self.delimiter + named
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
193 braced = mo.group('braced')
33482
2623668c5ff5 Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents: 33479
diff changeset
194 if braced is not None:
2623668c5ff5 Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents: 33479
diff changeset
195 try:
39003
18989d09d584 Fix bug in passing tuples to string.Template. All other values (with working
Thomas Wouters <thomas@python.org>
parents: 34127
diff changeset
196 return '%s' % (mapping[braced],)
33482
2623668c5ff5 Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents: 33479
diff changeset
197 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
198 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
199 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
200 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
201 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
202 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
203 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
204 self.pattern)
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
205 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
206
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
207
34127
4dcd78f3b5d1 Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents: 33871
diff changeset
208
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
209 ####################################################################
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
210 # NOTE: Everything below here is deprecated. Use string methods instead.
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
211 # This stuff will go away in Python 3.0.
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
212
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
213 # Backward compatible names for exceptions
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
214 index_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
215 atoi_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
216 atof_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
217 atol_error = ValueError
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
218
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 # convert UPPER CASE letters to lower case
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def lower(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
221 """lower(s) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
222
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
223 Return a copy of the string s converted to lowercase.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
224
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
225 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
226 return s.lower()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 # Convert lower case letters to UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 def upper(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
230 """upper(s) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
231
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
232 Return a copy of the string s converted to uppercase.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
233
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
234 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
235 return s.upper()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 # Swap lower case letters and UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 def swapcase(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
239 """swapcase(s) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
240
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
241 Return a copy of the string s with upper case characters
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
242 converted to lowercase and vice versa.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
243
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
244 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
245 return s.swapcase()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 # Strip leading and trailing tabs and spaces
26107
ef3f6929fc0e Make strip behave as documented. Will backport to 2.2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 25786
diff changeset
248 def strip(s, chars=None):
26213
a5d1e0814061 Update the docstring to match the code. Will backport.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26107
diff changeset
249 """strip(s [,chars]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
250
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
251 Return a copy of the string s with leading and trailing
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
252 whitespace removed.
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
253 If chars is given and not None, remove characters in chars instead.
26213
a5d1e0814061 Update the docstring to match the code. Will backport.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26107
diff changeset
254 If chars is unicode, S will be converted to unicode before stripping.
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
255
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
256 """
26107
ef3f6929fc0e Make strip behave as documented. Will backport to 2.2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 25786
diff changeset
257 return s.strip(chars)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
259 # Strip leading tabs and spaces
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
260 def lstrip(s, chars=None):
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
261 """lstrip(s [,chars]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
262
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
263 Return a copy of the string s with leading whitespace removed.
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
264 If chars is given and not None, remove characters in chars instead.
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
265
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
266 """
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
267 return s.lstrip(chars)
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
268
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
269 # Strip trailing tabs and spaces
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
270 def rstrip(s, chars=None):
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
271 """rstrip(s [,chars]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
272
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
273 Return a copy of the string s with trailing whitespace removed.
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
274 If chars is given and not None, remove characters in chars instead.
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
275
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
276 """
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
277 return s.rstrip(chars)
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
278
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
279
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 # Split a string into a list of space/tab-separated words
11991
81702a7354ec Marc-Andre Lemburg: the maxsplit argument for split() and replace()
Guido van Rossum <guido@python.org>
parents: 11741
diff changeset
281 def split(s, sep=None, maxsplit=-1):
11439
316be08c9ecd split() docstring: Made signature and description for the first
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
282 """split(s [,sep [,maxsplit]]) -> list of strings
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
283
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
284 Return a list of the words in the string s, using sep as the
21847
45731f1360ee string.split() docstring described the interpretation of the maxsplit
Fred Drake <fdrake@acm.org>
parents: 18776
diff changeset
285 delimiter string. If maxsplit is given, splits at no more than
45731f1360ee string.split() docstring described the interpretation of the maxsplit
Fred Drake <fdrake@acm.org>
parents: 18776
diff changeset
286 maxsplit places (resulting in at most maxsplit+1 words). If sep
33487
cfb2f5e09740 Make the hint about the None default less ambiguous.
Walter Dörwald <walter@livinglogic.de>
parents: 33486
diff changeset
287 is not specified or is None, any whitespace string is a separator.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
288
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
289 (split and splitfields are synonymous)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
291 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
292 return s.split(sep, maxsplit)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
293 splitfields = split
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
294
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
295 # Split a string into a list of space/tab-separated words
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
296 def rsplit(s, sep=None, maxsplit=-1):
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
297 """rsplit(s [,sep [,maxsplit]]) -> list of strings
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
298
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
299 Return a list of the words in the string s, using sep as the
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
300 delimiter string, starting at the end of the string and working
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
301 to the front. If maxsplit is given, at most maxsplit splits are
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
302 done. If sep is not specified or is None, any whitespace string
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
303 is a separator.
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
304 """
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
305 return s.rsplit(sep, maxsplit)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
306
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
307 # Join fields with optional separator
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
308 def join(words, sep = ' '):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
309 """join(list [,sep]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
310
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
311 Return a string composed of the words in list, with
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13315
diff changeset
312 intervening occurrences of sep. The default separator is a
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
313 single space.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
314
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
315 (joinfields and join are synonymous)
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
316
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
317 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
318 return sep.join(words)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
319 joinfields = join
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
320
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
321 # Find substring, raise exception if not found
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
322 def index(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
323 """index(s, sub [,start [,end]]) -> int
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
324
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
325 Like find but raises ValueError when the substring is not found.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
326
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
327 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
328 return s.index(*args)
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
329
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
330 # Find last substring, raise exception if not found
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
331 def rindex(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
332 """rindex(s, sub [,start [,end]]) -> int
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
333
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
334 Like rfind but raises ValueError when the substring is not found.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
335
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
336 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
337 return s.rindex(*args)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
338
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
339 # Count non-overlapping occurrences of substring
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
340 def count(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
341 """count(s, sub[, start[,end]]) -> int
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
342
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
343 Return the number of occurrences of substring sub in string
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
344 s[start:end]. Optional arguments start and end are
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
345 interpreted as in slice notation.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
346
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
347 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
348 return s.count(*args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
349
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
350 # Find substring, return -1 if not found
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
351 def find(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
352 """find(s, sub [,start [,end]]) -> in
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
353
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
354 Return the lowest index in s where substring sub is found,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
355 such that sub is contained within s[start,end]. Optional
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
356 arguments start and end are interpreted as in slice notation.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
357
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
358 Return -1 on failure.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
359
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
360 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
361 return s.find(*args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
363 # Find last substring, return -1 if not found
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
364 def rfind(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
365 """rfind(s, sub [,start [,end]]) -> int
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
366
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
367 Return the highest index in s where substring sub is found,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
368 such that sub is contained within s[start,end]. Optional
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
369 arguments start and end are interpreted as in slice notation.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
370
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
371 Return -1 on failure.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
372
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
373 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
374 return s.rfind(*args)
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
375
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
376 # for a bit of speed
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
377 _float = float
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
378 _int = int
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
379 _long = long
6821
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
380
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
381 # Convert string to float
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
382 def atof(s):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
383 """atof(s) -> float
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
384
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
385 Return the floating point number represented by the string s.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
386
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
387 """
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11991
diff changeset
388 return _float(s)
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11991
diff changeset
389
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
390
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 # Convert string to integer
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11991
diff changeset
392 def atoi(s , base=10):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
393 """atoi(s [,base]) -> int
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
394
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
395 Return the integer represented by the string s in the given
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
396 base, which defaults to 10. The string s must consist of one
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
397 or more digits, possibly preceded by a sign. If base is 0, it
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
398 is chosen from the leading characters of s, 0 for octal, 0x or
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
399 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
400 accepted.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
401
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
402 """
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11991
diff changeset
403 return _int(s, base)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
404
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
406 # Convert string to long integer
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11991
diff changeset
407 def atol(s, base=10):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
408 """atol(s [,base]) -> long
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
409
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
410 Return the long integer represented by the string s in the
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
411 given base, which defaults to 10. The string s must consist
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
412 of one or more digits, possibly preceded by a sign. If base
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
413 is 0, it is chosen from the leading characters of s, 0 for
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
414 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
415 0x or 0X is accepted. A trailing L or l is not accepted,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
416 unless base is 0.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
417
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
418 """
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11991
diff changeset
419 return _long(s, base)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
420
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
421
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 # Left-justify a string
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
423 def ljust(s, width, *args):
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
424 """ljust(s, width[, fillchar]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
425
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
426 Return a left-justified version of s, in a field of the
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
427 specified width, padded with spaces as needed. The string is
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
428 never truncated. If specified the fillchar is used instead of spaces.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
429
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
430 """
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
431 return s.ljust(width, *args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 # Right-justify a string
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
434 def rjust(s, width, *args):
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
435 """rjust(s, width[, fillchar]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
436
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
437 Return a right-justified version of s, in a field of the
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
438 specified width, padded with spaces as needed. The string is
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
439 never truncated. If specified the fillchar is used instead of spaces.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
440
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
441 """
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
442 return s.rjust(width, *args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 # Center a string
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
445 def center(s, width, *args):
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
446 """center(s, width[, fillchar]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
447
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
448 Return a center version of s, in a field of the specified
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
449 width. padded with spaces as needed. The string is never
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
450 truncated. If specified the fillchar is used instead of spaces.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
451
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
452 """
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
453 return s.center(width, *args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 # Decadent feature: the argument may be a string or a number
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 # (Use of this is deprecated; it should be a string as with ljust c.s.)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 def zfill(x, width):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
459 """zfill(x, width) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
460
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
461 Pad a numeric string x with zeros on the left, to fill a field
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
462 of the specified width. The string x is never truncated.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
463
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
464 """
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
465 if not isinstance(x, basestring):
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 22660
diff changeset
466 x = repr(x)
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 22660
diff changeset
467 return x.zfill(width)
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
468
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
469 # Expand tabs in a string.
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
470 # Doesn't take non-printing chars into account, but does understand \n.
2958
4f9d4e73057c default tabsize to 8
Guido van Rossum <guido@python.org>
parents: 2778
diff changeset
471 def expandtabs(s, tabsize=8):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
472 """expandtabs(s [,tabsize]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
473
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
474 Return a copy of the string s with all tab characters replaced
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
475 by the appropriate number of spaces, depending on the current
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
476 column, and the tabsize (default 8).
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
477
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
478 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
479 return s.expandtabs(tabsize)
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
480
3639
70183592370e add translate() -- which was in strop per release 1.3
Guido van Rossum <guido@python.org>
parents: 2958
diff changeset
481 # Character translation through look-up table.
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
482 def translate(s, table, deletions=""):
15610
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
483 """translate(s,table [,deletions]) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
484
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
485 Return a copy of the string s, where all characters occurring
15610
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
486 in the optional argument deletions are removed, and the
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
487 remaining characters have been mapped through the given
15610
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
488 translation table, which must be a string of length 256. The
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
489 deletions argument is not allowed for Unicode strings.
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
490
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
491 """
41440
cedc8d7d1c7f SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents: 41287
diff changeset
492 if deletions or table is None:
15610
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
493 return s.translate(table, deletions)
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
494 else:
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
495 # Add s[:0] so that if s is Unicode and table is an 8-bit string,
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
496 # table is converted to Unicode. This means that table *cannot*
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
497 # be a dictionary -- for that feature, use u.translate() directly.
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
498 return s.translate(table + s[:0])
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
499
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
500 # Capitalize a string, e.g. "aBc dEf" -> "Abc def".
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
501 def capitalize(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
502 """capitalize(s) -> string
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
503
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
504 Return a copy of the string s with only its first character
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
505 capitalized.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
506
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
507 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
508 return s.capitalize()
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
509
5213
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
510 # Substring replacement (global)
11991
81702a7354ec Marc-Andre Lemburg: the maxsplit argument for split() and replace()
Guido van Rossum <guido@python.org>
parents: 11741
diff changeset
511 def replace(s, old, new, maxsplit=-1):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
512 """replace (str, old, new[, maxsplit]) -> string
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
513
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
514 Return a copy of string str with all occurrences of substring
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
515 old replaced by new. If the optional argument maxsplit is
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
516 given, only the first maxsplit occurrences are replaced.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
517
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
518 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
519 return s.replace(old, new, maxsplit)
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
520
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
521
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
522 # Try importing optional built-in module "strop" -- if it exists,
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
523 # it redefines some string operations that are 100-1000 times faster.
1331
07900834cc97 * pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents: 1291
diff changeset
524 # It also defines values for whitespace, lowercase and uppercase
07900834cc97 * pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents: 1291
diff changeset
525 # that match <ctype.h>'s definitions.
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
526
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
527 try:
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
528 from strop import maketrans, lowercase, uppercase, whitespace
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
529 letters = lowercase + uppercase
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
530 except ImportError:
11741
da7f5f6a849d Detabify.
Fred Drake <fdrake@acm.org>
parents: 11707
diff changeset
531 pass # Use the original versions
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
532
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
533 ########################################################################
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
534 # the Formatter class
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
535 # see PEP 3101 for details and purpose of this class
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
536
50000
89bc31e06489 Merged revisions 67154,67157-67159,67162-67163,67166,67175-67176,67189,67224-67225,67243 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48018
diff changeset
537 # The hard parts are reused from the C implementation. They're exposed as "_"
89bc31e06489 Merged revisions 67154,67157-67159,67162-67163,67166,67175-67176,67189,67224-67225,67243 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48018
diff changeset
538 # prefixed methods of str and unicode.
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
539
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
540 # The overall parser is implemented in str._formatter_parser.
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
541 # The field name parser is implemented in str._formatter_field_name_split
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
542
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
543 class Formatter(object):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
544 def format(self, format_string, *args, **kwargs):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
545 return self.vformat(format_string, args, kwargs)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
546
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
547 def vformat(self, format_string, args, kwargs):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
548 used_args = set()
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
549 result = self._vformat(format_string, args, kwargs, used_args, 2)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
550 self.check_unused_args(used_args, args, kwargs)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
551 return result
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
552
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
553 def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
554 if recursion_depth < 0:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
555 raise ValueError('Max string recursion exceeded')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
556 result = []
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
557 for literal_text, field_name, format_spec, conversion in \
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
558 self.parse(format_string):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
559
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
560 # output the literal text
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
561 if literal_text:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
562 result.append(literal_text)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
563
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
564 # if there's a field, output it
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
565 if field_name is not None:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
566 # this is some markup, find the object and do
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
567 # the formatting
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
568
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
569 # given the field_name, find the object it references
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
570 # and the argument it came from
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
571 obj, arg_used = self.get_field(field_name, args, kwargs)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
572 used_args.add(arg_used)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
573
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
574 # do any conversion on the resulting object
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
575 obj = self.convert_field(obj, conversion)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
576
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
577 # expand the format spec, if needed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
578 format_spec = self._vformat(format_spec, args, kwargs,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
579 used_args, recursion_depth-1)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
580
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
581 # format the object and append to the result
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
582 result.append(self.format_field(obj, format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
583
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
584 return ''.join(result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
585
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
586
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
587 def get_value(self, key, args, kwargs):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
588 if isinstance(key, (int, long)):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
589 return args[key]
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
590 else:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
591 return kwargs[key]
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
592
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
593
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
594 def check_unused_args(self, used_args, args, kwargs):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
595 pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
596
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
597
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
598 def format_field(self, value, format_spec):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
599 return format(value, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
600
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
601
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
602 def convert_field(self, value, conversion):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
603 # do any conversion on the resulting object
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
604 if conversion == 'r':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
605 return repr(value)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
606 elif conversion == 's':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
607 return str(value)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
608 elif conversion is None:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
609 return value
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
610 raise ValueError("Unknown converion specifier {0!s}".format(conversion))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
611
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
612
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
613 # returns an iterable that contains tuples of the form:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
614 # (literal_text, field_name, format_spec, conversion)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
615 # literal_text can be zero length
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
616 # field_name can be None, in which case there's no
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
617 # object to format and output
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
618 # if field_name is not None, it is looked up, formatted
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
619 # with format_spec and conversion and then used
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
620 def parse(self, format_string):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
621 return format_string._formatter_parser()
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
622
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
623
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
624 # given a field_name, find the object it references.
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
625 # field_name: the field being looked up, e.g. "0.name"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
626 # or "lookup[3]"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
627 # used_args: a set of which args have been used
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
628 # args, kwargs: as passed in to vformat
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
629 def get_field(self, field_name, args, kwargs):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
630 first, rest = field_name._formatter_field_name_split()
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
631
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
632 obj = self.get_value(first, args, kwargs)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
633
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
634 # loop through the rest of the field_name, doing
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
635 # getattr or getitem as needed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
636 for is_attr, i in rest:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
637 if is_attr:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
638 obj = getattr(obj, i)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
639 else:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
640 obj = obj[i]
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
641
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
642 return obj, first