annotate Lib/string.py @ 49955:878d40468c4e legacy-trunk

Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
author Vinay Sajip <vinay_sajip@yahoo.co.uk>
date Wed, 03 Dec 2008 23:22:58 +0000
parents 89bc31e06489
children acbfc69b55f3
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):
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
46 """capwords(s, [sep]) -> string
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
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
50 join. Note that this replaces runs of whitespace characters by
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
51 a single space.
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
52
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
53 """
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
54 return (sep or ' ').join([x.capitalize() for x in s.split(sep)])
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
55
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
56
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
57 # 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
58 _idmapL = None
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
59 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
60 """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
61
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
62 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
63 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
64 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
65
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
66 """
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
67 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
68 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
69 global _idmapL
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
70 if not _idmapL:
48018
49726dca16d8 Replace all map(None, a) with list(a).
Georg Brandl <georg@python.org>
parents: 45381
diff changeset
71 _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
72 L = _idmapL[:]
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
73 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
74 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
75 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
76 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
77
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
78
34127
4dcd78f3b5d1 Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents: 33871
diff changeset
79
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
80 ####################################################################
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
81 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
82
33471
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
83 class _multimap:
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
84 """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
85
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
86 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
87 arguments.
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
88 """
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
89 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
90 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
91 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
92
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
93 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
94 try:
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
95 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
96 except KeyError:
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._secondary[key]
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
98
41dbee5ed295 Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents: 33450
diff changeset
99
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
100 class _TemplateMetaclass(type):
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
101 pattern = r"""
33588
68d11e439a0b Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents: 33505
diff changeset
102 %(delim)s(?:
68d11e439a0b Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents: 33505
diff changeset
103 (?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
104 (?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
105 {(?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
106 (?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
107 )
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
108 """
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
109
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
110 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
111 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
112 if 'pattern' in dct:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
113 pattern = cls.pattern
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
114 else:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
115 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
116 'delim' : _re.escape(cls.delimiter),
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
117 'id' : cls.idpattern,
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
118 }
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
119 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
120
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
121
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
122 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
123 """A string class for supporting $-substitutions."""
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
124 __metaclass__ = _TemplateMetaclass
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
125
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
126 delimiter = '$'
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
127 idpattern = r'[_a-z][_a-z0-9]*'
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
128
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
129 def __init__(self, template):
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
130 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
131
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
132 # 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
133
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
134 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
135 i = mo.start('invalid')
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
136 lines = self.template[:i].splitlines(True)
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
137 if not lines:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
138 colno = 1
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
139 lineno = 1
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
140 else:
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
141 colno = i - len(''.join(lines[:-1]))
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
142 lineno = len(lines)
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
143 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
144 (lineno, colno))
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
145
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
146 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
147 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
148 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
149 if not args:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
150 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
151 elif kws:
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
152 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
153 else:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
154 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
155 # 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
156 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
157 # 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
158 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
159 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
160 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
161 # 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
162 # 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
163 return '%s' % (val,)
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
164 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
165 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
166 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
167 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
168 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
169 self.pattern)
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
170 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
171
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
172 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
173 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
174 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
175 if not args:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
176 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
177 elif kws:
33477
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
178 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
179 else:
a220073536d6 substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents: 33471
diff changeset
180 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
181 # 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
182 def convert(mo):
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
183 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
184 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
185 try:
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
186 # 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
187 # 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
188 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
189 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
190 return self.delimiter + named
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
191 braced = mo.group('braced')
33482
2623668c5ff5 Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents: 33479
diff changeset
192 if braced is not None:
2623668c5ff5 Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents: 33479
diff changeset
193 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
194 return '%s' % (mapping[braced],)
33482
2623668c5ff5 Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents: 33479
diff changeset
195 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
196 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
197 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
198 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
199 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
200 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
201 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
202 self.pattern)
33439
629200d880ea Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents: 33278
diff changeset
203 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
204
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
205
34127
4dcd78f3b5d1 Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents: 33871
diff changeset
206
33278
2b136441035c Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents: 33257
diff changeset
207 ####################################################################
33257
11a4fbc88a80 PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents: 31199
diff changeset
208 # 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
209 # 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
210
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
211 # 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
212 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
213 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
214 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
215 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
216
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 # convert UPPER CASE letters to lower case
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 def lower(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
219 """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
220
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
221 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
222
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
223 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
224 return s.lower()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 # Convert lower case letters to UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 def upper(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
228 """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
229
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
230 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
231
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
232 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
233 return s.upper()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 # Swap lower case letters and UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 def swapcase(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
237 """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
238
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
239 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
240 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
241
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
242 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
243 return s.swapcase()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 # 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
246 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
247 """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
248
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
249 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
250 whitespace removed.
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
251 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
252 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
253
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
254 """
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
255 return s.strip(chars)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
257 # 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
258 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
259 """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
260
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
261 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
262 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
263
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
264 """
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
265 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
266
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
267 # 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
268 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
269 """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
270
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
271 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
272 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
273
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
274 """
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 26213
diff changeset
275 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
276
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
277
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 # 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
279 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
280 """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
281
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
282 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
283 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
284 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
285 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
286
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
287 (split and splitfields are synonymous)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
289 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
290 return s.split(sep, maxsplit)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
291 splitfields = split
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
292
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
293 # 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
294 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
295 """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
296
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
297 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
298 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
299 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
300 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
301 is a separator.
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
302 """
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
303 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
304
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
305 # 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
306 def join(words, sep = ' '):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
307 """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
308
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
309 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
310 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
311 single space.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
312
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
313 (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
314
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
315 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
316 return sep.join(words)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
317 joinfields = join
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
318
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
319 # 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
320 def index(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
321 """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
322
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
323 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
324
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
325 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
326 return s.index(*args)
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
327
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
328 # 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
329 def rindex(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
330 """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
331
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
332 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
333
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
334 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
335 return s.rindex(*args)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
336
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
337 # 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
338 def count(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
339 """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
340
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
341 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
342 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
343 interpreted as in slice notation.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
344
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
345 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
346 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
347
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
348 # 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
349 def find(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
350 """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
351
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
352 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
353 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
354 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
355
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
356 Return -1 on failure.
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 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
359 return s.find(*args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
361 # 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
362 def rfind(s, *args):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
363 """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
364
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
365 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
366 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
367 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
368
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
369 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
370
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
371 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
372 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
373
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
374 # for a bit of speed
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
375 _float = float
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
376 _int = int
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
377 _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
378
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
379 # Convert string to float
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
380 def atof(s):
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
381 """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
382
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
383 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
384
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
385 """
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
386 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
387
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
388
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 # 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
390 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
391 """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
392
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
393 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
394 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
395 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
396 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
397 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
398 accepted.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
399
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
400 """
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
401 return _int(s, base)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
402
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
404 # 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
405 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
406 """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
407
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
408 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
409 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
410 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
411 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
412 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
413 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
414 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
415
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
416 """
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
417 return _long(s, base)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
418
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
419
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 # 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
421 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
422 """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
423
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
424 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
425 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
426 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
427
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
428 """
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
429 return s.ljust(width, *args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
430
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 # 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
432 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
433 """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
434
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
435 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
436 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
437 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
438
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
439 """
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
440 return s.rjust(width, *args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 # 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
443 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
444 """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
445
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
446 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
447 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
448 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
449
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
450 """
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30633
diff changeset
451 return s.center(width, *args)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 # 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
454 # Decadent feature: the argument may be a string or a number
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 # (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
456 def zfill(x, width):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
457 """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
458
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
459 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
460 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
461
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
462 """
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
463 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
464 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
465 return x.zfill(width)
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
466
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
467 # Expand tabs in a string.
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
468 # 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
469 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
470 """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
471
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
472 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
473 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
474 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
475
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
476 """
13315
58183679c90a Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents: 12248
diff changeset
477 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
478
3639
70183592370e add translate() -- which was in strop per release 1.3
Guido van Rossum <guido@python.org>
parents: 2958
diff changeset
479 # 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
480 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
481 """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
482
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
483 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
484 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
485 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
486 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
487 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
488
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
489 """
41440
cedc8d7d1c7f SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents: 41287
diff changeset
490 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
491 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
492 else:
ed315b55f19e Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents: 14670
diff changeset
493 # 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
494 # 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
495 # 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
496 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
497
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
498 # Capitalize a string, e.g. "aBc dEf" -> "Abc def".
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
499 def capitalize(s):
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
500 """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
501
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
502 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
503 capitalized.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
504
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
505 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
506 return s.capitalize()
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
507
5213
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
508 # 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
509 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
510 """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
511
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
512 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
513 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
514 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
515
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
516 """
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
517 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
518
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
519
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
520 # 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
521 # 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
522 # 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
523 # 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
524
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
525 try:
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11379
diff changeset
526 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
527 letters = lowercase + uppercase
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
528 except ImportError:
11741
da7f5f6a849d Detabify.
Fred Drake <fdrake@acm.org>
parents: 11707
diff changeset
529 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
530
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
531 ########################################################################
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
532 # the Formatter class
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
533 # 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
534
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
535 # 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
536 # 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
537
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
538 # 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
539 # 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
540
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
541 class Formatter(object):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
542 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
543 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
544
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
545 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
546 used_args = set()
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
547 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
548 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
549 return result
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
550
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
551 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
552 if recursion_depth < 0:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
553 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
554 result = []
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
555 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
556 self.parse(format_string):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
557
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
558 # output the literal text
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
559 if literal_text:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
560 result.append(literal_text)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
561
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
562 # 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
563 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
564 # 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
565 # the formatting
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
566
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
567 # 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
568 # 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
569 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
570 used_args.add(arg_used)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
571
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
572 # 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
573 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
574
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
575 # 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
576 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
577 used_args, recursion_depth-1)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
578
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
579 # 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
580 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
581
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
582 return ''.join(result)
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
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
585 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
586 if isinstance(key, (int, long)):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
587 return args[key]
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
588 else:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
589 return kwargs[key]
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
590
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
591
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
592 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
593 pass
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
594
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
595
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
596 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
597 return format(value, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
598
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
599
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
600 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
601 # 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
602 if conversion == 'r':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
603 return repr(value)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
604 elif conversion == 's':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
605 return str(value)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
606 elif conversion is None:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
607 return value
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
608 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
609
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
610
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
611 # 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
612 # (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
613 # 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
614 # 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
615 # object to format and output
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
616 # 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
617 # 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
618 def parse(self, format_string):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
619 return format_string._formatter_parser()
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
620
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
621
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
622 # 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
623 # 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
624 # or "lookup[3]"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
625 # 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
626 # 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
627 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
628 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
629
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
630 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
631
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
632 # 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
633 # getattr or getitem as needed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
634 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
635 if is_attr:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
636 obj = getattr(obj, i)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
637 else:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
638 obj = obj[i]
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
639
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 41440
diff changeset
640 return obj, first