mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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.
This commit is contained in:
parent
e4dc324884
commit
8c66326368
22 changed files with 2669 additions and 18 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue