diff options
author | Eric Smith <eric@trueblade.com> | 2007-08-26 22:27:13 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2007-08-26 22:27:13 (GMT) |
commit | 7ade6485abde95c5cc9676ad3e476ba3aca98037 (patch) | |
tree | fa5710899c3e376f89eb6a6460e06f3feee62d58 /Lib/string.py | |
parent | 2bf4d5ba2881725bb7695bc0573bab0e2ca4fec5 (diff) | |
download | cpython-7ade6485abde95c5cc9676ad3e476ba3aca98037.zip cpython-7ade6485abde95c5cc9676ad3e476ba3aca98037.tar.gz cpython-7ade6485abde95c5cc9676ad3e476ba3aca98037.tar.bz2 |
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Diffstat (limited to 'Lib/string.py')
-rw-r--r-- | Lib/string.py | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/Lib/string.py b/Lib/string.py index 9df78af..edf5ae2 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -200,31 +200,59 @@ class Template(metaclass=_TemplateMetaclass): # exposed here via the sys module. sys was chosen because it's always # available and doesn't have to be dynamically loaded. -# The parser is implemented in sys._formatter_parser. -# The "object lookup" is implemented in sys._formatter_lookup +# The overall parser is implemented in sys._formatter_parser. +# The field name parser is implemented in sys._formatter_field_name_split -from sys import _formatter_parser, _formatter_lookup +from sys import _formatter_parser, _formatter_field_name_split class Formatter: def format(self, format_string, *args, **kwargs): return self.vformat(format_string, args, kwargs) def vformat(self, format_string, args, kwargs): + used_args = set() result = [] for (is_markup, literal, field_name, format_spec, conversion) in \ _formatter_parser(format_string): if is_markup: - # find the object - index, name, obj = _formatter_lookup(field_name, args, kwargs) + # given the field_name, find the object it references + + # split it into the first part, and and iterator that + # looks over the rest + first, rest = _formatter_field_name_split(field_name) + + used_args.add(first) + obj = self.get_value(first, args, kwargs) + + # loop through the rest of the field_name, doing + # getattr or getitem as needed + for is_attr, i in rest: + if is_attr: + obj = getattr(obj, i) + else: + obj = obj[i] + + # do any conversion on the resulting object + if conversion == 'r': + obj = repr(obj) + elif conversion == 's': + obj = str(obj) + + # format the object and append to the result + result.append(self.format_field(obj, format_spec)) else: result.append(literal) + self.check_unused_args(used_args, args, kwargs) return ''.join(result) def get_value(self, key, args, kwargs): - pass + if isinstance(key, int): + return args[key] + else: + return kwargs[key] def check_unused_args(self, used_args, args, kwargs): pass def format_field(self, value, format_spec): - pass + return format(value, format_spec) |