Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1 | """A collection of string constants. |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 2 | |
| 3 | Public module variables: |
| 4 | |
Georg Brandl | 5076740 | 2008-11-22 08:31:09 +0000 | [diff] [blame] | 5 | whitespace -- a string containing all ASCII whitespace |
| 6 | ascii_lowercase -- a string containing all ASCII lowercase letters |
| 7 | ascii_uppercase -- a string containing all ASCII uppercase letters |
| 8 | ascii_letters -- a string containing all ASCII letters |
| 9 | digits -- a string containing all ASCII decimal digits |
| 10 | hexdigits -- a string containing all ASCII hexadecimal digits |
| 11 | octdigits -- a string containing all ASCII octal digits |
| 12 | punctuation -- a string containing all ASCII punctuation characters |
| 13 | printable -- a string containing all ASCII characters considered printable |
Guido van Rossum | 2003204 | 1997-12-29 19:26:28 +0000 | [diff] [blame] | 14 | |
| 15 | """ |
| 16 | |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 17 | # Some strings for ctype-style character classification |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 18 | whitespace = ' \t\n\r\v\f' |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 19 | ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 20 | ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
Fred Drake | 960fdf9 | 2001-07-20 18:38:26 +0000 | [diff] [blame] | 21 | ascii_letters = ascii_lowercase + ascii_uppercase |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 22 | digits = '0123456789' |
| 23 | hexdigits = digits + 'abcdef' + 'ABCDEF' |
| 24 | octdigits = '01234567' |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 25 | punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 26 | printable = digits + ascii_letters + punctuation + whitespace |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 27 | |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 28 | # Functions which aren't available as string methods. |
| 29 | |
| 30 | # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 31 | def capwords(s, sep=None): |
| 32 | """capwords(s, [sep]) -> string |
| 33 | |
| 34 | Split the argument into words using split, capitalize each |
| 35 | word using capitalize, and join the capitalized words using |
| 36 | join. Note that this replaces runs of whitespace characters by |
| 37 | a single space. |
| 38 | |
| 39 | """ |
| 40 | return (sep or ' ').join([x.capitalize() for x in s.split(sep)]) |
| 41 | |
| 42 | |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 43 | # Construct a translation map for bytes.translate |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 44 | def maketrans(frm: bytes, to: bytes) -> bytes: |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 45 | """maketrans(frm, to) -> bytes |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 46 | |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 47 | Return a translation table (a bytes object of length 256) |
| 48 | suitable for use in bytes.translate where each byte in frm is |
| 49 | mapped to the byte at the same position in to. |
| 50 | The strings frm and to must be of the same length. |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 51 | """ |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 52 | if len(frm) != len(to): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 53 | raise ValueError("maketrans arguments must have same length") |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 54 | if not (isinstance(frm, bytes) and isinstance(to, bytes)): |
| 55 | raise TypeError("maketrans arguments must be bytes objects") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 56 | L = bytearray(range(256)) |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 57 | for i, c in enumerate(frm): |
| 58 | L[c] = to[i] |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 59 | return bytes(L) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 60 | |
Raymond Hettinger | 57aef9c | 2004-12-07 07:55:07 +0000 | [diff] [blame] | 61 | |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 62 | #################################################################### |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 63 | import re as _re |
| 64 | |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 65 | class _multimap: |
| 66 | """Helper class for combining multiple mappings. |
| 67 | |
| 68 | Used by .{safe_,}substitute() to combine the mapping and keyword |
| 69 | arguments. |
| 70 | """ |
| 71 | def __init__(self, primary, secondary): |
| 72 | self._primary = primary |
| 73 | self._secondary = secondary |
| 74 | |
| 75 | def __getitem__(self, key): |
| 76 | try: |
| 77 | return self._primary[key] |
| 78 | except KeyError: |
| 79 | return self._secondary[key] |
| 80 | |
| 81 | |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 82 | class _TemplateMetaclass(type): |
| 83 | pattern = r""" |
Raymond Hettinger | 55593c3 | 2004-09-26 18:56:44 +0000 | [diff] [blame] | 84 | %(delim)s(?: |
| 85 | (?P<escaped>%(delim)s) | # Escape sequence of two delimiters |
| 86 | (?P<named>%(id)s) | # delimiter and a Python identifier |
| 87 | {(?P<braced>%(id)s)} | # delimiter and a braced identifier |
| 88 | (?P<invalid>) # Other ill-formed delimiter exprs |
| 89 | ) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 90 | """ |
| 91 | |
| 92 | def __init__(cls, name, bases, dct): |
| 93 | super(_TemplateMetaclass, cls).__init__(name, bases, dct) |
| 94 | if 'pattern' in dct: |
| 95 | pattern = cls.pattern |
| 96 | else: |
| 97 | pattern = _TemplateMetaclass.pattern % { |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 98 | 'delim' : _re.escape(cls.delimiter), |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 99 | 'id' : cls.idpattern, |
| 100 | } |
| 101 | cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) |
| 102 | |
| 103 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 104 | class Template(metaclass=_TemplateMetaclass): |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 105 | """A string class for supporting $-substitutions.""" |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 106 | |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 107 | delimiter = '$' |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 108 | idpattern = r'[_a-z][_a-z0-9]*' |
| 109 | |
| 110 | def __init__(self, template): |
| 111 | self.template = template |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 112 | |
| 113 | # Search for $$, $identifier, ${identifier}, and any bare $'s |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 114 | |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 115 | def _invalid(self, mo): |
| 116 | i = mo.start('invalid') |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 117 | lines = self.template[:i].splitlines(True) |
| 118 | if not lines: |
| 119 | colno = 1 |
| 120 | lineno = 1 |
| 121 | else: |
| 122 | colno = i - len(''.join(lines[:-1])) |
| 123 | lineno = len(lines) |
| 124 | raise ValueError('Invalid placeholder in string: line %d, col %d' % |
| 125 | (lineno, colno)) |
| 126 | |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 127 | def substitute(self, *args, **kws): |
| 128 | if len(args) > 1: |
| 129 | raise TypeError('Too many positional arguments') |
| 130 | if not args: |
| 131 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 132 | elif kws: |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 133 | mapping = _multimap(kws, args[0]) |
| 134 | else: |
| 135 | mapping = args[0] |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 136 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 137 | def convert(mo): |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 138 | # Check the most common path first. |
| 139 | named = mo.group('named') or mo.group('braced') |
| 140 | if named is not None: |
| 141 | val = mapping[named] |
| 142 | # We use this idiom instead of str() because the latter will |
| 143 | # fail if val is a Unicode containing non-ASCII characters. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 144 | return '%s' % (val,) |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 145 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 146 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 147 | if mo.group('invalid') is not None: |
| 148 | self._invalid(mo) |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 149 | raise ValueError('Unrecognized named group in pattern', |
| 150 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 151 | return self.pattern.sub(convert, self.template) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 152 | |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 153 | def safe_substitute(self, *args, **kws): |
| 154 | if len(args) > 1: |
| 155 | raise TypeError('Too many positional arguments') |
| 156 | if not args: |
| 157 | mapping = kws |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 158 | elif kws: |
Barry Warsaw | b6234a9 | 2004-09-13 15:25:15 +0000 | [diff] [blame] | 159 | mapping = _multimap(kws, args[0]) |
| 160 | else: |
| 161 | mapping = args[0] |
Barry Warsaw | 46b629c | 2004-09-13 14:35:04 +0000 | [diff] [blame] | 162 | # Helper function for .sub() |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 163 | def convert(mo): |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 164 | named = mo.group('named') |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 165 | if named is not None: |
| 166 | try: |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 167 | # We use this idiom instead of str() because the latter |
| 168 | # will fail if val is a Unicode containing non-ASCII |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 169 | return '%s' % (mapping[named],) |
Barry Warsaw | 8bee761 | 2004-08-25 02:22:30 +0000 | [diff] [blame] | 170 | except KeyError: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 171 | return self.delimiter + named |
Raymond Hettinger | 0d58e2b | 2004-08-26 00:21:13 +0000 | [diff] [blame] | 172 | braced = mo.group('braced') |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 173 | if braced is not None: |
| 174 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 175 | return '%s' % (mapping[braced],) |
Raymond Hettinger | 6d19111 | 2004-09-14 02:34:08 +0000 | [diff] [blame] | 176 | except KeyError: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 177 | return self.delimiter + '{' + braced + '}' |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 178 | if mo.group('escaped') is not None: |
Barry Warsaw | 17cb600 | 2004-09-18 00:06:34 +0000 | [diff] [blame] | 179 | return self.delimiter |
Barry Warsaw | b5c6b5b | 2004-09-13 20:52:50 +0000 | [diff] [blame] | 180 | if mo.group('invalid') is not None: |
Barry Warsaw | 8c72eae | 2004-11-01 03:52:43 +0000 | [diff] [blame] | 181 | return self.delimiter |
Neal Norwitz | 6627a96 | 2004-10-17 16:27:18 +0000 | [diff] [blame] | 182 | raise ValueError('Unrecognized named group in pattern', |
| 183 | self.pattern) |
Barry Warsaw | 12827c1 | 2004-09-10 03:08:08 +0000 | [diff] [blame] | 184 | return self.pattern.sub(convert, self.template) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | |
| 188 | ######################################################################## |
| 189 | # the Formatter class |
| 190 | # see PEP 3101 for details and purpose of this class |
| 191 | |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 192 | # The hard parts are reused from the C implementation. They're exposed as "_" |
| 193 | # prefixed methods of str and unicode. |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 194 | |
Eric Smith | e226b55 | 2007-08-27 11:28:18 +0000 | [diff] [blame] | 195 | # The overall parser is implemented in str._formatter_parser. |
| 196 | # The field name parser is implemented in str._formatter_field_name_split |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 197 | |
| 198 | class Formatter: |
| 199 | def format(self, format_string, *args, **kwargs): |
| 200 | return self.vformat(format_string, args, kwargs) |
| 201 | |
| 202 | def vformat(self, format_string, args, kwargs): |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 203 | used_args = set() |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 204 | result = self._vformat(format_string, args, kwargs, used_args, 2) |
| 205 | self.check_unused_args(used_args, args, kwargs) |
| 206 | return result |
| 207 | |
| 208 | def _vformat(self, format_string, args, kwargs, used_args, recursion_depth): |
| 209 | if recursion_depth < 0: |
| 210 | raise ValueError('Max string recursion exceeded') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 211 | result = [] |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 212 | for literal_text, field_name, format_spec, conversion in \ |
| 213 | self.parse(format_string): |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 214 | |
| 215 | # output the literal text |
| 216 | if literal_text: |
| 217 | result.append(literal_text) |
| 218 | |
| 219 | # if there's a field, output it |
| 220 | if field_name is not None: |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 221 | # this is some markup, find the object and do |
| 222 | # the formatting |
| 223 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 224 | # given the field_name, find the object it references |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 225 | # and the argument it came from |
Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 226 | obj, arg_used = self.get_field(field_name, args, kwargs) |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 227 | used_args.add(arg_used) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 228 | |
| 229 | # do any conversion on the resulting object |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 230 | obj = self.convert_field(obj, conversion) |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 231 | |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 232 | # expand the format spec, if needed |
| 233 | format_spec = self._vformat(format_spec, args, kwargs, |
| 234 | used_args, recursion_depth-1) |
| 235 | |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 236 | # format the object and append to the result |
| 237 | result.append(self.format_field(obj, format_spec)) |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 238 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 239 | return ''.join(result) |
| 240 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 241 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 242 | def get_value(self, key, args, kwargs): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 243 | if isinstance(key, int): |
| 244 | return args[key] |
| 245 | else: |
| 246 | return kwargs[key] |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 247 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 248 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 249 | def check_unused_args(self, used_args, args, kwargs): |
| 250 | pass |
| 251 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 252 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 253 | def format_field(self, value, format_spec): |
Eric Smith | 7ade648 | 2007-08-26 22:27:13 +0000 | [diff] [blame] | 254 | return format(value, format_spec) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | def convert_field(self, value, conversion): |
| 258 | # do any conversion on the resulting object |
| 259 | if conversion == 'r': |
| 260 | return repr(value) |
| 261 | elif conversion == 's': |
| 262 | return str(value) |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 263 | elif conversion is None: |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 264 | return value |
Eric Smith | 1152919 | 2007-09-04 23:04:22 +0000 | [diff] [blame] | 265 | raise ValueError("Unknown converion specifier {0!s}".format(conversion)) |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 266 | |
| 267 | |
| 268 | # returns an iterable that contains tuples of the form: |
| 269 | # (literal_text, field_name, format_spec, conversion) |
Eric Smith | 625cbf2 | 2007-08-29 03:22:59 +0000 | [diff] [blame] | 270 | # literal_text can be zero length |
| 271 | # field_name can be None, in which case there's no |
| 272 | # object to format and output |
| 273 | # if field_name is not None, it is looked up, formatted |
| 274 | # with format_spec and conversion and then used |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 275 | def parse(self, format_string): |
| 276 | return format_string._formatter_parser() |
| 277 | |
| 278 | |
| 279 | # given a field_name, find the object it references. |
| 280 | # field_name: the field being looked up, e.g. "0.name" |
| 281 | # or "lookup[3]" |
| 282 | # used_args: a set of which args have been used |
| 283 | # args, kwargs: as passed in to vformat |
Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 284 | def get_field(self, field_name, args, kwargs): |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 285 | first, rest = field_name._formatter_field_name_split() |
| 286 | |
Eric Smith | 9e7c8da | 2007-08-28 11:15:20 +0000 | [diff] [blame] | 287 | obj = self.get_value(first, args, kwargs) |
| 288 | |
| 289 | # loop through the rest of the field_name, doing |
| 290 | # getattr or getitem as needed |
| 291 | for is_attr, i in rest: |
| 292 | if is_attr: |
| 293 | obj = getattr(obj, i) |
| 294 | else: |
| 295 | obj = obj[i] |
| 296 | |
Eric Smith | 3bcc42a | 2007-08-31 02:26:31 +0000 | [diff] [blame] | 297 | return obj, first |