10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""A collection of string operations (most are no longer used). 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoWarning: most of the code you see here isn't normally used nowadays. 40a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBeginning with Python 1.6, many of these functions are implemented as 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaomethods on the standard string object. They used to be implemented by 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoa built-in module called strop, but strop is now obsolete itself. 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 80a8c90248264a8b26970b4473770bcc3df8515fJosh GaoPublic module variables: 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowhitespace -- a string containing all characters considered whitespace 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaolowercase -- a string containing all characters considered lowercase letters 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouppercase -- a string containing all characters considered uppercase letters 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoletters -- a string containing all characters considered letters 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodigits -- a string containing all characters considered decimal digits 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohexdigits -- a string containing all characters considered hexadecimal digits 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaooctdigits -- a string containing all characters considered octal digits 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaopunctuation -- a string containing all characters considered punctuation 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoprintable -- a string containing all characters considered printable 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Some strings for ctype-style character classification 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowhitespace = ' \t\n\r\v\f' 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaolowercase = 'abcdefghijklmnopqrstuvwxyz' 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoletters = lowercase + uppercase 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoascii_lowercase = lowercase 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoascii_uppercase = uppercase 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoascii_letters = ascii_lowercase + ascii_uppercase 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodigits = '0123456789' 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohexdigits = digits + 'abcdef' + 'ABCDEF' 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaooctdigits = '01234567' 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaopunctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoprintable = digits + letters + punctuation + whitespace 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Case conversion helpers 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Use str to convert Unicode literal in case of -U 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gaol = map(chr, xrange(256)) 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_idmap = str('').join(l) 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel l 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Functions which aren't available as string methods. 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef capwords(s, sep=None): 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """capwords(s [,sep]) -> string 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Split the argument into words using split, capitalize each 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao word using capitalize, and join the capitalized words using 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao join. If the optional second argument sep is absent or None, 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao runs of whitespace characters are replaced by a single space 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao and leading and trailing whitespace are removed, otherwise 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sep is used to split and join the words. 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (sep or ' ').join(x.capitalize() for x in s.split(sep)) 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Construct a translation string 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_idmapL = None 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef maketrans(fromstr, tostr): 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """maketrans(frm, to) -> string 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a translation table (a string of 256 bytes long) 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao suitable for use in string.translate. The strings frm and to 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao must be of the same length. 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(fromstr) != len(tostr): 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError, "maketrans arguments must have same length" 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao global _idmapL 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not _idmapL: 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _idmapL = list(_idmap) 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao L = _idmapL[:] 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fromstr = map(ord, fromstr) 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in range(len(fromstr)): 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao L[fromstr[i]] = tostr[i] 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ''.join(L) 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#################################################################### 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport re as _re 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _multimap: 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Helper class for combining multiple mappings. 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Used by .{safe_,}substitute() to combine the mapping and keyword 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao arguments. 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, primary, secondary): 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._primary = primary 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._secondary = secondary 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getitem__(self, key): 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._primary[key] 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError: 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._secondary[key] 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _TemplateMetaclass(type): 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pattern = r""" 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao %(delim)s(?: 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<escaped>%(delim)s) | # Escape sequence of two delimiters 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<named>%(id)s) | # delimiter and a Python identifier 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao {(?P<braced>%(id)s)} | # delimiter and a braced identifier 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<invalid>) # Other ill-formed delimiter exprs 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ) 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(cls, name, bases, dct): 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(_TemplateMetaclass, cls).__init__(name, bases, dct) 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if 'pattern' in dct: 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pattern = cls.pattern 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pattern = _TemplateMetaclass.pattern % { 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'delim' : _re.escape(cls.delimiter), 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'id' : cls.idpattern, 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao } 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Template: 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """A string class for supporting $-substitutions.""" 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __metaclass__ = _TemplateMetaclass 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao delimiter = '$' 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao idpattern = r'[_a-z][_a-z0-9]*' 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, template): 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.template = template 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Search for $$, $identifier, ${identifier}, and any bare $'s 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _invalid(self, mo): 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = mo.start('invalid') 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lines = self.template[:i].splitlines(True) 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not lines: 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao colno = 1 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lineno = 1 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao colno = i - len(''.join(lines[:-1])) 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lineno = len(lines) 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('Invalid placeholder in string: line %d, col %d' % 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (lineno, colno)) 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def substitute(self, *args, **kws): 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(args) > 1: 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError('Too many positional arguments') 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not args: 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapping = kws 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif kws: 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapping = _multimap(kws, args[0]) 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapping = args[0] 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Helper function for .sub() 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def convert(mo): 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Check the most common path first. 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao named = mo.group('named') or mo.group('braced') 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if named is not None: 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao val = mapping[named] 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We use this idiom instead of str() because the latter will 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # fail if val is a Unicode containing non-ASCII characters. 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '%s' % (val,) 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mo.group('escaped') is not None: 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.delimiter 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mo.group('invalid') is not None: 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._invalid(mo) 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('Unrecognized named group in pattern', 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.pattern) 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.pattern.sub(convert, self.template) 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def safe_substitute(self, *args, **kws): 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(args) > 1: 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError('Too many positional arguments') 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not args: 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapping = kws 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif kws: 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapping = _multimap(kws, args[0]) 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mapping = args[0] 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Helper function for .sub() 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def convert(mo): 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao named = mo.group('named') 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if named is not None: 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We use this idiom instead of str() because the latter 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # will fail if val is a Unicode containing non-ASCII 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '%s' % (mapping[named],) 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError: 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.delimiter + named 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao braced = mo.group('braced') 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if braced is not None: 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '%s' % (mapping[braced],) 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError: 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.delimiter + '{' + braced + '}' 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mo.group('escaped') is not None: 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.delimiter 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if mo.group('invalid') is not None: 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.delimiter 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('Unrecognized named group in pattern', 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.pattern) 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.pattern.sub(convert, self.template) 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#################################################################### 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# NOTE: Everything below here is deprecated. Use string methods instead. 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This stuff will go away in Python 3.0. 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Backward compatible names for exceptions 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoindex_error = ValueError 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoatoi_error = ValueError 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoatof_error = ValueError 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoatol_error = ValueError 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# convert UPPER CASE letters to lower case 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef lower(s): 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """lower(s) -> string 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s converted to lowercase. 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.lower() 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert lower case letters to UPPER CASE 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef upper(s): 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """upper(s) -> string 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s converted to uppercase. 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.upper() 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Swap lower case letters and UPPER CASE 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef swapcase(s): 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """swapcase(s) -> string 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s with upper case characters 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao converted to lowercase and vice versa. 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.swapcase() 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Strip leading and trailing tabs and spaces 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef strip(s, chars=None): 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """strip(s [,chars]) -> string 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s with leading and trailing 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao whitespace removed. 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If chars is given and not None, remove characters in chars instead. 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If chars is unicode, S will be converted to unicode before stripping. 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.strip(chars) 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Strip leading tabs and spaces 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef lstrip(s, chars=None): 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """lstrip(s [,chars]) -> string 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s with leading whitespace removed. 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If chars is given and not None, remove characters in chars instead. 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.lstrip(chars) 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Strip trailing tabs and spaces 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rstrip(s, chars=None): 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """rstrip(s [,chars]) -> string 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s with trailing whitespace removed. 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If chars is given and not None, remove characters in chars instead. 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.rstrip(chars) 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Split a string into a list of space/tab-separated words 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef split(s, sep=None, maxsplit=-1): 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """split(s [,sep [,maxsplit]]) -> list of strings 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a list of the words in the string s, using sep as the 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao delimiter string. If maxsplit is given, splits at no more than 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao maxsplit places (resulting in at most maxsplit+1 words). If sep 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is not specified or is None, any whitespace string is a separator. 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (split and splitfields are synonymous) 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.split(sep, maxsplit) 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gaosplitfields = split 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Split a string into a list of space/tab-separated words 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rsplit(s, sep=None, maxsplit=-1): 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """rsplit(s [,sep [,maxsplit]]) -> list of strings 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a list of the words in the string s, using sep as the 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao delimiter string, starting at the end of the string and working 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to the front. If maxsplit is given, at most maxsplit splits are 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao done. If sep is not specified or is None, any whitespace string 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is a separator. 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.rsplit(sep, maxsplit) 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Join fields with optional separator 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef join(words, sep = ' '): 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """join(list [,sep]) -> string 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a string composed of the words in list, with 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intervening occurrences of sep. The default separator is a 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao single space. 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (joinfields and join are synonymous) 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sep.join(words) 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaojoinfields = join 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find substring, raise exception if not found 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef index(s, *args): 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """index(s, sub [,start [,end]]) -> int 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like find but raises ValueError when the substring is not found. 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.index(*args) 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find last substring, raise exception if not found 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rindex(s, *args): 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """rindex(s, sub [,start [,end]]) -> int 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like rfind but raises ValueError when the substring is not found. 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.rindex(*args) 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Count non-overlapping occurrences of substring 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef count(s, *args): 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """count(s, sub[, start[,end]]) -> int 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return the number of occurrences of substring sub in string 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s[start:end]. Optional arguments start and end are 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao interpreted as in slice notation. 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.count(*args) 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find substring, return -1 if not found 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef find(s, *args): 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """find(s, sub [,start [,end]]) -> in 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return the lowest index in s where substring sub is found, 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao such that sub is contained within s[start,end]. Optional 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao arguments start and end are interpreted as in slice notation. 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return -1 on failure. 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.find(*args) 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Find last substring, return -1 if not found 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rfind(s, *args): 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """rfind(s, sub [,start [,end]]) -> int 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return the highest index in s where substring sub is found, 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao such that sub is contained within s[start,end]. Optional 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao arguments start and end are interpreted as in slice notation. 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return -1 on failure. 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.rfind(*args) 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# for a bit of speed 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_float = float 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_int = int 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_long = long 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert string to float 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef atof(s): 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """atof(s) -> float 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return the floating point number represented by the string s. 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _float(s) 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert string to integer 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef atoi(s , base=10): 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """atoi(s [,base]) -> int 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return the integer represented by the string s in the given 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao base, which defaults to 10. The string s must consist of one 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao or more digits, possibly preceded by a sign. If base is 0, it 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is chosen from the leading characters of s, 0 for octal, 0x or 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0X for hexadecimal. If base is 16, a preceding 0x or 0X is 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao accepted. 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _int(s, base) 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Convert string to long integer 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef atol(s, base=10): 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """atol(s [,base]) -> long 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return the long integer represented by the string s in the 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao given base, which defaults to 10. The string s must consist 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of one or more digits, possibly preceded by a sign. If base 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is 0, it is chosen from the leading characters of s, 0 for 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao octal, 0x or 0X for hexadecimal. If base is 16, a preceding 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0x or 0X is accepted. A trailing L or l is not accepted, 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unless base is 0. 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _long(s, base) 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Left-justify a string 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef ljust(s, width, *args): 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ljust(s, width[, fillchar]) -> string 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a left-justified version of s, in a field of the 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao specified width, padded with spaces as needed. The string is 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao never truncated. If specified the fillchar is used instead of spaces. 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.ljust(width, *args) 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Right-justify a string 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rjust(s, width, *args): 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """rjust(s, width[, fillchar]) -> string 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a right-justified version of s, in a field of the 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao specified width, padded with spaces as needed. The string is 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao never truncated. If specified the fillchar is used instead of spaces. 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.rjust(width, *args) 4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Center a string 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef center(s, width, *args): 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """center(s, width[, fillchar]) -> string 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a center version of s, in a field of the specified 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao width. padded with spaces as needed. The string is never 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao truncated. If specified the fillchar is used instead of spaces. 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.center(width, *args) 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Decadent feature: the argument may be a string or a number 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# (Use of this is deprecated; it should be a string as with ljust c.s.) 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef zfill(x, width): 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """zfill(x, width) -> string 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Pad a numeric string x with zeros on the left, to fill a field 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of the specified width. The string x is never truncated. 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not isinstance(x, basestring): 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = repr(x) 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return x.zfill(width) 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Expand tabs in a string. 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Doesn't take non-printing chars into account, but does understand \n. 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef expandtabs(s, tabsize=8): 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """expandtabs(s [,tabsize]) -> string 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s with all tab characters replaced 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao by the appropriate number of spaces, depending on the current 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao column, and the tabsize (default 8). 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.expandtabs(tabsize) 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Character translation through look-up table. 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef translate(s, table, deletions=""): 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """translate(s,table [,deletions]) -> string 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s, where all characters occurring 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in the optional argument deletions are removed, and the 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao remaining characters have been mapped through the given 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao translation table, which must be a string of length 256. The 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao deletions argument is not allowed for Unicode strings. 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if deletions or table is None: 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.translate(table, deletions) 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Add s[:0] so that if s is Unicode and table is an 8-bit string, 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # table is converted to Unicode. This means that table *cannot* 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # be a dictionary -- for that feature, use u.translate() directly. 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.translate(table + s[:0]) 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Capitalize a string, e.g. "aBc dEf" -> "Abc def". 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef capitalize(s): 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """capitalize(s) -> string 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of the string s with only its first character 5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao capitalized. 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.capitalize() 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Substring replacement (global) 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef replace(s, old, new, maxreplace=-1): 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """replace (str, old, new[, maxreplace]) -> string 5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a copy of string str with all occurrences of substring 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao old replaced by new. If the optional argument maxreplace is 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao given, only the first maxreplace occurrences are replaced. 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.replace(old, new, maxreplace) 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Try importing optional built-in module "strop" -- if it exists, 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# it redefines some string operations that are 100-1000 times faster. 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# It also defines values for whitespace, lowercase and uppercase 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# that match <ctype.h>'s definitions. 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from strop import maketrans, lowercase, uppercase, whitespace 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao letters = lowercase + uppercase 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError: 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass # Use the original versions 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao######################################################################## 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# the Formatter class 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# see PEP 3101 for details and purpose of this class 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The hard parts are reused from the C implementation. They're exposed as "_" 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# prefixed methods of str and unicode. 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The overall parser is implemented in str._formatter_parser. 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The field name parser is implemented in str._formatter_field_name_split 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Formatter(object): 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def format(self, format_string, *args, **kwargs): 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.vformat(format_string, args, kwargs) 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def vformat(self, format_string, args, kwargs): 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao used_args = set() 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self._vformat(format_string, args, kwargs, used_args, 2) 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_unused_args(used_args, args, kwargs) 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return result 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _vformat(self, format_string, args, kwargs, used_args, recursion_depth): 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if recursion_depth < 0: 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('Max string recursion exceeded') 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = [] 5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for literal_text, field_name, format_spec, conversion in \ 5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.parse(format_string): 5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # output the literal text 5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if literal_text: 5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.append(literal_text) 5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if there's a field, output it 5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if field_name is not None: 5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # this is some markup, find the object and do 5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the formatting 5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # given the field_name, find the object it references 5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # and the argument it came from 5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj, arg_used = self.get_field(field_name, args, kwargs) 5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao used_args.add(arg_used) 5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # do any conversion on the resulting object 5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = self.convert_field(obj, conversion) 5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expand the format spec, if needed 5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_spec = self._vformat(format_spec, args, kwargs, 5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao used_args, recursion_depth-1) 5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # format the object and append to the result 5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.append(self.format_field(obj, format_spec)) 5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ''.join(result) 5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_value(self, key, args, kwargs): 5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(key, (int, long)): 5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return args[key] 5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return kwargs[key] 5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def check_unused_args(self, used_args, args, kwargs): 5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def format_field(self, value, format_spec): 5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return format(value, format_spec) 6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def convert_field(self, value, conversion): 6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # do any conversion on the resulting object 6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if conversion is None: 6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return value 6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif conversion == 's': 6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return str(value) 6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif conversion == 'r': 6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return repr(value) 6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Unknown conversion specifier {0!s}".format(conversion)) 6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # returns an iterable that contains tuples of the form: 6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (literal_text, field_name, format_spec, conversion) 6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # literal_text can be zero length 6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # field_name can be None, in which case there's no 6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # object to format and output 6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if field_name is not None, it is looked up, formatted 6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # with format_spec and conversion and then used 6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def parse(self, format_string): 6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return format_string._formatter_parser() 6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # given a field_name, find the object it references. 6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # field_name: the field being looked up, e.g. "0.name" 6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # or "lookup[3]" 6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # used_args: a set of which args have been used 6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # args, kwargs: as passed in to vformat 6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_field(self, field_name, args, kwargs): 6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao first, rest = field_name._formatter_field_name_split() 6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = self.get_value(first, args, kwargs) 6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # loop through the rest of the field_name, doing 6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # getattr or getitem as needed 6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for is_attr, i in rest: 6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if is_attr: 6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = getattr(obj, i) 6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = obj[i] 6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj, first 643