re-implement and improve filename formatter

A format string now gets parsed only once instead of re-parsing it each
time it is applied to a set of data.

The initial parsing causes directory path creation to be at about 2x
slower than before, since each format string there is used only once,
but building a filename, the more common operation, is at least 2x
faster. The "directory slowness" cancels at about 5 filenames and
everything above that is significantly faster.
This commit is contained in:
Mike Fährmann
2018-08-24 20:21:05 +02:00
parent 34b556922d
commit 590c0b3ad5
2 changed files with 114 additions and 55 deletions

View File

@@ -209,6 +209,13 @@ class TestFormatter(unittest.TestCase):
self._run_test("{a[:5]}" , v[:5])
self._run_test("{a[:50]}", v[:50])
self._run_test("{a[:]}" , v)
self._run_test("{a[1:10:2]}" , v[1:10:2])
self._run_test("{a[-10:-1:2]}", v[-10:-1:2])
self._run_test("{a[5::2]}" , v[5::2])
self._run_test("{a[50::2]}", v[50::2])
self._run_test("{a[:5:2]}" , v[:5:2])
self._run_test("{a[:50:2]}", v[:50:2])
self._run_test("{a[::]}" , v)
def test_maxlen(self):
v = self.kwdict["a"]
@@ -219,8 +226,8 @@ class TestFormatter(unittest.TestCase):
self._run_test("{a:Lab/foo/}", "foo")
def _run_test(self, format_string, result, default=None):
formatter = util.Formatter(default)
output = formatter.vformat(format_string, self.kwdict)
formatter = util.Formatter(format_string, default)
output = formatter.format_map(self.kwdict)
self.assertEqual(output, result, format_string)