1 """A collection of string operations (most are no longer used).
3 Warning: most of the code you see here isn't normally used nowadays.
4 Beginning with Python 1.6, many of these functions are implemented as
5 methods on the standard string object. They used to be implemented by
6 a built-in module called strop, but strop is now obsolete itself.
8 Public module variables:
10 whitespace -- a string containing all characters considered whitespace
11 lowercase -- a string containing all characters considered lowercase letters
12 uppercase -- a string containing all characters considered uppercase letters
13 letters -- a string containing all characters considered letters
14 digits -- a string containing all characters considered decimal digits
15 hexdigits -- a string containing all characters considered hexadecimal digits
16 octdigits -- a string containing all characters considered octal digits
17 punctuation -- a string containing all characters considered punctuation
18 printable -- a string containing all characters considered printable
22 # Some strings for ctype-style character classification
23 whitespace
= ' \t\n\r\v\f'
24 lowercase
= 'abcdefghijklmnopqrstuvwxyz'
25 uppercase
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
26 letters
= lowercase
+ uppercase
27 ascii_lowercase
= lowercase
28 ascii_uppercase
= uppercase
29 ascii_letters
= ascii_lowercase
+ ascii_uppercase
31 hexdigits
= digits
+ 'abcdef' + 'ABCDEF'
32 octdigits
= '01234567'
33 punctuation
= """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
34 printable
= digits
+ letters
+ punctuation
+ whitespace
36 # Case conversion helpers
37 # Use str to convert Unicode literal in case of -U
38 # Note that Cookie.py bogusly uses _idmap :(
39 l
= map(chr, xrange(256))
40 _idmap
= str('').join(l
)
43 # Functions which aren't available as string methods.
45 # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
46 def capwords(s
, sep
=None):
47 """capwords(s, [sep]) -> string
49 Split the argument into words using split, capitalize each
50 word using capitalize, and join the capitalized words using
51 join. Note that this replaces runs of whitespace characters by
55 return (sep
or ' ').join([x
.capitalize() for x
in s
.split(sep
)])
58 # Construct a translation string
60 def maketrans(fromstr
, tostr
):
61 """maketrans(frm, to) -> string
63 Return a translation table (a string of 256 bytes long)
64 suitable for use in string.translate. The strings frm and to
65 must be of the same length.
68 if len(fromstr
) != len(tostr
):
69 raise ValueError, "maketrans arguments must have same length"
72 _idmapL
= map(None, _idmap
)
74 fromstr
= map(ord, fromstr
)
75 for i
in range(len(fromstr
)):
76 L
[fromstr
[i
]] = tostr
[i
]
81 ####################################################################
85 """Helper class for combining multiple mappings.
87 Used by .{safe_,}substitute() to combine the mapping and keyword
90 def __init__(self
, primary
, secondary
):
91 self
._primary
= primary
92 self
._secondary
= secondary
94 def __getitem__(self
, key
):
96 return self
._primary
[key
]
98 return self
._secondary
[key
]
101 class _TemplateMetaclass(type):
104 (?P<escaped>%(delim)s) | # Escape sequence of two delimiters
105 (?P<named>%(id)s) | # delimiter and a Python identifier
106 {(?P<braced>%(id)s)} | # delimiter and a braced identifier
107 (?P<invalid>) # Other ill-formed delimiter exprs
111 def __init__(cls
, name
, bases
, dct
):
112 super(_TemplateMetaclass
, cls
).__init
__(name
, bases
, dct
)
114 pattern
= cls
.pattern
116 pattern
= _TemplateMetaclass
.pattern
% {
117 'delim' : _re
.escape(cls
.delimiter
),
118 'id' : cls
.idpattern
,
120 cls
.pattern
= _re
.compile(pattern
, _re
.IGNORECASE | _re
.VERBOSE
)
124 """A string class for supporting $-substitutions."""
125 __metaclass__
= _TemplateMetaclass
128 idpattern
= r
'[_a-z][_a-z0-9]*'
130 def __init__(self
, template
):
131 self
.template
= template
133 # Search for $$, $identifier, ${identifier}, and any bare $'s
135 def _invalid(self
, mo
):
136 i
= mo
.start('invalid')
137 lines
= self
.template
[:i
].splitlines(True)
142 colno
= i
- len(''.join(lines
[:-1]))
144 raise ValueError('Invalid placeholder in string: line %d, col %d' %
147 def substitute(self
, *args
, **kws
):
149 raise TypeError('Too many positional arguments')
153 mapping
= _multimap(kws
, args
[0])
156 # Helper function for .sub()
158 # Check the most common path first.
159 named
= mo
.group('named') or mo
.group('braced')
160 if named
is not None:
162 # We use this idiom instead of str() because the latter will
163 # fail if val is a Unicode containing non-ASCII characters.
165 if mo
.group('escaped') is not None:
166 return self
.delimiter
167 if mo
.group('invalid') is not None:
169 raise ValueError('Unrecognized named group in pattern',
171 return self
.pattern
.sub(convert
, self
.template
)
173 def safe_substitute(self
, *args
, **kws
):
175 raise TypeError('Too many positional arguments')
179 mapping
= _multimap(kws
, args
[0])
182 # Helper function for .sub()
184 named
= mo
.group('named')
185 if named
is not None:
187 # We use this idiom instead of str() because the latter
188 # will fail if val is a Unicode containing non-ASCII
189 return '%s' % mapping
[named
]
191 return self
.delimiter
+ named
192 braced
= mo
.group('braced')
193 if braced
is not None:
195 return '%s' % mapping
[braced
]
197 return self
.delimiter
+ '{' + braced
+ '}'
198 if mo
.group('escaped') is not None:
199 return self
.delimiter
200 if mo
.group('invalid') is not None:
201 return self
.delimiter
202 raise ValueError('Unrecognized named group in pattern',
204 return self
.pattern
.sub(convert
, self
.template
)
208 ####################################################################
209 # NOTE: Everything below here is deprecated. Use string methods instead.
210 # This stuff will go away in Python 3.0.
212 # Backward compatible names for exceptions
213 index_error
= ValueError
214 atoi_error
= ValueError
215 atof_error
= ValueError
216 atol_error
= ValueError
218 # convert UPPER CASE letters to lower case
220 """lower(s) -> string
222 Return a copy of the string s converted to lowercase.
227 # Convert lower case letters to UPPER CASE
229 """upper(s) -> string
231 Return a copy of the string s converted to uppercase.
236 # Swap lower case letters and UPPER CASE
238 """swapcase(s) -> string
240 Return a copy of the string s with upper case characters
241 converted to lowercase and vice versa.
246 # Strip leading and trailing tabs and spaces
247 def strip(s
, chars
=None):
248 """strip(s [,chars]) -> string
250 Return a copy of the string s with leading and trailing
252 If chars is given and not None, remove characters in chars instead.
253 If chars is unicode, S will be converted to unicode before stripping.
256 return s
.strip(chars
)
258 # Strip leading tabs and spaces
259 def lstrip(s
, chars
=None):
260 """lstrip(s [,chars]) -> string
262 Return a copy of the string s with leading whitespace removed.
263 If chars is given and not None, remove characters in chars instead.
266 return s
.lstrip(chars
)
268 # Strip trailing tabs and spaces
269 def rstrip(s
, chars
=None):
270 """rstrip(s [,chars]) -> string
272 Return a copy of the string s with trailing whitespace removed.
273 If chars is given and not None, remove characters in chars instead.
276 return s
.rstrip(chars
)
279 # Split a string into a list of space/tab-separated words
280 def split(s
, sep
=None, maxsplit
=-1):
281 """split(s [,sep [,maxsplit]]) -> list of strings
283 Return a list of the words in the string s, using sep as the
284 delimiter string. If maxsplit is given, splits at no more than
285 maxsplit places (resulting in at most maxsplit+1 words). If sep
286 is not specified or is None, any whitespace string is a separator.
288 (split and splitfields are synonymous)
291 return s
.split(sep
, maxsplit
)
294 # Split a string into a list of space/tab-separated words
295 def rsplit(s
, sep
=None, maxsplit
=-1):
296 """rsplit(s [,sep [,maxsplit]]) -> list of strings
298 Return a list of the words in the string s, using sep as the
299 delimiter string, starting at the end of the string and working
300 to the front. If maxsplit is given, at most maxsplit splits are
301 done. If sep is not specified or is None, any whitespace string
304 return s
.rsplit(sep
, maxsplit
)
306 # Join fields with optional separator
307 def join(words
, sep
= ' '):
308 """join(list [,sep]) -> string
310 Return a string composed of the words in list, with
311 intervening occurrences of sep. The default separator is a
314 (joinfields and join are synonymous)
317 return sep
.join(words
)
320 # Find substring, raise exception if not found
322 """index(s, sub [,start [,end]]) -> int
324 Like find but raises ValueError when the substring is not found.
327 return s
.index(*args
)
329 # Find last substring, raise exception if not found
330 def rindex(s
, *args
):
331 """rindex(s, sub [,start [,end]]) -> int
333 Like rfind but raises ValueError when the substring is not found.
336 return s
.rindex(*args
)
338 # Count non-overlapping occurrences of substring
340 """count(s, sub[, start[,end]]) -> int
342 Return the number of occurrences of substring sub in string
343 s[start:end]. Optional arguments start and end are
344 interpreted as in slice notation.
347 return s
.count(*args
)
349 # Find substring, return -1 if not found
351 """find(s, sub [,start [,end]]) -> in
353 Return the lowest index in s where substring sub is found,
354 such that sub is contained within s[start,end]. Optional
355 arguments start and end are interpreted as in slice notation.
357 Return -1 on failure.
362 # Find last substring, return -1 if not found
364 """rfind(s, sub [,start [,end]]) -> int
366 Return the highest index in s where substring sub is found,
367 such that sub is contained within s[start,end]. Optional
368 arguments start and end are interpreted as in slice notation.
370 Return -1 on failure.
373 return s
.rfind(*args
)
380 # Convert string to float
384 Return the floating point number represented by the string s.
390 # Convert string to integer
391 def atoi(s
, base
=10):
392 """atoi(s [,base]) -> int
394 Return the integer represented by the string s in the given
395 base, which defaults to 10. The string s must consist of one
396 or more digits, possibly preceded by a sign. If base is 0, it
397 is chosen from the leading characters of s, 0 for octal, 0x or
398 0X for hexadecimal. If base is 16, a preceding 0x or 0X is
405 # Convert string to long integer
406 def atol(s
, base
=10):
407 """atol(s [,base]) -> long
409 Return the long integer represented by the string s in the
410 given base, which defaults to 10. The string s must consist
411 of one or more digits, possibly preceded by a sign. If base
412 is 0, it is chosen from the leading characters of s, 0 for
413 octal, 0x or 0X for hexadecimal. If base is 16, a preceding
414 0x or 0X is accepted. A trailing L or l is not accepted,
418 return _long(s
, base
)
421 # Left-justify a string
422 def ljust(s
, width
, *args
):
423 """ljust(s, width[, fillchar]) -> string
425 Return a left-justified version of s, in a field of the
426 specified width, padded with spaces as needed. The string is
427 never truncated. If specified the fillchar is used instead of spaces.
430 return s
.ljust(width
, *args
)
432 # Right-justify a string
433 def rjust(s
, width
, *args
):
434 """rjust(s, width[, fillchar]) -> string
436 Return a right-justified version of s, in a field of the
437 specified width, padded with spaces as needed. The string is
438 never truncated. If specified the fillchar is used instead of spaces.
441 return s
.rjust(width
, *args
)
444 def center(s
, width
, *args
):
445 """center(s, width[, fillchar]) -> string
447 Return a center version of s, in a field of the specified
448 width. padded with spaces as needed. The string is never
449 truncated. If specified the fillchar is used instead of spaces.
452 return s
.center(width
, *args
)
454 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
455 # Decadent feature: the argument may be a string or a number
456 # (Use of this is deprecated; it should be a string as with ljust c.s.)
458 """zfill(x, width) -> string
460 Pad a numeric string x with zeros on the left, to fill a field
461 of the specified width. The string x is never truncated.
464 if not isinstance(x
, basestring
):
466 return x
.zfill(width
)
468 # Expand tabs in a string.
469 # Doesn't take non-printing chars into account, but does understand \n.
470 def expandtabs(s
, tabsize
=8):
471 """expandtabs(s [,tabsize]) -> string
473 Return a copy of the string s with all tab characters replaced
474 by the appropriate number of spaces, depending on the current
475 column, and the tabsize (default 8).
478 return s
.expandtabs(tabsize
)
480 # Character translation through look-up table.
481 def translate(s
, table
, deletions
=""):
482 """translate(s,table [,deletions]) -> string
484 Return a copy of the string s, where all characters occurring
485 in the optional argument deletions are removed, and the
486 remaining characters have been mapped through the given
487 translation table, which must be a string of length 256. The
488 deletions argument is not allowed for Unicode strings.
492 return s
.translate(table
, deletions
)
494 # Add s[:0] so that if s is Unicode and table is an 8-bit string,
495 # table is converted to Unicode. This means that table *cannot*
496 # be a dictionary -- for that feature, use u.translate() directly.
497 return s
.translate(table
+ s
[:0])
499 # Capitalize a string, e.g. "aBc dEf" -> "Abc def".
501 """capitalize(s) -> string
503 Return a copy of the string s with only its first character
507 return s
.capitalize()
509 # Substring replacement (global)
510 def replace(s
, old
, new
, maxsplit
=-1):
511 """replace (str, old, new[, maxsplit]) -> string
513 Return a copy of string str with all occurrences of substring
514 old replaced by new. If the optional argument maxsplit is
515 given, only the first maxsplit occurrences are replaced.
518 return s
.replace(old
, new
, maxsplit
)
521 # Try importing optional built-in module "strop" -- if it exists,
522 # it redefines some string operations that are 100-1000 times faster.
523 # It also defines values for whitespace, lowercase and uppercase
524 # that match <ctype.h>'s definitions.
527 from strop
import maketrans
, lowercase
, uppercase
, whitespace
528 letters
= lowercase
+ uppercase
530 pass # Use the original versions