annotate Lib/stringold.py @ 7278:59d2761ba1cf legacy-trunk

Remove obsolete pagenumbering / pagestyle cruft.
author Fred Drake <fdrake@acm.org>
date Fri, 13 Feb 1998 07:08:56 +0000
parents 744e2b68f3b6
children b4b0e4e759c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # module 'string' -- A collection of string operations
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
1260
b120cdd30fbc * Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents: 1095
diff changeset
3 # Warning: most of the code you see here isn't normally used nowadays.
b120cdd30fbc * Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents: 1095
diff changeset
4 # At the end of this file most functions are replaced by built-in
b120cdd30fbc * Fix bug in tzparse.py for DST timezone
Guido van Rossum <guido@python.org>
parents: 1095
diff changeset
5 # functions imported from built-in module "strop".
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
7 """Common string manipulations.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
8
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
9 Public module variables:
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
10
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
11 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
12 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
13 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
14 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
15 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
16 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
17 octdigits -- a string containing all characters considered octal digits
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
18
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
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # 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
22 whitespace = ' \t\n\r\v\f'
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 lowercase = 'abcdefghijklmnopqrstuvwxyz'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 letters = lowercase + uppercase
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 digits = '0123456789'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 hexdigits = digits + 'abcdef' + 'ABCDEF'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 octdigits = '01234567'
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # Case conversion helpers
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
31 _idmap = ''
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
32 for i in range(256): _idmap = _idmap + chr(i)
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
33 _lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
34 _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
35 _swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 del i
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
38 # 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
39 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
40 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
41 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
42 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
43
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # convert UPPER CASE letters to lower case
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def lower(s):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
46 """lower(s) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
47
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
48 Return a copy of the string s converted to lowercase.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
49
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
50 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 res = ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 for c in s:
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
53 res = res + _lower[ord(c)]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # Convert lower case letters to UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 def upper(s):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
58 """upper(s) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
59
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
60 Return a copy of the string s converted to uppercase.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
61
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
62 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 res = ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 for c in s:
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
65 res = res + _upper[ord(c)]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # Swap lower case letters and UPPER CASE
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def swapcase(s):
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
70 """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
71
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
72 Return a copy of the string s with upper case characters
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
73 converted to lowercase and vice versa.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
74
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
75 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 res = ''
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 for c in s:
585
37905fe1892a New implementation method for case conversion.
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
78 res = res + _swapcase[ord(c)]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # Strip leading and trailing tabs and spaces
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 def strip(s):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
83 """strip(s) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
84
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
85 Return a copy of the string s with leading and trailing
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
86 whitespace removed.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
87
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
88 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 i, j = 0, len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 while i < j and s[i] in whitespace: i = i+1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 while i < j and s[j-1] in whitespace: j = j-1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return s[i:j]
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
94 # Strip leading tabs and spaces
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
95 def lstrip(s):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
96 """lstrip(s) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
97
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
98 Return a copy of the string s with leading whitespace removed.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
99
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
100 """
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
101 i, j = 0, len(s)
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
102 while i < j and s[i] in whitespace: i = i+1
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
103 return s[i:j]
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
104
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
105 # Strip trailing tabs and spaces
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
106 def rstrip(s):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
107 """rstrip(s) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
108
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
109 Return a copy of the string s with trailing whitespace
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
110 removed.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
111
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
112 """
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
113 i, j = 0, len(s)
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
114 while i < j and s[j-1] in whitespace: j = j-1
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
115 return s[i:j]
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
116
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
117
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 # Split a string into a list of space/tab-separated words
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 # NB: split(s) is NOT the same as splitfields(s, ' ')!
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
120 def split(s, sep=None, maxsplit=0):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
121 """split(str [,sep [,maxsplit]]) -> list of strings
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
122
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
123 Return a list of the words in the string s, using sep as the
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
124 delimiter string. If maxsplit is nonzero, splits into at most
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
125 maxsplit words If sep is not specified, any whitespace string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
126 is a separator. Maxsplit defaults to 0.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
127
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
128 (split and splitfields are synonymous)
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
129
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
130 """
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
131 if sep is not None: return splitfields(s, sep, maxsplit)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 res = []
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 i, n = 0, len(s)
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
134 if maxsplit <= 0: maxsplit = n
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
135 count = 0
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 while i < n:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 while i < n and s[i] in whitespace: i = i+1
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
138 if i == n: break
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
139 if count >= maxsplit:
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
140 res.append(s[i:])
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
141 break
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 j = i
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 while j < n and s[j] not in whitespace: j = j+1
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
144 count = count + 1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 res.append(s[i:j])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 i = j
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 # Split a list into fields separated by a given string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # NB: splitfields(s, ' ') is NOT the same as split(s)!
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents: 980
diff changeset
151 # splitfields(s, '') returns [s] (in analogy with split() in nawk)
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
152 def splitfields(s, sep=None, maxsplit=0):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
153 """splitfields(str [,sep [,maxsplit]]) -> list of strings
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
154
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
155 Return a list of the words in the string s, using sep as the
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
156 delimiter string. If maxsplit is nonzero, splits into at most
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
157 maxsplit words If sep is not specified, any whitespace string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
158 is a separator. Maxsplit defaults to 0.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
159
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
160 (split and splitfields are synonymous)
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
161
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
162 """
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
163 if sep is None: return split(s, None, maxsplit)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 res = []
980
ffe2fbc583de splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents: 928
diff changeset
165 nsep = len(sep)
ffe2fbc583de splitfields(s, '') is illegal
Guido van Rossum <guido@python.org>
parents: 928
diff changeset
166 if nsep == 0:
1052
6508ea32c6b0 New module regsub contains sub(), gsub() and split() as in nawk.
Guido van Rossum <guido@python.org>
parents: 980
diff changeset
167 return [s]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 ns = len(s)
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
169 if maxsplit <= 0: maxsplit = ns
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 i = j = 0
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
171 count = 0
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 while j+nsep <= ns:
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
173 if s[j:j+nsep] == sep:
3957
f01e7af1dd60 Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
174 count = count + 1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 res.append(s[i:j])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 i = j = j + nsep
6691
d790fe25e148 In string.split(), honor maxsplit (if > 0).
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
177 if count >= maxsplit: break
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 else:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 j = j + 1
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 res.append(s[i:])
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return res
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
183 # Join words with spaces between them
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
184 def join(words, sep = ' '):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
185 """join(list [,sep]) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
186
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
187 Return a string composed of the words in list, with
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
188 intervening occurences of sep. Sep defaults to a single
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
189 space.
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
190
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
191 (joinfields and join are synonymous)
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
192
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
193 """
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
194 return joinfields(words, sep)
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
195
2778
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
196 # Join fields with optional separator
17fdec2fb309 make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents: 2541
diff changeset
197 def joinfields(words, sep = ' '):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
198 """joinfields(list [,sep]) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
199
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
200 Return a string composed of the words in list, with
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
201 intervening occurences of sep. The default separator is a
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
202 single space.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
203
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
204 (joinfields and join are synonymous)
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
205
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
206 """
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
207 res = ''
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
208 for w in words:
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
209 res = res + (sep + w)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
210 return res[len(sep):]
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
211
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
212 # Find substring, raise exception if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
213 def index(s, sub, i = 0, last=None):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
214 """index(s, sub [,start [,end]]) -> int
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
215
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
216 Return the lowest index in s where substring sub is found,
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
217 such that sub is contained within s[start,end]. Optional
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
218 arguments start and end are interpreted as in slice notation.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
219
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
220 Raise ValueError if not found.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
221
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
222 """
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
223 if last is None: last = len(s)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
224 res = find(s, sub, i, last)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
225 if res < 0:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
226 raise ValueError, 'substring not found in string.index'
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
227 return res
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
228
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
229 # Find last substring, raise exception if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
230 def rindex(s, sub, i = 0, last=None):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
231 """rindex(s, sub [,start [,end]]) -> int
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
232
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
233 Return the highest index in s where substring sub is found,
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
234 such that sub is contained within s[start,end]. Optional
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
235 arguments start and end are interpreted as in slice notation.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
236
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
237 Raise ValueError if not found.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
238
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
239 """
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
240 if last is None: last = len(s)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
241 res = rfind(s, sub, i, last)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
242 if res < 0:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
243 raise ValueError, 'substring not found in string.index'
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
244 return res
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
245
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
246 # Count non-overlapping occurrences of substring
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
247 def count(s, sub, i = 0, last=None):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
248 """count(s, sub[, start[,end]]) -> int
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
249
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
250 Return the number of occurrences of substring sub in string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
251 s[start:end]. Optional arguments start and end are
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
252 interpreted as in slice notation.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
253
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
254 """
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
255 Slen = len(s) # cache this value, for speed
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
256 if last is None:
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
257 last = Slen
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
258 elif last < 0:
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
259 last = max(0, last + Slen)
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
260 elif last > Slen:
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
261 last = Slen
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
262 if i < 0: i = max(0, i + Slen)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
263 n = len(sub)
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
264 m = last + 1 - n
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
265 if n == 0: return m-i
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
266 r = 0
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
267 while i < m:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
268 if sub == s[i:i+n]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
269 r = r+1
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
270 i = i+n
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
271 else:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
272 i = i+1
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
273 return r
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
274
1291
517d29ebc840 * ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents: 1260
diff changeset
275 # Find substring, return -1 if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
276 def find(s, sub, i = 0, last=None):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
277 """find(s, sub [,start [,end]]) -> in
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
278
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
279 Return the lowest index in s where substring sub is found,
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
280 such that sub is contained within s[start,end]. Optional
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
281 arguments start and end are interpreted as in slice notation.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
282
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
283 Return -1 on failure.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
284
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
285 """
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
286 Slen = len(s) # cache this value, for speed
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
287 if last is None:
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
288 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
289 elif last < 0:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
290 last = max(0, last + Slen)
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
291 elif last > Slen:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
292 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
293 if i < 0: i = max(0, i + Slen)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
294 n = len(sub)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
295 m = last + 1 - n
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
296 while i < m:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
297 if sub == s[i:i+n]: return i
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
298 i = i+1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
299 return -1
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
301 # Find last substring, return -1 if not found
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
302 def rfind(s, sub, i = 0, last=None):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
303 """rfind(s, sub [,start [,end]]) -> int
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
304
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
305 Return the highest index in s where substring sub is found,
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
306 such that sub is contained within s[start,end]. Optional
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
307 arguments start and end are interpreted as in slice notation.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
308
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
309 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
310
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
311 """
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
312 Slen = len(s) # cache this value, for speed
6504
071a0d3a49b9 Add optional 4th argument to count(), matching find() etc.
Guido van Rossum <guido@python.org>
parents: 5244
diff changeset
313 if last is None:
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
314 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
315 elif last < 0:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
316 last = max(0, last + Slen)
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
317 elif last > Slen:
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
318 last = Slen
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
319 if i < 0: i = max(0, i + Slen)
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
320 n = len(sub)
5188
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 4145
diff changeset
321 m = last + 1 - n
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
322 r = -1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
323 while i < m:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
324 if sub == s[i:i+n]: r = i
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
325 i = i+1
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
326 return r
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1357
diff changeset
327
6821
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
328 # "Safe" environment for eval()
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
329 safe_env = {"__builtins__": {}}
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
330
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
331 # Convert string to float
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
332 re = None
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
333 def atof(str):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
334 """atof(s) -> float
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
335
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
336 Return the floating point number represented by the string s.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
337
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
338 """
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
339 global re
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
340 if re is None:
6820
8ef481c7c3af Since this module is used as a fallback in case no built-in modules
Guido van Rossum <guido@python.org>
parents: 6691
diff changeset
341 # Don't fail if re doesn't exist -- just skip the syntax check
8ef481c7c3af Since this module is used as a fallback in case no built-in modules
Guido van Rossum <guido@python.org>
parents: 6691
diff changeset
342 try:
8ef481c7c3af Since this module is used as a fallback in case no built-in modules
Guido van Rossum <guido@python.org>
parents: 6691
diff changeset
343 import re
8ef481c7c3af Since this module is used as a fallback in case no built-in modules
Guido van Rossum <guido@python.org>
parents: 6691
diff changeset
344 except ImportError:
8ef481c7c3af Since this module is used as a fallback in case no built-in modules
Guido van Rossum <guido@python.org>
parents: 6691
diff changeset
345 re = 0
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
346 sign = ''
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6504
diff changeset
347 s = strip(str)
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
348 if s and s[0] in '+-':
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
349 sign = s[0]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
350 s = s[1:]
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
351 if not s:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
352 raise ValueError, 'non-float argument to string.atof'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
353 while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
6820
8ef481c7c3af Since this module is used as a fallback in case no built-in modules
Guido van Rossum <guido@python.org>
parents: 6691
diff changeset
354 if re and not re.match('[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?$', s):
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
355 raise ValueError, 'non-float argument to string.atof'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
356 try:
6821
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
357 return float(eval(sign + s, safe_env))
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
358 except SyntaxError:
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
359 raise ValueError, 'non-float argument to string.atof'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
360
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 # Convert string to integer
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
362 def atoi(str, base=10):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
363 """atoi(s [,base]) -> int
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
364
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
365 Return the integer represented by the string s in the given
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
366 base, which defaults to 10. The string s must consist of one
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
367 or more digits, possibly preceded by a sign. If base is 0, it
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
368 is chosen from the leading characters of s, 0 for octal, 0x or
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
369 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
370 accepted.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
371
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
372 """
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
373 if base != 10:
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
374 # We only get here if strop doesn't define atoi()
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
375 raise ValueError, "this string.atoi doesn't support base != 10"
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
376 sign = ''
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 s = str
1086
db817f3a1c65 * change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
378 if s and s[0] in '+-':
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
379 sign = s[0]
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
380 s = s[1:]
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
381 if not s:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
382 raise ValueError, 'non-integer argument to string.atoi'
928
8725fa8d7ff9 Removed *.libs (now in ./sgi);
Guido van Rossum <guido@python.org>
parents: 724
diff changeset
383 while s[0] == '0' and len(s) > 1: s = s[1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 for c in s:
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
385 if c not in digits:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
386 raise ValueError, 'non-integer argument to string.atoi'
6821
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
387 return eval(sign + s, safe_env)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
389 # Convert string to long integer
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
390 def atol(str, base=10):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
391 """atol(s [,base]) -> long
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
392
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
393 Return the long integer represented by the string s in the
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
394 given base, which defaults to 10. The string s must consist
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
395 of one or more digits, possibly preceded by a sign. If base
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
396 is 0, it is chosen from the leading characters of s, 0 for
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
397 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
398 0x or 0X is accepted. A trailing L or l is not accepted,
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
399 unless base is 0.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
400
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
401 """
2541
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
402 if base != 10:
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
403 # We only get here if strop doesn't define atol()
b72ccf5b7dbc add dummy base to atoi/atol; careful about negative start indices in find/count
Guido van Rossum <guido@python.org>
parents: 1788
diff changeset
404 raise ValueError, "this string.atol doesn't support base != 10"
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
405 sign = ''
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
406 s = str
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
407 if s and s[0] in '+-':
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
408 sign = s[0]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
409 s = s[1:]
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
410 if not s:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
411 raise ValueError, 'non-integer argument to string.atol'
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
412 while s[0] == '0' and len(s) > 1: s = s[1:]
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
413 for c in s:
1788
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
414 if c not in digits:
56a986e49f56 * Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
415 raise ValueError, 'non-integer argument to string.atol'
6821
b19cd4313126 At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents: 6820
diff changeset
416 return eval(sign + s + 'L', safe_env)
1357
2fac209e1c3f * profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents: 1331
diff changeset
417
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 # Left-justify a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 def ljust(s, width):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
420 """ljust(s, width) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
421
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
422 Return a left-justified version of s, in a field of the
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
423 specified width, padded with spaces as needed. The string is
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
424 never truncated.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
425
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
426 """
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
427 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
428 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
429 return s + ' '*n
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 def rjust(s, width):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
433 """rjust(s, width) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
434
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
435 Return a right-justified version of s, in a field of the
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
436 specified width, padded with spaces as needed. The string is
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
437 never truncated.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
438
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
439 """
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
440 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
441 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
442 return ' '*n + s
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 # Center a string
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 def center(s, width):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
446 """center(s, width) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
447
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
448 Return a center version of s, in a field of the specified
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
449 width. padded with spaces as needed. The string is never
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
450 truncated.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
451
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
452 """
196
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
453 n = width - len(s)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
454 if n <= 0: return s
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
455 half = n/2
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
456 if n%2 and width%2:
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
457 # This ensures that center(center(s, i), j) = center(s, j)
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
458 half = half+1
48fc0b8f33d9 Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents: 102
diff changeset
459 return ' '*half + s + ' '*(n-half)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 # 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
462 # Decadent feature: the argument may be a string or a number
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 # (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
464 def zfill(x, width):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
465 """zfill(x, width) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
466
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
467 Pad a numeric string x with zeros on the left, to fill a field
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
468 of the specified width. The string x is never truncated.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
469
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
470 """
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 365
diff changeset
471 if type(x) == type(''): s = x
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 else: s = `x`
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 n = len(s)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 if n >= width: return s
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 sign = ''
365
4e3073bb0e3e A plus is also a sign!
Guido van Rossum <guido@python.org>
parents: 196
diff changeset
476 if s[0] in ('-', '+'):
4e3073bb0e3e A plus is also a sign!
Guido van Rossum <guido@python.org>
parents: 196
diff changeset
477 sign, s = s[0], s[1:]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 return sign + '0'*(width-n) + s
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
479
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
480 # Expand tabs in a string.
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
481 # 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
482 def expandtabs(s, tabsize=8):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
483 """expandtabs(s [,tabsize]) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
484
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
485 Return a copy of the string s with all tab characters replaced
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
486 by the appropriate number of spaces, depending on the current
6967
744e2b68f3b6 Minor editing corrections.
Guido van Rossum <guido@python.org>
parents: 6964
diff changeset
487 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
488
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
489 """
724
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
490 res = line = ''
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
491 for c in s:
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
492 if c == '\t':
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
493 c = ' '*(tabsize - len(line)%tabsize)
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
494 line = line + c
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
495 if c == '\n':
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
496 res = res + line
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
497 line = ''
476bb7e5b854 Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents: 585
diff changeset
498 return res + line
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
499
3639
70183592370e add translate() -- which was in strop per release 1.3
Guido van Rossum <guido@python.org>
parents: 2958
diff changeset
500 # 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
501 def translate(s, table, deletions=""):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
502 """translate(s,table [,deletechars]) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
503
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
504 Return a copy of the string s, where all characters occurring
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
505 in the optional argument deletechars are removed, and the
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
506 remaining characters have been mapped through the given
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
507 translation table, which must be a string of length 256.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
508
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
509 """
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
510 if type(table) != type('') or len(table) != 256:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
511 raise TypeError, "translation table must be 256 characters long"
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
512 res = ""
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
513 for c in s:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
514 if c not in deletions:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
515 res = res + table[ord(c)]
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
516 return res
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
517
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
518 # Capitalize a string, e.g. "aBc dEf" -> "Abc def".
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
519 def capitalize(s):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
520 """capitalize(s) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
521
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
522 Return a copy of the string s with only its first character
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
523 capitalized.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
524
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
525 """
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
526 return upper(s[:1]) + lower(s[1:])
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
527
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
528 # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
529 # See also regsub.capwords().
4076
eda29bef880c Add optional separator character to capwords(), for completeness.
Guido van Rossum <guido@python.org>
parents: 3957
diff changeset
530 def capwords(s, sep=None):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
531 """capwords(s, [sep]) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
532
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
533 Split the argument into words using split, capitalize each
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
534 word using capitalize, and join the capitalized words using
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
535 join. Note that this replaces runs of whitespace characters by
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
536 a single space.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
537
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
538 """
4145
951ed9a83f57 Use correct separator for capwords(s, sep).
Guido van Rossum <guido@python.org>
parents: 4076
diff changeset
539 return join(map(capitalize, split(s, sep)), sep or ' ')
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
540
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
541 # Construct a translation string
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
542 _idmapL = None
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
543 def maketrans(fromstr, tostr):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
544 """maketrans(frm, to) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
545
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
546 Return a translation table (a string of 256 bytes long)
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
547 suitable for use in string.translate. The strings frm and to
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
548 must be of the same length.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
549
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
550 """
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
551 if len(fromstr) != len(tostr):
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
552 raise ValueError, "maketrans arguments must have same length"
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
553 global _idmapL
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
554 if not _idmapL:
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
555 _idmapL = map(None, _idmap)
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
556 L = _idmapL[:]
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
557 fromstr = map(ord, fromstr)
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
558 for i in range(len(fromstr)):
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
559 L[fromstr[i]] = tostr[i]
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3669
diff changeset
560 return joinfields(L, "")
3669
1d134ecda094 Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents: 3639
diff changeset
561
5213
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
562 # Substring replacement (global)
5244
247139d200e8 Changed my mind on replace().
Guido van Rossum <guido@python.org>
parents: 5213
diff changeset
563 def replace(str, old, new, maxsplit=0):
6964
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
564 """replace (str, old, new[, maxsplit]) -> string
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
565
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
566 Return a copy of string str with all occurrences of substring
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
567 old replaced by new. If the optional argument maxsplit is
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
568 given, only the first maxsplit occurrences are replaced.
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
569
33be7d5d498b Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents: 6821
diff changeset
570 """
5244
247139d200e8 Changed my mind on replace().
Guido van Rossum <guido@python.org>
parents: 5213
diff changeset
571 return joinfields(splitfields(str, old, maxsplit), new)
5213
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
572
5b407e0cbc71 Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
573
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
574 # 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
575 # 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
576 # 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
577 # 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
578
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
579 try:
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 1052
diff changeset
580 from strop import *
1331
07900834cc97 * pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents: 1291
diff changeset
581 letters = lowercase + uppercase
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
582 except ImportError:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
583 pass # Use the original, slow versions