summaryrefslogtreecommitdiff
path: root/Lib/string.py
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2007-08-25 02:26:07 +0000
committerEric Smith <eric@trueblade.com>2007-08-25 02:26:07 +0000
commit8c6632636807c35bee40210ed8483c1eca82664f (patch)
tree50f386d98ce14116eaf9d83085b82ff11bdb3e69 /Lib/string.py
parente4dc32488446240942123cf4e9e7296ad97e20bf (diff)
downloadcpython-git-8c6632636807c35bee40210ed8483c1eca82664f.tar.gz
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.
Diffstat (limited to 'Lib/string.py')
-rw-r--r--Lib/string.py39
1 files changed, 39 insertions, 0 deletions
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