Mercurial > cpython-lru_cache-weakref
diff Lib/string.py @ 42817:5355337709e1
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.
| author | Eric Smith <eric@trueblade.com> |
|---|---|
| date | Sat, 25 Aug 2007 02:26:07 +0000 |
| parents | b623777f22b0 |
| children | 5fd65e250843 |
line wrap: on
line diff
--- a/Lib/string.py Sat Aug 25 01:33:49 2007 +0000 +++ b/Lib/string.py Sat Aug 25 02:26:07 2007 +0000 @@ -189,3 +189,42 @@ 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
