cache parsed Formatter functions
This commit is contained in:
parent
a416e54765
commit
befe635022
@ -559,6 +559,7 @@ class Formatter():
|
|||||||
Replaces all occurrences of <old> with <new>
|
Replaces all occurrences of <old> with <new>
|
||||||
Example: {f:R /_/} -> "f_o_o_b_a_r" (if "f" is "f o o b a r")
|
Example: {f:R /_/} -> "f_o_o_b_a_r" (if "f" is "f o o b a r")
|
||||||
"""
|
"""
|
||||||
|
CACHE = {}
|
||||||
CONVERSIONS = {
|
CONVERSIONS = {
|
||||||
"l": str.lower,
|
"l": str.lower,
|
||||||
"u": str.upper,
|
"u": str.upper,
|
||||||
@ -575,19 +576,26 @@ class Formatter():
|
|||||||
|
|
||||||
def __init__(self, format_string, default=None):
|
def __init__(self, format_string, default=None):
|
||||||
self.default = default
|
self.default = default
|
||||||
self.result = []
|
key = (format_string, default)
|
||||||
self.fields = []
|
|
||||||
|
|
||||||
for literal_text, field_name, format_spec, conversion in \
|
try:
|
||||||
_string.formatter_parser(format_string):
|
self.result, self.fields = self.CACHE[key]
|
||||||
if literal_text:
|
except KeyError:
|
||||||
self.result.append(literal_text)
|
self.result = []
|
||||||
if field_name:
|
self.fields = []
|
||||||
self.fields.append((
|
|
||||||
len(self.result),
|
for literal_text, field_name, format_spec, conv in \
|
||||||
self._field_access(field_name, format_spec, conversion),
|
_string.formatter_parser(format_string):
|
||||||
))
|
if literal_text:
|
||||||
self.result.append("")
|
self.result.append(literal_text)
|
||||||
|
if field_name:
|
||||||
|
self.fields.append((
|
||||||
|
len(self.result),
|
||||||
|
self._field_access(field_name, format_spec, conv),
|
||||||
|
))
|
||||||
|
self.result.append("")
|
||||||
|
|
||||||
|
self.CACHE[key] = (self.result, self.fields)
|
||||||
|
|
||||||
if len(self.result) == 1:
|
if len(self.result) == 1:
|
||||||
if self.fields:
|
if self.fields:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user