Mercurial > cpython-lru_cache-weakref
annotate Lib/string.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 18989d09d584 |
| children | e8c46cc99df5 |
| 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 | 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 | 17 punctuation -- a string containing all characters considered punctuation |
| 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 | 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 | 24 lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 25 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
| 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 | 30 digits = '0123456789' |
| 31 hexdigits = digits + 'abcdef' + 'ABCDEF' | |
| 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 | 35 |
| 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 |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
38 # Note that Cookie.py bogusly uses _idmap :( |
|
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
|
39 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
|
40 _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
|
41 del l |
| 5 | 42 |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
43 # 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
|
44 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
45 # 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
|
46 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
|
47 """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
|
48 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
54 """ |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
55 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
|
56 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
57 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
58 # 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
|
59 _idmapL = None |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
60 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
|
61 """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
|
62 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
67 """ |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
68 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
|
69 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
|
70 global _idmapL |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
71 if not _idmapL: |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
72 _idmapL = map(None, _idmap) |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
73 L = _idmapL[:] |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
79 |
|
34127
4dcd78f3b5d1
Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents:
33871
diff
changeset
|
80 |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
81 #################################################################### |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
82 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
|
83 |
|
33471
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
84 class _multimap: |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
85 """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
|
86 |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
87 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
|
88 arguments. |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
89 """ |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
90 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
|
91 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
|
92 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
|
93 |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
94 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
|
95 try: |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
96 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
|
97 except KeyError: |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
98 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
|
99 |
|
41dbee5ed295
Accepted Raymond's patch to combine mapping and keyword arguments, with slight
Barry Warsaw <barry@python.org>
parents:
33450
diff
changeset
|
100 |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
101 class _TemplateMetaclass(type): |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
102 pattern = r""" |
|
33588
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
103 %(delim)s(?: |
|
68d11e439a0b
Make the regex pattern easier to read, understand, and modify
Raymond Hettinger <python@rcn.com>
parents:
33505
diff
changeset
|
104 (?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
|
105 (?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
|
106 {(?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
|
107 (?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
|
108 ) |
|
33439
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 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
111 def __init__(cls, name, bases, dct): |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
112 super(_TemplateMetaclass, cls).__init__(name, bases, dct) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
113 if 'pattern' in dct: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
114 pattern = cls.pattern |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
115 else: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
116 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
|
117 'delim' : _re.escape(cls.delimiter), |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
118 'id' : cls.idpattern, |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
119 } |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
120 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
|
121 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
122 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
123 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
|
124 """A string class for supporting $-substitutions.""" |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
125 __metaclass__ = _TemplateMetaclass |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
126 |
|
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
|
127 delimiter = '$' |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
128 idpattern = r'[_a-z][_a-z0-9]*' |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
129 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
130 def __init__(self, template): |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
131 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
|
132 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
133 # 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
|
134 |
|
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
|
135 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
|
136 i = mo.start('invalid') |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
137 lines = self.template[:i].splitlines(True) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
138 if not lines: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
139 colno = 1 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
140 lineno = 1 |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
141 else: |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
142 colno = i - len(''.join(lines[:-1])) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
143 lineno = len(lines) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
144 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
|
145 (lineno, colno)) |
|
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
146 |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 if not args: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
151 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
|
152 elif kws: |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
153 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
|
154 else: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
155 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
|
156 # 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
|
157 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
|
158 # 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
|
159 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
|
160 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
|
161 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
|
162 # 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
|
163 # 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
|
164 return '%s' % (val,) |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 self.pattern) |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
171 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
|
172 |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 if not args: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
177 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
|
178 elif kws: |
|
33477
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
179 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
|
180 else: |
|
a220073536d6
substitute(), safe_substitute(): Paul Moore provides a better hack for dealing
Barry Warsaw <barry@python.org>
parents:
33471
diff
changeset
|
181 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
|
182 # 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
|
183 def convert(mo): |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
184 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
|
185 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
|
186 try: |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
187 # 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
|
188 # 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
|
189 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
|
190 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
|
191 return self.delimiter + named |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
192 braced = mo.group('braced') |
|
33482
2623668c5ff5
Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents:
33479
diff
changeset
|
193 if braced is not None: |
|
2623668c5ff5
Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents:
33479
diff
changeset
|
194 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
|
195 return '%s' % (mapping[braced],) |
|
33482
2623668c5ff5
Fix small bugs in Template code.
Raymond Hettinger <python@rcn.com>
parents:
33479
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 self.pattern) |
|
33439
629200d880ea
Many updates to PEP 292 templates. Summary:
Barry Warsaw <barry@python.org>
parents:
33278
diff
changeset
|
204 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
|
205 |
|
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
206 |
|
34127
4dcd78f3b5d1
Remove outdated references to the regsub module.
Raymond Hettinger <python@rcn.com>
parents:
33871
diff
changeset
|
207 |
|
33278
2b136441035c
Minor improvements to the template code.
Raymond Hettinger <python@rcn.com>
parents:
33257
diff
changeset
|
208 #################################################################### |
|
33257
11a4fbc88a80
PEP 292 classes Template and SafeTemplate are added to the string module.
Barry Warsaw <barry@python.org>
parents:
31199
diff
changeset
|
209 # 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
|
210 # 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
|
211 |
|
1788
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
212 # 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 |
| 5 | 218 # convert UPPER CASE letters to lower case |
| 219 def lower(s): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
220 """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
|
221 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
222 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
|
223 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
224 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
225 return s.lower() |
| 5 | 226 |
| 227 # Convert lower case letters to UPPER CASE | |
| 228 def upper(s): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
229 """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
|
230 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
231 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
|
232 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
233 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
234 return s.upper() |
| 5 | 235 |
| 236 # Swap lower case letters and UPPER CASE | |
| 237 def swapcase(s): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
238 """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
|
239 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
240 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
|
241 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
|
242 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
243 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
244 return s.swapcase() |
| 5 | 245 |
| 246 # 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
|
247 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
|
248 """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
|
249 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
250 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
|
251 whitespace removed. |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
252 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
|
253 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
|
254 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
255 """ |
|
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
|
256 return s.strip(chars) |
| 5 | 257 |
|
3957
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
258 # 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
|
259 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
|
260 """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
|
261 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
262 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
|
263 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
|
264 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
265 """ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
266 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
|
267 |
|
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
268 # 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
|
269 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
|
270 """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
|
271 |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
272 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
|
273 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
|
274 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
275 """ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
276 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
|
277 |
|
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
278 |
| 5 | 279 # 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
|
280 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
|
281 """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
|
282 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
283 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
|
284 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
|
285 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
|
286 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
|
287 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
288 (split and splitfields are synonymous) |
| 5 | 289 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
290 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
291 return s.split(sep, maxsplit) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
292 splitfields = split |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
293 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
294 # 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
|
295 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
|
296 """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
|
297 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
298 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
|
299 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
|
300 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
|
301 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
|
302 is a separator. |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
303 """ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
304 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
|
305 |
|
2778
17fdec2fb309
make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents:
2541
diff
changeset
|
306 # 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
|
307 def join(words, sep = ' '): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
308 """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
|
309 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
310 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
|
311 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
|
312 single space. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
313 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
314 (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
|
315 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
316 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
317 return sep.join(words) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
318 joinfields = join |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
319 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
320 # 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
|
321 def index(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
322 """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
|
323 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
324 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
|
325 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
326 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
327 return s.index(*args) |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
328 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
329 # 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
|
330 def rindex(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
331 """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
|
332 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
333 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
|
334 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
335 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
336 return s.rindex(*args) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
337 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
338 # 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
|
339 def count(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
340 """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
|
341 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
342 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
|
343 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
|
344 interpreted as in slice notation. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
345 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
346 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
347 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
|
348 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
349 # 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
|
350 def find(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
351 """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
|
352 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
353 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
|
354 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
|
355 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
|
356 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
357 Return -1 on failure. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
358 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
359 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
360 return s.find(*args) |
| 5 | 361 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
362 # 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
|
363 def rfind(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
364 """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
|
365 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
366 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
|
367 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
|
368 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
|
369 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
370 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
|
371 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
372 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
373 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
|
374 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
375 # for a bit of speed |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
376 _float = float |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
377 _int = int |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
378 _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
|
379 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
380 # Convert string to float |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
381 def atof(s): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
382 """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
|
383 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
384 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
|
385 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
386 """ |
|
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
|
387 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
|
388 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
389 |
| 5 | 390 # 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
|
391 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
|
392 """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
|
393 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 accepted. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
400 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
401 """ |
|
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
|
402 return _int(s, base) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
403 |
| 5 | 404 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
405 # 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
|
406 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
|
407 """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
|
408 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
409 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 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
|
415 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
|
416 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
417 """ |
|
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
|
418 return _long(s, base) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
419 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
420 |
| 5 | 421 # 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
|
422 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
|
423 """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
|
424 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
425 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
|
426 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
|
427 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
|
428 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
429 """ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
430 return s.ljust(width, *args) |
| 5 | 431 |
| 432 # 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
|
433 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
|
434 """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
|
435 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
436 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
|
437 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
|
438 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
|
439 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
440 """ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
441 return s.rjust(width, *args) |
| 5 | 442 |
| 443 # 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
|
444 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
|
445 """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
|
446 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
447 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
|
448 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
|
449 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
|
450 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
451 """ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
452 return s.center(width, *args) |
| 5 | 453 |
| 454 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' | |
| 455 # Decadent feature: the argument may be a string or a number | |
| 456 # (Use of this is deprecated; it should be a string as with ljust c.s.) | |
| 457 def zfill(x, width): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
458 """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
|
459 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
460 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
|
461 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
|
462 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
463 """ |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23045
diff
changeset
|
464 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
|
465 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
|
466 return x.zfill(width) |
|
724
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
467 |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
468 # Expand tabs in a string. |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
469 # Doesn't take non-printing chars into account, but does understand \n. |
| 2958 | 470 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
|
471 """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
|
472 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
473 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
|
474 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
|
475 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
|
476 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
477 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
478 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
|
479 |
|
3639
70183592370e
add translate() -- which was in strop per release 1.3
Guido van Rossum <guido@python.org>
parents:
2958
diff
changeset
|
480 # 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
|
481 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
|
482 """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
|
483 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
484 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
|
485 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
|
486 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
|
487 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
|
488 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
|
489 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
490 """ |
|
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 if 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 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
|
493 else: |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
494 # 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
|
495 # 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
|
496 # 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
|
497 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
|
498 |
|
3669
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
499 # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
|
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
500 def capitalize(s): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
501 """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
|
502 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
503 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
|
504 capitalized. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
505 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
506 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
507 return s.capitalize() |
|
3669
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
508 |
|
5213
5b407e0cbc71
Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
509 # 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
|
510 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
|
511 """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
|
512 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
513 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
|
514 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
|
515 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
|
516 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
517 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
518 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
|
519 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
520 |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
521 # 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
|
522 # 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
|
523 # 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
|
524 # 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
|
525 |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
526 try: |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
527 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
|
528 letters = lowercase + uppercase |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
529 except ImportError: |
| 11741 | 530 pass # Use the original versions |
