1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
---|
2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
---|
3 | """A collection of string operations (most are no longer used). |
---|
4 | |
---|
5 | Warning: most of the code you see here isn't normally used nowadays. |
---|
6 | Beginning with Python 1.6, many of these functions are implemented as |
---|
7 | methods on the standard string object. They used to be implemented by |
---|
8 | a built-in module called strop, but strop is now obsolete itself. |
---|
9 | |
---|
10 | Public module variables: |
---|
11 | |
---|
12 | whitespace -- a string containing all characters considered whitespace |
---|
13 | lowercase -- a string containing all characters considered lowercase letters |
---|
14 | uppercase -- a string containing all characters considered uppercase letters |
---|
15 | letters -- a string containing all characters considered letters |
---|
16 | digits -- a string containing all characters considered decimal digits |
---|
17 | hexdigits -- a string containing all characters considered hexadecimal digits |
---|
18 | octdigits -- a string containing all characters considered octal digits |
---|
19 | punctuation -- a string containing all characters considered punctuation |
---|
20 | printable -- a string containing all characters considered printable |
---|
21 | |
---|
22 | """ |
---|
23 | |
---|
24 | # Some strings for ctype-style character classification |
---|
25 | whitespace = ' \t\n\r\v\f' |
---|
26 | lowercase = 'abcdefghijklmnopqrstuvwxyz' |
---|
27 | uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
---|
28 | letters = lowercase + uppercase |
---|
29 | ascii_lowercase = lowercase |
---|
30 | ascii_uppercase = uppercase |
---|
31 | ascii_letters = ascii_lowercase + ascii_uppercase |
---|
32 | digits = '0123456789' |
---|
33 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
---|
34 | octdigits = '01234567' |
---|
35 | punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
---|
36 | printable = digits + letters + punctuation + whitespace |
---|
37 | |
---|
38 | # Case conversion helpers |
---|
39 | # Use str to convert Unicode literal in case of -U |
---|
40 | # Note that Cookie.py bogusly uses _idmap :( |
---|
41 | l = map(chr, xrange(256)) |
---|
42 | _idmap = str('').join(l) |
---|
43 | del l |
---|
44 | |
---|
45 | # Functions which aren't available as string methods. |
---|
46 | |
---|
47 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
---|
48 | # See also regsub.capwords(). |
---|
49 | def capwords(s, sep=None): |
---|
50 | """capwords(s, [sep]) -> string |
---|
51 | |
---|
52 | Split the argument into words using split, capitalize each |
---|
53 | word using capitalize, and join the capitalized words using |
---|
54 | join. Note that this replaces runs of whitespace characters by |
---|
55 | a single space. |
---|
56 | |
---|
57 | """ |
---|
58 | return (sep or ' ').join([x.capitalize() for x in s.split(sep)]) |
---|
59 | |
---|
60 | |
---|
61 | # Construct a translation string |
---|
62 | _idmapL = None |
---|
63 | def maketrans(fromstr, tostr): |
---|
64 | """maketrans(frm, to) -> string |
---|
65 | |
---|
66 | Return a translation table (a string of 256 bytes long) |
---|
67 | suitable for use in string.translate. The strings frm and to |
---|
68 | must be of the same length. |
---|
69 | |
---|
70 | """ |
---|
71 | if len(fromstr) != len(tostr): |
---|
72 | raise ValueError, "maketrans arguments must have same length" |
---|
73 | global _idmapL |
---|
74 | if not _idmapL: |
---|
75 | _idmapL = map(None, _idmap) |
---|
76 | L = _idmapL[:] |
---|
77 | fromstr = map(ord, fromstr) |
---|
78 | for i in range(len(fromstr)): |
---|
79 | L[fromstr[i]] = tostr[i] |
---|
80 | return ''.join(L) |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | #################################################################### |
---|
85 | import re as _re |
---|
86 | |
---|
87 | class _multimap: |
---|
88 | """Helper class for combining multiple mappings. |
---|
89 | |
---|
90 | Used by .{safe_,}substitute() to combine the mapping and keyword |
---|
91 | arguments. |
---|
92 | """ |
---|
93 | def __init__(self, primary, secondary): |
---|
94 | self._primary = primary |
---|
95 | self._secondary = secondary |
---|
96 | |
---|
97 | def __getitem__(self, key): |
---|
98 | try: |
---|
99 | return self._primary[key] |
---|
100 | except KeyError: |
---|
101 | return self._secondary[key] |
---|
102 | |
---|
103 | |
---|
104 | class _TemplateMetaclass(type): |
---|
105 | pattern = r""" |
---|
106 | %(delim)s(?: |
---|
107 | (?P<escaped>%(delim)s) | # Escape sequence of two delimiters |
---|
108 | (?P<named>%(id)s) | # delimiter and a Python identifier |
---|
109 | {(?P<braced>%(id)s)} | # delimiter and a braced identifier |
---|
110 | (?P<invalid>) # Other ill-formed delimiter exprs |
---|
111 | ) |
---|
112 | """ |
---|
113 | |
---|
114 | def __init__(cls, name, bases, dct): |
---|
115 | super(_TemplateMetaclass, cls).__init__(name, bases, dct) |
---|
116 | if 'pattern' in dct: |
---|
117 | pattern = cls.pattern |
---|
118 | else: |
---|
119 | pattern = _TemplateMetaclass.pattern % { |
---|
120 | 'delim' : _re.escape(cls.delimiter), |
---|
121 | 'id' : cls.idpattern, |
---|
122 | } |
---|
123 | cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) |
---|
124 | |
---|
125 | |
---|
126 | class Template: |
---|
127 | """A string class for supporting $-substitutions.""" |
---|
128 | __metaclass__ = _TemplateMetaclass |
---|
129 | |
---|
130 | delimiter = '$' |
---|
131 | idpattern = r'[_a-z][_a-z0-9]*' |
---|
132 | |
---|
133 | def __init__(self, template): |
---|
134 | self.template = template |
---|
135 | |
---|
136 | # Search for $$, $identifier, ${identifier}, and any bare $'s |
---|
137 | |
---|
138 | def _invalid(self, mo): |
---|
139 | i = mo.start('invalid') |
---|
140 | lines = self.template[:i].splitlines(True) |
---|
141 | if not lines: |
---|
142 | colno = 1 |
---|
143 | lineno = 1 |
---|
144 | else: |
---|
145 | colno = i - len(''.join(lines[:-1])) |
---|
146 | lineno = len(lines) |
---|
147 | raise ValueError('Invalid placeholder in string: line %d, col %d' % |
---|
148 | (lineno, colno)) |
---|
149 | |
---|
150 | def substitute(self, *args, **kws): |
---|
151 | if len(args) > 1: |
---|
152 | raise TypeError('Too many positional arguments') |
---|
153 | if not args: |
---|
154 | mapping = kws |
---|
155 | elif kws: |
---|
156 | mapping = _multimap(kws, args[0]) |
---|
157 | else: |
---|
158 | mapping = args[0] |
---|
159 | # Helper function for .sub() |
---|
160 | def convert(mo): |
---|
161 | # Check the most common path first. |
---|
162 | named = mo.group('named') or mo.group('braced') |
---|
163 | if named is not None: |
---|
164 | val = mapping[named] |
---|
165 | # We use this idiom instead of str() because the latter will |
---|
166 | # fail if val is a Unicode containing non-ASCII characters. |
---|
167 | return '%s' % val |
---|
168 | if mo.group('escaped') is not None: |
---|
169 | return self.delimiter |
---|
170 | if mo.group('invalid') is not None: |
---|
171 | self._invalid(mo) |
---|
172 | raise ValueError('Unrecognized named group in pattern', |
---|
173 | self.pattern) |
---|
174 | return self.pattern.sub(convert, self.template) |
---|
175 | |
---|
176 | def safe_substitute(self, *args, **kws): |
---|
177 | if len(args) > 1: |
---|
178 | raise TypeError('Too many positional arguments') |
---|
179 | if not args: |
---|
180 | mapping = kws |
---|
181 | elif kws: |
---|
182 | mapping = _multimap(kws, args[0]) |
---|
183 | else: |
---|
184 | mapping = args[0] |
---|
185 | # Helper function for .sub() |
---|
186 | def convert(mo): |
---|
187 | named = mo.group('named') |
---|
188 | if named is not None: |
---|
189 | try: |
---|
190 | # We use this idiom instead of str() because the latter |
---|
191 | # will fail if val is a Unicode containing non-ASCII |
---|
192 | return '%s' % mapping[named] |
---|
193 | except KeyError: |
---|
194 | return self.delimiter + named |
---|
195 | braced = mo.group('braced') |
---|
196 | if braced is not None: |
---|
197 | try: |
---|
198 | return '%s' % mapping[braced] |
---|
199 | except KeyError: |
---|
200 | return self.delimiter + '{' + braced + '}' |
---|
201 | if mo.group('escaped') is not None: |
---|
202 | return self.delimiter |
---|
203 | if mo.group('invalid') is not None: |
---|
204 | return self.delimiter |
---|
205 | raise ValueError('Unrecognized named group in pattern', |
---|
206 | self.pattern) |
---|
207 | return self.pattern.sub(convert, self.template) |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | #################################################################### |
---|
212 | # NOTE: Everything below here is deprecated. Use string methods instead. |
---|
213 | # This stuff will go away in Python 3.0. |
---|
214 | |
---|
215 | # Backward compatible names for exceptions |
---|
216 | index_error = ValueError |
---|
217 | atoi_error = ValueError |
---|
218 | atof_error = ValueError |
---|
219 | atol_error = ValueError |
---|
220 | |
---|
221 | # convert UPPER CASE letters to lower case |
---|
222 | def lower(s): |
---|
223 | """lower(s) -> string |
---|
224 | |
---|
225 | Return a copy of the string s converted to lowercase. |
---|
226 | |
---|
227 | """ |
---|
228 | return s.lower() |
---|
229 | |
---|
230 | # Convert lower case letters to UPPER CASE |
---|
231 | def upper(s): |
---|
232 | """upper(s) -> string |
---|
233 | |
---|
234 | Return a copy of the string s converted to uppercase. |
---|
235 | |
---|
236 | """ |
---|
237 | return s.upper() |
---|
238 | |
---|
239 | # Swap lower case letters and UPPER CASE |
---|
240 | def swapcase(s): |
---|
241 | """swapcase(s) -> string |
---|
242 | |
---|
243 | Return a copy of the string s with upper case characters |
---|
244 | converted to lowercase and vice versa. |
---|
245 | |
---|
246 | """ |
---|
247 | return s.swapcase() |
---|
248 | |
---|
249 | # Strip leading and trailing tabs and spaces |
---|
250 | def strip(s, chars=None): |
---|
251 | """strip(s [,chars]) -> string |
---|
252 | |
---|
253 | Return a copy of the string s with leading and trailing |
---|
254 | whitespace removed. |
---|
255 | If chars is given and not None, remove characters in chars instead. |
---|
256 | If chars is unicode, S will be converted to unicode before stripping. |
---|
257 | |
---|
258 | """ |
---|
259 | return s.strip(chars) |
---|
260 | |
---|
261 | # Strip leading tabs and spaces |
---|
262 | def lstrip(s, chars=None): |
---|
263 | """lstrip(s [,chars]) -> string |
---|
264 | |
---|
265 | Return a copy of the string s with leading whitespace removed. |
---|
266 | If chars is given and not None, remove characters in chars instead. |
---|
267 | |
---|
268 | """ |
---|
269 | return s.lstrip(chars) |
---|
270 | |
---|
271 | # Strip trailing tabs and spaces |
---|
272 | def rstrip(s, chars=None): |
---|
273 | """rstrip(s [,chars]) -> string |
---|
274 | |
---|
275 | Return a copy of the string s with trailing whitespace removed. |
---|
276 | If chars is given and not None, remove characters in chars instead. |
---|
277 | |
---|
278 | """ |
---|
279 | return s.rstrip(chars) |
---|
280 | |
---|
281 | |
---|
282 | # Split a string into a list of space/tab-separated words |
---|
283 | def split(s, sep=None, maxsplit=-1): |
---|
284 | """split(s [,sep [,maxsplit]]) -> list of strings |
---|
285 | |
---|
286 | Return a list of the words in the string s, using sep as the |
---|
287 | delimiter string. If maxsplit is given, splits at no more than |
---|
288 | maxsplit places (resulting in at most maxsplit+1 words). If sep |
---|
289 | is not specified or is None, any whitespace string is a separator. |
---|
290 | |
---|
291 | (split and splitfields are synonymous) |
---|
292 | |
---|
293 | """ |
---|
294 | return s.split(sep, maxsplit) |
---|
295 | splitfields = split |
---|
296 | |
---|
297 | # Split a string into a list of space/tab-separated words |
---|
298 | def rsplit(s, sep=None, maxsplit=-1): |
---|
299 | """rsplit(s [,sep [,maxsplit]]) -> list of strings |
---|
300 | |
---|
301 | Return a list of the words in the string s, using sep as the |
---|
302 | delimiter string, starting at the end of the string and working |
---|
303 | to the front. If maxsplit is given, at most maxsplit splits are |
---|
304 | done. If sep is not specified or is None, any whitespace string |
---|
305 | is a separator. |
---|
306 | """ |
---|
307 | return s.rsplit(sep, maxsplit) |
---|
308 | |
---|
309 | # Join fields with optional separator |
---|
310 | def join(words, sep = ' '): |
---|
311 | """join(list [,sep]) -> string |
---|
312 | |
---|
313 | Return a string composed of the words in list, with |
---|
314 | intervening occurrences of sep. The default separator is a |
---|
315 | single space. |
---|
316 | |
---|
317 | (joinfields and join are synonymous) |
---|
318 | |
---|
319 | """ |
---|
320 | return sep.join(words) |
---|
321 | joinfields = join |
---|
322 | |
---|
323 | # Find substring, raise exception if not found |
---|
324 | def index(s, *args): |
---|
325 | """index(s, sub [,start [,end]]) -> int |
---|
326 | |
---|
327 | Like find but raises ValueError when the substring is not found. |
---|
328 | |
---|
329 | """ |
---|
330 | return s.index(*args) |
---|
331 | |
---|
332 | # Find last substring, raise exception if not found |
---|
333 | def rindex(s, *args): |
---|
334 | """rindex(s, sub [,start [,end]]) -> int |
---|
335 | |
---|
336 | Like rfind but raises ValueError when the substring is not found. |
---|
337 | |
---|
338 | """ |
---|
339 | return s.rindex(*args) |
---|
340 | |
---|
341 | # Count non-overlapping occurrences of substring |
---|
342 | def count(s, *args): |
---|
343 | """count(s, sub[, start[,end]]) -> int |
---|
344 | |
---|
345 | Return the number of occurrences of substring sub in string |
---|
346 | s[start:end]. Optional arguments start and end are |
---|
347 | interpreted as in slice notation. |
---|
348 | |
---|
349 | """ |
---|
350 | return s.count(*args) |
---|
351 | |
---|
352 | # Find substring, return -1 if not found |
---|
353 | def find(s, *args): |
---|
354 | """find(s, sub [,start [,end]]) -> in |
---|
355 | |
---|
356 | Return the lowest index in s where substring sub is found, |
---|
357 | such that sub is contained within s[start,end]. Optional |
---|
358 | arguments start and end are interpreted as in slice notation. |
---|
359 | |
---|
360 | Return -1 on failure. |
---|
361 | |
---|
362 | """ |
---|
363 | return s.find(*args) |
---|
364 | |
---|
365 | # Find last substring, return -1 if not found |
---|
366 | def rfind(s, *args): |
---|
367 | """rfind(s, sub [,start [,end]]) -> int |
---|
368 | |
---|
369 | Return the highest index in s where substring sub is found, |
---|
370 | such that sub is contained within s[start,end]. Optional |
---|
371 | arguments start and end are interpreted as in slice notation. |
---|
372 | |
---|
373 | Return -1 on failure. |
---|
374 | |
---|
375 | """ |
---|
376 | return s.rfind(*args) |
---|
377 | |
---|
378 | # for a bit of speed |
---|
379 | _float = float |
---|
380 | _int = int |
---|
381 | _long = long |
---|
382 | |
---|
383 | # Convert string to float |
---|
384 | def atof(s): |
---|
385 | """atof(s) -> float |
---|
386 | |
---|
387 | Return the floating point number represented by the string s. |
---|
388 | |
---|
389 | """ |
---|
390 | return _float(s) |
---|
391 | |
---|
392 | |
---|
393 | # Convert string to integer |
---|
394 | def atoi(s , base=10): |
---|
395 | """atoi(s [,base]) -> int |
---|
396 | |
---|
397 | Return the integer represented by the string s in the given |
---|
398 | base, which defaults to 10. The string s must consist of one |
---|
399 | or more digits, possibly preceded by a sign. If base is 0, it |
---|
400 | is chosen from the leading characters of s, 0 for octal, 0x or |
---|
401 | 0X for hexadecimal. If base is 16, a preceding 0x or 0X is |
---|
402 | accepted. |
---|
403 | |
---|
404 | """ |
---|
405 | return _int(s, base) |
---|
406 | |
---|
407 | |
---|
408 | # Convert string to long integer |
---|
409 | def atol(s, base=10): |
---|
410 | """atol(s [,base]) -> long |
---|
411 | |
---|
412 | Return the long integer represented by the string s in the |
---|
413 | given base, which defaults to 10. The string s must consist |
---|
414 | of one or more digits, possibly preceded by a sign. If base |
---|
415 | is 0, it is chosen from the leading characters of s, 0 for |
---|
416 | octal, 0x or 0X for hexadecimal. If base is 16, a preceding |
---|
417 | 0x or 0X is accepted. A trailing L or l is not accepted, |
---|
418 | unless base is 0. |
---|
419 | |
---|
420 | """ |
---|
421 | return _long(s, base) |
---|
422 | |
---|
423 | |
---|
424 | # Left-justify a string |
---|
425 | def ljust(s, width, *args): |
---|
426 | """ljust(s, width[, fillchar]) -> string |
---|
427 | |
---|
428 | Return a left-justified version of s, in a field of the |
---|
429 | specified width, padded with spaces as needed. The string is |
---|
430 | never truncated. If specified the fillchar is used instead of spaces. |
---|
431 | |
---|
432 | """ |
---|
433 | return s.ljust(width, *args) |
---|
434 | |
---|
435 | # Right-justify a string |
---|
436 | def rjust(s, width, *args): |
---|
437 | """rjust(s, width[, fillchar]) -> string |
---|
438 | |
---|
439 | Return a right-justified version of s, in a field of the |
---|
440 | specified width, padded with spaces as needed. The string is |
---|
441 | never truncated. If specified the fillchar is used instead of spaces. |
---|
442 | |
---|
443 | """ |
---|
444 | return s.rjust(width, *args) |
---|
445 | |
---|
446 | # Center a string |
---|
447 | def center(s, width, *args): |
---|
448 | """center(s, width[, fillchar]) -> string |
---|
449 | |
---|
450 | Return a center version of s, in a field of the specified |
---|
451 | width. padded with spaces as needed. The string is never |
---|
452 | truncated. If specified the fillchar is used instead of spaces. |
---|
453 | |
---|
454 | """ |
---|
455 | return s.center(width, *args) |
---|
456 | |
---|
457 | # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' |
---|
458 | # Decadent feature: the argument may be a string or a number |
---|
459 | # (Use of this is deprecated; it should be a string as with ljust c.s.) |
---|
460 | def zfill(x, width): |
---|
461 | """zfill(x, width) -> string |
---|
462 | |
---|
463 | Pad a numeric string x with zeros on the left, to fill a field |
---|
464 | of the specified width. The string x is never truncated. |
---|
465 | |
---|
466 | """ |
---|
467 | if not isinstance(x, basestring): |
---|
468 | x = repr(x) |
---|
469 | return x.zfill(width) |
---|
470 | |
---|
471 | # Expand tabs in a string. |
---|
472 | # Doesn't take non-printing chars into account, but does understand \n. |
---|
473 | def expandtabs(s, tabsize=8): |
---|
474 | """expandtabs(s [,tabsize]) -> string |
---|
475 | |
---|
476 | Return a copy of the string s with all tab characters replaced |
---|
477 | by the appropriate number of spaces, depending on the current |
---|
478 | column, and the tabsize (default 8). |
---|
479 | |
---|
480 | """ |
---|
481 | return s.expandtabs(tabsize) |
---|
482 | |
---|
483 | # Character translation through look-up table. |
---|
484 | def translate(s, table, deletions=""): |
---|
485 | """translate(s,table [,deletions]) -> string |
---|
486 | |
---|
487 | Return a copy of the string s, where all characters occurring |
---|
488 | in the optional argument deletions are removed, and the |
---|
489 | remaining characters have been mapped through the given |
---|
490 | translation table, which must be a string of length 256. The |
---|
491 | deletions argument is not allowed for Unicode strings. |
---|
492 | |
---|
493 | """ |
---|
494 | if deletions: |
---|
495 | return s.translate(table, deletions) |
---|
496 | else: |
---|
497 | # Add s[:0] so that if s is Unicode and table is an 8-bit string, |
---|
498 | # table is converted to Unicode. This means that table *cannot* |
---|
499 | # be a dictionary -- for that feature, use u.translate() directly. |
---|
500 | return s.translate(table + s[:0]) |
---|
501 | |
---|
502 | # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
---|
503 | def capitalize(s): |
---|
504 | """capitalize(s) -> string |
---|
505 | |
---|
506 | Return a copy of the string s with only its first character |
---|
507 | capitalized. |
---|
508 | |
---|
509 | """ |
---|
510 | return s.capitalize() |
---|
511 | |
---|
512 | # Substring replacement (global) |
---|
513 | def replace(s, old, new, maxsplit=-1): |
---|
514 | """replace (str, old, new[, maxsplit]) -> string |
---|
515 | |
---|
516 | Return a copy of string str with all occurrences of substring |
---|
517 | old replaced by new. If the optional argument maxsplit is |
---|
518 | given, only the first maxsplit occurrences are replaced. |
---|
519 | |
---|
520 | """ |
---|
521 | return s.replace(old, new, maxsplit) |
---|
522 | |
---|
523 | |
---|
524 | # Try importing optional built-in module "strop" -- if it exists, |
---|
525 | # it redefines some string operations that are 100-1000 times faster. |
---|
526 | # It also defines values for whitespace, lowercase and uppercase |
---|
527 | # that match <ctype.h>'s definitions. |
---|
528 | |
---|
529 | try: |
---|
530 | from strop import maketrans, lowercase, uppercase, whitespace |
---|
531 | letters = lowercase + uppercase |
---|
532 | except ImportError: |
---|
533 | pass # Use the original versions |
---|