From 8c6632636807c35bee40210ed8483c1eca82664f Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Sat, 25 Aug 2007 02:26:07 +0000 Subject: Implementation of PEP 3101, Advanced String Formatting. Known issues: The string.Formatter class, as discussed in the PEP, is incomplete. Error handling needs to conform to the PEP. Need to fix this warning that I introduced in Python/formatter_unicode.c: Objects/stringlib/unicodedefs.h:26: warning: `STRINGLIB_CMP' defined but not used Need to make sure sign formatting is correct, more tests needed. Need to remove '()' sign formatting, left over from an earlier version of the PEP. --- Lib/string.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'Lib/string.py') diff --git a/Lib/string.py b/Lib/string.py index 87073aab4d..9df78afd1f 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -189,3 +189,42 @@ class Template(metaclass=_TemplateMetaclass): raise ValueError('Unrecognized named group in pattern', self.pattern) return self.pattern.sub(convert, self.template) + + + +######################################################################## +# the Formatter class +# see PEP 3101 for details and purpose of this class + +# The hard parts are reused from the C implementation. They're +# 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 + +from sys import _formatter_parser, _formatter_lookup + +class Formatter: + def format(self, format_string, *args, **kwargs): + return self.vformat(format_string, args, kwargs) + + def vformat(self, format_string, args, kwargs): + 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) + else: + result.append(literal) + return ''.join(result) + + def get_value(self, key, args, kwargs): + pass + + def check_unused_args(self, used_args, args, kwargs): + pass + + def format_field(self, value, format_spec): + pass -- cgit v1.2.1