0001 # module 'string' -- A collection of string operations 0002 0003 # Warning: most of the code you see here isn't normally used nowadays. With 0004 # Python 1.6, many of these functions are implemented as methods on the 0005 # standard string object. They used to be implemented by a built-in module 0006 # called strop, but strop is now obsolete itself. 0007 0008 """Common string manipulations. 0009 0010 Public module variables: 0011 0012 whitespace -- a string containing all characters considered whitespace 0013 lowercase -- a string containing all characters considered lowercase letters 0014 uppercase -- a string containing all characters considered uppercase letters 0015 letters -- a string containing all characters considered letters 0016 digits -- a string containing all characters considered decimal digits 0017 hexdigits -- a string containing all characters considered hexadecimal digits 0018 octdigits -- a string containing all characters considered octal digits 0019 0020 """ 0021 0022 # Some strings for ctype-style character classification 0023 whitespace = ' \t\n\r\v\f' 0024 lowercase = 'abcdefghijklmnopqrstuvwxyz' 0025 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 0026 letters = lowercase + uppercase 0027 digits = '0123456789' 0028 hexdigits = digits + 'abcdef' + 'ABCDEF' 0029 octdigits = '01234567' 0030 0031 # Case conversion helpers 0032 _idmap = '' 0033 for i in range(256): _idmap = _idmap + chr(i) 0034 del i 0035 0036 # Backward compatible names for exceptions 0037 index_error = ValueError 0038 atoi_error = ValueError 0039 atof_error = ValueError 0040 atol_error = ValueError 0041 0042 # convert UPPER CASE letters to lower case 0043 def lower(s): 0044 """lower(s) -> string 0045 0046 Return a copy of the string s converted to lowercase. 0047 0048 """ 0049 return s.lower() 0050 0051 # Convert lower case letters to UPPER CASE 0052 def upper(s): 0053 """upper(s) -> string 0054 0055 Return a copy of the string s converted to uppercase. 0056 0057 """ 0058 return s.upper() 0059 0060 # Swap lower case letters and UPPER CASE 0061 def swapcase(s): 0062 """swapcase(s) -> string 0063 0064 Return a copy of the string s with upper case characters 0065 converted to lowercase and vice versa. 0066 0067 """ 0068 return s.swapcase() 0069 0070 # Strip leading and trailing tabs and spaces 0071 def strip(s): 0072 """strip(s) -> string 0073 0074 Return a copy of the string s with leading and trailing 0075 whitespace removed. 0076 0077 """ 0078 return s.strip() 0079 0080 # Strip leading tabs and spaces 0081 def lstrip(s): 0082 """lstrip(s) -> string 0083 0084 Return a copy of the string s with leading whitespace removed. 0085 0086 """ 0087 return s.lstrip() 0088 0089 # Strip trailing tabs and spaces 0090 def rstrip(s): 0091 """rstrip(s) -> string 0092 0093 Return a copy of the string s with trailing whitespace 0094 removed. 0095 0096 """ 0097 return s.rstrip() 0098 0099 0100 # Split a string into a list of space/tab-separated words 0101 def split(s, sep=None, maxsplit=0): 0102 """split(str [,sep [,maxsplit]]) -> list of strings 0103 0104 Return a list of the words in the string s, using sep as the 0105 delimiter string. If maxsplit is nonzero, splits into at most 0106 maxsplit words If sep is not specified, any whitespace string 0107 is a separator. Maxsplit defaults to 0. 0108 0109 (split and splitfields are synonymous) 0110 0111 """ 0112 return s.split(sep, maxsplit) 0113 splitfields = split 0114 0115 # Join fields with optional separator 0116 def join(words, sep = ' '): 0117 """join(list [,sep]) -> string 0118 0119 Return a string composed of the words in list, with 0120 intervening occurrences of sep. The default separator is a 0121 single space. 0122 0123 (joinfields and join are synonymous) 0124 0125 """ 0126 return sep.join(words) 0127 joinfields = join 0128 0129 # for a little bit of speed 0130 _apply = apply 0131 0132 # Find substring, raise exception if not found 0133 def index(s, *args): 0134 """index(s, sub [,start [,end]]) -> int 0135 0136 Like find but raises ValueError when the substring is not found. 0137 0138 """ 0139 return _apply(s.index, args) 0140 0141 # Find last substring, raise exception if not found 0142 def rindex(s, *args): 0143 """rindex(s, sub [,start [,end]]) -> int 0144 0145 Like rfind but raises ValueError when the substring is not found. 0146 0147 """ 0148 return _apply(s.rindex, args) 0149 0150 # Count non-overlapping occurrences of substring 0151 def count(s, *args): 0152 """count(s, sub[, start[,end]]) -> int 0153 0154 Return the number of occurrences of substring sub in string 0155 s[start:end]. Optional arguments start and end are 0156 interpreted as in slice notation. 0157 0158 """ 0159 return _apply(s.count, args) 0160 0161 # Find substring, return -1 if not found 0162 def find(s, *args): 0163 """find(s, sub [,start [,end]]) -> in 0164 0165 Return the lowest index in s where substring sub is found, 0166 such that sub is contained within s[start,end]. Optional 0167 arguments start and end are interpreted as in slice notation. 0168 0169 Return -1 on failure. 0170 0171 """ 0172 return _apply(s.find, args) 0173 0174 # Find last substring, return -1 if not found 0175 def rfind(s, *args): 0176 """rfind(s, sub [,start [,end]]) -> int 0177 0178 Return the highest index in s where substring sub is found, 0179 such that sub is contained within s[start,end]. Optional 0180 arguments start and end are interpreted as in slice notation. 0181 0182 Return -1 on failure. 0183 0184 """ 0185 return _apply(s.rfind, args) 0186 0187 # for a bit of speed 0188 _float = float 0189 _int = int 0190 _long = long 0191 _StringType = type('') 0192 0193 # Convert string to float 0194 def atof(s): 0195 """atof(s) -> float 0196 0197 Return the floating point number represented by the string s. 0198 0199 """ 0200 if type(s) == _StringType: 0201 return _float(s) 0202 else: 0203 raise TypeError('argument 1: expected string, %s found' % 0204 type(s).__name__) 0205 0206 # Convert string to integer 0207 def atoi(*args): 0208 """atoi(s [,base]) -> int 0209 0210 Return the integer represented by the string s in the given 0211 base, which defaults to 10. The string s must consist of one 0212 or more digits, possibly preceded by a sign. If base is 0, it 0213 is chosen from the leading characters of s, 0 for octal, 0x or 0214 0X for hexadecimal. If base is 16, a preceding 0x or 0X is 0215 accepted. 0216 0217 """ 0218 try: 0219 s = args[0] 0220 except IndexError: 0221 raise TypeError('function requires at least 1 argument: %d given' % 0222 len(args)) 0223 # Don't catch type error resulting from too many arguments to int(). The 0224 # error message isn't compatible but the error type is, and this function 0225 # is complicated enough already. 0226 if type(s) == _StringType: 0227 return _apply(_int, args) 0228 else: 0229 raise TypeError('argument 1: expected string, %s found' % 0230 type(s).__name__) 0231 0232 0233 # Convert string to long integer 0234 def atol(*args): 0235 """atol(s [,base]) -> long 0236 0237 Return the long integer represented by the string s in the 0238 given base, which defaults to 10. The string s must consist 0239 of one or more digits, possibly preceded by a sign. If base 0240 is 0, it is chosen from the leading characters of s, 0 for 0241 octal, 0x or 0X for hexadecimal. If base is 16, a preceding 0242 0x or 0X is accepted. A trailing L or l is not accepted, 0243 unless base is 0. 0244 0245 """ 0246 try: 0247 s = args[0] 0248 except IndexError: 0249 raise TypeError('function requires at least 1 argument: %d given' % 0250 len(args)) 0251 # Don't catch type error resulting from too many arguments to long(). The 0252 # error message isn't compatible but the error type is, and this function 0253 # is complicated enough already. 0254 if type(s) == _StringType: 0255 return _apply(_long, args) 0256 else: 0257 raise TypeError('argument 1: expected string, %s found' % 0258 type(s).__name__) 0259 0260 0261 # Left-justify a string 0262 def ljust(s, width): 0263 """ljust(s, width) -> string 0264 0265 Return a left-justified version of s, in a field of the 0266 specified width, padded with spaces as needed. The string is 0267 never truncated. 0268 0269 """ 0270 n = width - len(s) 0271 if n <= 0: return s 0272 return s + ' '*n 0273 0274 # Right-justify a string 0275 def rjust(s, width): 0276 """rjust(s, width) -> string 0277 0278 Return a right-justified version of s, in a field of the 0279 specified width, padded with spaces as needed. The string is 0280 never truncated. 0281 0282 """ 0283 n = width - len(s) 0284 if n <= 0: return s 0285 return ' '*n + s 0286 0287 # Center a string 0288 def center(s, width): 0289 """center(s, width) -> string 0290 0291 Return a center version of s, in a field of the specified 0292 width. padded with spaces as needed. The string is never 0293 truncated. 0294 0295 """ 0296 n = width - len(s) 0297 if n <= 0: return s 0298 half = n/2 0299 if n%2 and width%2: 0300 # This ensures that center(center(s, i), j) = center(s, j) 0301 half = half+1 0302 return ' '*half + s + ' '*(n-half) 0303 0304 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' 0305 # Decadent feature: the argument may be a string or a number 0306 # (Use of this is deprecated; it should be a string as with ljust c.s.) 0307 def zfill(x, width): 0308 """zfill(x, width) -> string 0309 0310 Pad a numeric string x with zeros on the left, to fill a field 0311 of the specified width. The string x is never truncated. 0312 0313 """ 0314 if type(x) == type(''): s = x 0315 else: s = repr(x) 0316 n = len(s) 0317 if n >= width: return s 0318 sign = '' 0319 if s[0] in ('-', '+'): 0320 sign, s = s[0], s[1:] 0321 return sign + '0'*(width-n) + s 0322 0323 # Expand tabs in a string. 0324 # Doesn't take non-printing chars into account, but does understand \n. 0325 def expandtabs(s, tabsize=8): 0326 """expandtabs(s [,tabsize]) -> string 0327 0328 Return a copy of the string s with all tab characters replaced 0329 by the appropriate number of spaces, depending on the current 0330 column, and the tabsize (default 8). 0331 0332 """ 0333 res = line = '' 0334 for c in s: 0335 if c == '\t': 0336 c = ' '*(tabsize - len(line) % tabsize) 0337 line = line + c 0338 if c == '\n': 0339 res = res + line 0340 line = '' 0341 return res + line 0342 0343 # Character translation through look-up table. 0344 def translate(s, table, deletions=""): 0345 """translate(s,table [,deletechars]) -> string 0346 0347 Return a copy of the string s, where all characters occurring 0348 in the optional argument deletechars are removed, and the 0349 remaining characters have been mapped through the given 0350 translation table, which must be a string of length 256. 0351 0352 """ 0353 return s.translate(table, deletions) 0354 0355 # Capitalize a string, e.g. "aBc dEf" -> "Abc def". 0356 def capitalize(s): 0357 """capitalize(s) -> string 0358 0359 Return a copy of the string s with only its first character 0360 capitalized. 0361 0362 """ 0363 return s.capitalize() 0364 0365 # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". 0366 # See also regsub.capwords(). 0367 def capwords(s, sep=None): 0368 """capwords(s, [sep]) -> string 0369 0370 Split the argument into words using split, capitalize each 0371 word using capitalize, and join the capitalized words using 0372 join. Note that this replaces runs of whitespace characters by 0373 a single space. 0374 0375 """ 0376 return join(map(capitalize, s.split(sep)), sep or ' ') 0377 0378 # Construct a translation string 0379 _idmapL = None 0380 def maketrans(fromstr, tostr): 0381 """maketrans(frm, to) -> string 0382 0383 Return a translation table (a string of 256 bytes long) 0384 suitable for use in string.translate. The strings frm and to 0385 must be of the same length. 0386 0387 """ 0388 if len(fromstr) != len(tostr): 0389 raise ValueError, "maketrans arguments must have same length" 0390 global _idmapL 0391 if not _idmapL: 0392 _idmapL = map(None, _idmap) 0393 L = _idmapL[:] 0394 fromstr = map(ord, fromstr) 0395 for i in range(len(fromstr)): 0396 L[fromstr[i]] = tostr[i] 0397 return join(L, "") 0398 0399 # Substring replacement (global) 0400 def replace(s, old, new, maxsplit=0): 0401 """replace (str, old, new[, maxsplit]) -> string 0402 0403 Return a copy of string str with all occurrences of substring 0404 old replaced by new. If the optional argument maxsplit is 0405 given, only the first maxsplit occurrences are replaced. 0406 0407 """ 0408 return s.replace(old, new, maxsplit) 0409 0410 0411 # XXX: transitional 0412 # 0413 # If string objects do not have methods, then we need to use the old string.py 0414 # library, which uses strop for many more things than just the few outlined 0415 # below. 0416 try: 0417 ''.upper 0418 except AttributeError: 0419 from stringold import * 0420 0421 # Try importing optional built-in module "strop" -- if it exists, 0422 # it redefines some string operations that are 100-1000 times faster. 0423 # It also defines values for whitespace, lowercase and uppercase 0424 # that match <ctype.h>'s definitions. 0425 0426 try: 0427 from strop import maketrans, lowercase, uppercase, whitespace 0428 letters = lowercase + uppercase 0429 except ImportError: 0430 pass # Use the original versions 0431
Generated by PyXR 0.9.4