13257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel"""A collection of string operations (most are no longer used). 23257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 33257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielWarning: most of the code you see here isn't normally used nowadays. 43257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielBeginning with Python 1.6, many of these functions are implemented as 53257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielmethods on the standard string object. They used to be implemented by 63257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniela built-in module called strop, but strop is now obsolete itself. 73257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 83257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielPublic module variables: 93257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielwhitespace -- a string containing all characters considered whitespace 113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniellowercase -- a string containing all characters considered lowercase letters 123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieluppercase -- a string containing all characters considered uppercase letters 133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielletters -- a string containing all characters considered letters 143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldigits -- a string containing all characters considered decimal digits 153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielhexdigits -- a string containing all characters considered hexadecimal digits 163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieloctdigits -- a string containing all characters considered octal digits 173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielpunctuation -- a string containing all characters considered punctuation 183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielprintable -- a string containing all characters considered printable 193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel""" 213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Some strings for ctype-style character classification 233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielwhitespace = ' \t\n\r\v\f' 243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniellowercase = 'abcdefghijklmnopqrstuvwxyz' 253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieluppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielletters = lowercase + uppercase 273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielascii_lowercase = lowercase 283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielascii_uppercase = uppercase 293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielascii_letters = ascii_lowercase + ascii_uppercase 303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldigits = '0123456789' 313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielhexdigits = digits + 'abcdef' + 'ABCDEF' 323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieloctdigits = '01234567' 333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielpunctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" 343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielprintable = digits + letters + punctuation + whitespace 353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Case conversion helpers 373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Use str to convert Unicode literal in case of -U 383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniell = map(chr, xrange(256)) 393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel_idmap = str('').join(l) 403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldel l 413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Functions which aren't available as string methods. 433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". 453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef capwords(s, sep=None): 463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """capwords(s [,sep]) -> string 473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Split the argument into words using split, capitalize each 493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel word using capitalize, and join the capitalized words using 503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel join. If the optional second argument sep is absent or None, 513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel runs of whitespace characters are replaced by a single space 523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel and leading and trailing whitespace are removed, otherwise 533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel sep is used to split and join the words. 543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return (sep or ' ').join(x.capitalize() for x in s.split(sep)) 573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Construct a translation string 603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel_idmapL = None 613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef maketrans(fromstr, tostr): 623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """maketrans(frm, to) -> string 633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a translation table (a string of 256 bytes long) 653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel suitable for use in string.translate. The strings frm and to 663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel must be of the same length. 673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if len(fromstr) != len(tostr): 703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise ValueError, "maketrans arguments must have same length" 713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel global _idmapL 723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not _idmapL: 733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel _idmapL = list(_idmap) 743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel L = _idmapL[:] 753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel fromstr = map(ord, fromstr) 763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel for i in range(len(fromstr)): 773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel L[fromstr[i]] = tostr[i] 783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return ''.join(L) 793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel#################################################################### 833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport re as _re 843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass _multimap: 863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """Helper class for combining multiple mappings. 873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Used by .{safe_,}substitute() to combine the mapping and keyword 893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel arguments. 903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self, primary, secondary): 923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._primary = primary 933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._secondary = secondary 943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __getitem__(self, key): 963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel try: 973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._primary[key] 983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel except KeyError: 993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self._secondary[key] 1003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass _TemplateMetaclass(type): 1033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel pattern = r""" 1043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel %(delim)s(?: 1053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel (?P<escaped>%(delim)s) | # Escape sequence of two delimiters 1063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel (?P<named>%(id)s) | # delimiter and a Python identifier 1073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel {(?P<braced>%(id)s)} | # delimiter and a braced identifier 1083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel (?P<invalid>) # Other ill-formed delimiter exprs 1093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel ) 1103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 1113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(cls, name, bases, dct): 1133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel super(_TemplateMetaclass, cls).__init__(name, bases, dct) 1143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if 'pattern' in dct: 1153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel pattern = cls.pattern 1163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 1173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel pattern = _TemplateMetaclass.pattern % { 1183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 'delim' : _re.escape(cls.delimiter), 1193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 'id' : cls.idpattern, 1203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel } 1213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) 1223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass Template: 1253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """A string class for supporting $-substitutions.""" 1263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel __metaclass__ = _TemplateMetaclass 1273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel delimiter = '$' 1293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel idpattern = r'[_a-z][_a-z0-9]*' 1303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def __init__(self, template): 1323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.template = template 1333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # Search for $$, $identifier, ${identifier}, and any bare $'s 1353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def _invalid(self, mo): 1373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel i = mo.start('invalid') 1383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel lines = self.template[:i].splitlines(True) 1393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not lines: 1403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel colno = 1 1413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel lineno = 1 1423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 1433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel colno = i - len(''.join(lines[:-1])) 1443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel lineno = len(lines) 1453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise ValueError('Invalid placeholder in string: line %d, col %d' % 1463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel (lineno, colno)) 1473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def substitute(*args, **kws): 1493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not args: 1503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise TypeError("descriptor 'substitute' of 'Template' object " 1513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "needs an argument") 1523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self, args = args[0], args[1:] # allow the "self" keyword be passed 1533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if len(args) > 1: 1543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise TypeError('Too many positional arguments') 1553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not args: 1563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel mapping = kws 1573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel elif kws: 1583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel mapping = _multimap(kws, args[0]) 1593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 1603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel mapping = args[0] 1613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # Helper function for .sub() 1623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def convert(mo): 1633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # Check the most common path first. 1643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel named = mo.group('named') or mo.group('braced') 1653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if named is not None: 1663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel val = mapping[named] 1673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # We use this idiom instead of str() because the latter will 1683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # fail if val is a Unicode containing non-ASCII characters. 1693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return '%s' % (val,) 1703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if mo.group('escaped') is not None: 1713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.delimiter 1723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if mo.group('invalid') is not None: 1733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self._invalid(mo) 1743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise ValueError('Unrecognized named group in pattern', 1753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.pattern) 1763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.pattern.sub(convert, self.template) 1773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 1783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def safe_substitute(*args, **kws): 1793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not args: 1803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise TypeError("descriptor 'safe_substitute' of 'Template' object " 1813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "needs an argument") 1823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self, args = args[0], args[1:] # allow the "self" keyword be passed 1833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if len(args) > 1: 1843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise TypeError('Too many positional arguments') 1853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not args: 1863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel mapping = kws 1873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel elif kws: 1883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel mapping = _multimap(kws, args[0]) 1893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 1903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel mapping = args[0] 1913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # Helper function for .sub() 1923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def convert(mo): 1933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel named = mo.group('named') or mo.group('braced') 1943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if named is not None: 1953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel try: 1963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # We use this idiom instead of str() because the latter 1973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # will fail if val is a Unicode containing non-ASCII 1983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return '%s' % (mapping[named],) 1993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel except KeyError: 2003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return mo.group() 2013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if mo.group('escaped') is not None: 2023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.delimiter 2033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if mo.group('invalid') is not None: 2043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return mo.group() 2053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise ValueError('Unrecognized named group in pattern', 2063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.pattern) 2073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.pattern.sub(convert, self.template) 2083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel#################################################################### 2123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# NOTE: Everything below here is deprecated. Use string methods instead. 2133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# This stuff will go away in Python 3.0. 2143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Backward compatible names for exceptions 2163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielindex_error = ValueError 2173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielatoi_error = ValueError 2183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielatof_error = ValueError 2193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielatol_error = ValueError 2203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# convert UPPER CASE letters to lower case 2223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef lower(s): 2233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """lower(s) -> string 2243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s converted to lowercase. 2263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.lower() 2293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Convert lower case letters to UPPER CASE 2313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef upper(s): 2323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """upper(s) -> string 2333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s converted to uppercase. 2353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.upper() 2383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Swap lower case letters and UPPER CASE 2403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef swapcase(s): 2413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """swapcase(s) -> string 2423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s with upper case characters 2443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel converted to lowercase and vice versa. 2453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.swapcase() 2483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Strip leading and trailing tabs and spaces 2503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef strip(s, chars=None): 2513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """strip(s [,chars]) -> string 2523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s with leading and trailing 2543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel whitespace removed. 2553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel If chars is given and not None, remove characters in chars instead. 2563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel If chars is unicode, S will be converted to unicode before stripping. 2573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.strip(chars) 2603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Strip leading tabs and spaces 2623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef lstrip(s, chars=None): 2633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """lstrip(s [,chars]) -> string 2643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s with leading whitespace removed. 2663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel If chars is given and not None, remove characters in chars instead. 2673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.lstrip(chars) 2703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Strip trailing tabs and spaces 2723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef rstrip(s, chars=None): 2733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """rstrip(s [,chars]) -> string 2743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s with trailing whitespace removed. 2763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel If chars is given and not None, remove characters in chars instead. 2773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.rstrip(chars) 2803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Split a string into a list of space/tab-separated words 2833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef split(s, sep=None, maxsplit=-1): 2843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """split(s [,sep [,maxsplit]]) -> list of strings 2853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a list of the words in the string s, using sep as the 2873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel delimiter string. If maxsplit is given, splits at no more than 2883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel maxsplit places (resulting in at most maxsplit+1 words). If sep 2893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel is not specified or is None, any whitespace string is a separator. 2903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel (split and splitfields are synonymous) 2923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 2943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.split(sep, maxsplit) 2953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielsplitfields = split 2963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 2973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Split a string into a list of space/tab-separated words 2983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef rsplit(s, sep=None, maxsplit=-1): 2993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """rsplit(s [,sep [,maxsplit]]) -> list of strings 3003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a list of the words in the string s, using sep as the 3023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel delimiter string, starting at the end of the string and working 3033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel to the front. If maxsplit is given, at most maxsplit splits are 3043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel done. If sep is not specified or is None, any whitespace string 3053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel is a separator. 3063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.rsplit(sep, maxsplit) 3083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Join fields with optional separator 3103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef join(words, sep = ' '): 3113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """join(list [,sep]) -> string 3123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a string composed of the words in list, with 3143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel intervening occurrences of sep. The default separator is a 3153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel single space. 3163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel (joinfields and join are synonymous) 3183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return sep.join(words) 3213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieljoinfields = join 3223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Find substring, raise exception if not found 3243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef index(s, *args): 3253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """index(s, sub [,start [,end]]) -> int 3263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Like find but raises ValueError when the substring is not found. 3283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.index(*args) 3313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Find last substring, raise exception if not found 3333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef rindex(s, *args): 3343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """rindex(s, sub [,start [,end]]) -> int 3353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Like rfind but raises ValueError when the substring is not found. 3373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.rindex(*args) 3403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Count non-overlapping occurrences of substring 3423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef count(s, *args): 3433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """count(s, sub[, start[,end]]) -> int 3443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return the number of occurrences of substring sub in string 3463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel s[start:end]. Optional arguments start and end are 3473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel interpreted as in slice notation. 3483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.count(*args) 3513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Find substring, return -1 if not found 3533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef find(s, *args): 3543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """find(s, sub [,start [,end]]) -> in 3553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return the lowest index in s where substring sub is found, 3573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel such that sub is contained within s[start,end]. Optional 3583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel arguments start and end are interpreted as in slice notation. 3593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return -1 on failure. 3613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.find(*args) 3643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Find last substring, return -1 if not found 3663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef rfind(s, *args): 3673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """rfind(s, sub [,start [,end]]) -> int 3683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return the highest index in s where substring sub is found, 3703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel such that sub is contained within s[start,end]. Optional 3713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel arguments start and end are interpreted as in slice notation. 3723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return -1 on failure. 3743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.rfind(*args) 3773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# for a bit of speed 3793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel_float = float 3803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel_int = int 3813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel_long = long 3823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Convert string to float 3843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef atof(s): 3853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """atof(s) -> float 3863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return the floating point number represented by the string s. 3883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 3903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return _float(s) 3913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Convert string to integer 3943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef atoi(s , base=10): 3953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """atoi(s [,base]) -> int 3963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 3973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return the integer represented by the string s in the given 3983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel base, which defaults to 10. The string s must consist of one 3993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel or more digits, possibly preceded by a sign. If base is 0, it 4003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel is chosen from the leading characters of s, 0 for octal, 0x or 4013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 0X for hexadecimal. If base is 16, a preceding 0x or 0X is 4023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel accepted. 4033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return _int(s, base) 4063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Convert string to long integer 4093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef atol(s, base=10): 4103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """atol(s [,base]) -> long 4113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return the long integer represented by the string s in the 4133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel given base, which defaults to 10. The string s must consist 4143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel of one or more digits, possibly preceded by a sign. If base 4153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel is 0, it is chosen from the leading characters of s, 0 for 4163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel octal, 0x or 0X for hexadecimal. If base is 16, a preceding 4173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 0x or 0X is accepted. A trailing L or l is not accepted, 4183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel unless base is 0. 4193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return _long(s, base) 4223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Left-justify a string 4253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef ljust(s, width, *args): 4263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ljust(s, width[, fillchar]) -> string 4273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a left-justified version of s, in a field of the 4293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel specified width, padded with spaces as needed. The string is 4303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel never truncated. If specified the fillchar is used instead of spaces. 4313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.ljust(width, *args) 4343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Right-justify a string 4363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef rjust(s, width, *args): 4373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """rjust(s, width[, fillchar]) -> string 4383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a right-justified version of s, in a field of the 4403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel specified width, padded with spaces as needed. The string is 4413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel never truncated. If specified the fillchar is used instead of spaces. 4423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.rjust(width, *args) 4453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Center a string 4473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef center(s, width, *args): 4483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """center(s, width[, fillchar]) -> string 4493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a center version of s, in a field of the specified 4513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel width. padded with spaces as needed. The string is never 4523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel truncated. If specified the fillchar is used instead of spaces. 4533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.center(width, *args) 4563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' 4583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Decadent feature: the argument may be a string or a number 4593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# (Use of this is deprecated; it should be a string as with ljust c.s.) 4603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef zfill(x, width): 4613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """zfill(x, width) -> string 4623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Pad a numeric string x with zeros on the left, to fill a field 4643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel of the specified width. The string x is never truncated. 4653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not isinstance(x, basestring): 4683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel x = repr(x) 4693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return x.zfill(width) 4703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Expand tabs in a string. 4723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Doesn't take non-printing chars into account, but does understand \n. 4733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef expandtabs(s, tabsize=8): 4743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """expandtabs(s [,tabsize]) -> string 4753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s with all tab characters replaced 4773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel by the appropriate number of spaces, depending on the current 4783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel column, and the tabsize (default 8). 4793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.expandtabs(tabsize) 4823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Character translation through look-up table. 4843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef translate(s, table, deletions=""): 4853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """translate(s,table [,deletions]) -> string 4863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s, where all characters occurring 4883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel in the optional argument deletions are removed, and the 4893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel remaining characters have been mapped through the given 4903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel translation table, which must be a string of length 256. The 4913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel deletions argument is not allowed for Unicode strings. 4923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 4933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 4943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if deletions or table is None: 4953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.translate(table, deletions) 4963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 4973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # Add s[:0] so that if s is Unicode and table is an 8-bit string, 4983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # table is converted to Unicode. This means that table *cannot* 4993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # be a dictionary -- for that feature, use u.translate() directly. 5003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.translate(table + s[:0]) 5013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Capitalize a string, e.g. "aBc dEf" -> "Abc def". 5033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef capitalize(s): 5043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """capitalize(s) -> string 5053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of the string s with only its first character 5073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel capitalized. 5083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 5103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.capitalize() 5113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Substring replacement (global) 5133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef replace(s, old, new, maxreplace=-1): 5143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """replace (str, old, new[, maxreplace]) -> string 5153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel Return a copy of string str with all occurrences of substring 5173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel old replaced by new. If the optional argument maxreplace is 5183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel given, only the first maxreplace occurrences are replaced. 5193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel """ 5213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return s.replace(old, new, maxreplace) 5223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Try importing optional built-in module "strop" -- if it exists, 5253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# it redefines some string operations that are 100-1000 times faster. 5263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# It also defines values for whitespace, lowercase and uppercase 5273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# that match <ctype.h>'s definitions. 5283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieltry: 5303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel from strop import maketrans, lowercase, uppercase, whitespace 5313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel letters = lowercase + uppercase 5323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielexcept ImportError: 5333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel pass # Use the original versions 5343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel######################################################################## 5363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# the Formatter class 5373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# see PEP 3101 for details and purpose of this class 5383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# The hard parts are reused from the C implementation. They're exposed as "_" 5403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# prefixed methods of str and unicode. 5413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# The overall parser is implemented in str._formatter_parser. 5433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# The field name parser is implemented in str._formatter_field_name_split 5443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass Formatter(object): 5463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def format(*args, **kwargs): 5473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if not args: 5483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise TypeError("descriptor 'format' of 'Formatter' object " 5493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "needs an argument") 5503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self, args = args[0], args[1:] # allow the "self" keyword be passed 5513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel try: 5523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel format_string, args = args[0], args[1:] # allow the "format_string" keyword be passed 5533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel except IndexError: 5543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if 'format_string' in kwargs: 5553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel format_string = kwargs.pop('format_string') 5563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 5573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise TypeError("format() missing 1 required positional " 5583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "argument: 'format_string'") 5593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return self.vformat(format_string, args, kwargs) 5603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def vformat(self, format_string, args, kwargs): 5623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel used_args = set() 5633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel result = self._vformat(format_string, args, kwargs, used_args, 2) 5643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.check_unused_args(used_args, args, kwargs) 5653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return result 5663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def _vformat(self, format_string, args, kwargs, used_args, recursion_depth): 5683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if recursion_depth < 0: 5693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise ValueError('Max string recursion exceeded') 5703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel result = [] 5713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel for literal_text, field_name, format_spec, conversion in \ 5723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel self.parse(format_string): 5733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # output the literal text 5753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if literal_text: 5763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel result.append(literal_text) 5773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # if there's a field, output it 5793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if field_name is not None: 5803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # this is some markup, find the object and do 5813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # the formatting 5823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # given the field_name, find the object it references 5843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # and the argument it came from 5853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel obj, arg_used = self.get_field(field_name, args, kwargs) 5863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel used_args.add(arg_used) 5873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # do any conversion on the resulting object 5893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel obj = self.convert_field(obj, conversion) 5903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5913257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # expand the format spec, if needed 5923257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel format_spec = self._vformat(format_spec, args, kwargs, 5933257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel used_args, recursion_depth-1) 5943257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5953257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # format the object and append to the result 5963257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel result.append(self.format_field(obj, format_spec)) 5973257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 5983257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return ''.join(result) 5993257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6003257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6013257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def get_value(self, key, args, kwargs): 6023257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if isinstance(key, (int, long)): 6033257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return args[key] 6043257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 6053257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return kwargs[key] 6063257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6073257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6083257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def check_unused_args(self, used_args, args, kwargs): 6093257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel pass 6103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def format_field(self, value, format_spec): 6133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return format(value, format_spec) 6143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def convert_field(self, value, conversion): 6173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # do any conversion on the resulting object 6183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if conversion is None: 6193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return value 6203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel elif conversion == 's': 6213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return str(value) 6223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel elif conversion == 'r': 6233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return repr(value) 6243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel raise ValueError("Unknown conversion specifier {0!s}".format(conversion)) 6253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # returns an iterable that contains tuples of the form: 6283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # (literal_text, field_name, format_spec, conversion) 6293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # literal_text can be zero length 6303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # field_name can be None, in which case there's no 6313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # object to format and output 6323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # if field_name is not None, it is looked up, formatted 6333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # with format_spec and conversion and then used 6343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def parse(self, format_string): 6353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return format_string._formatter_parser() 6363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # given a field_name, find the object it references. 6393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # field_name: the field being looked up, e.g. "0.name" 6403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # or "lookup[3]" 6413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # used_args: a set of which args have been used 6423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # args, kwargs: as passed in to vformat 6433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel def get_field(self, field_name, args, kwargs): 6443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel first, rest = field_name._formatter_field_name_split() 6453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel obj = self.get_value(first, args, kwargs) 6473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # loop through the rest of the field_name, doing 6493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel # getattr or getitem as needed 6503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel for is_attr, i in rest: 6513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel if is_attr: 6523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel obj = getattr(obj, i) 6533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel else: 6543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel obj = obj[i] 6553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 6563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel return obj, first 657