[diffoscope] 03/08: presenters: PartialString: add numl, pformatl, formatl convenience methods
Ximin Luo
infinity0 at debian.org
Mon Jul 3 20:27:50 CEST 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch WIP/humungous-diffs
in repository diffoscope.
commit 3b4a3986632dcb23a6375f637e45e976c8ee0afc
Author: Ximin Luo <infinity0 at debian.org>
Date: Mon Jul 3 20:04:02 2017 +0200
presenters: PartialString: add numl, pformatl, formatl convenience methods
---
diffoscope/presenters/utils.py | 34 ++++++++++++++++++++++++++++++++--
tests/test_presenters.py | 5 +++++
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index 69040d6..9f49069 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -129,7 +129,7 @@ class FormatPlaceholder(object):
class PartialString(object):
- """A format string where the "holes" are indexed by arbitrary python
+ r"""A format string where the "holes" are indexed by arbitrary python
objects instead of string names or integer indexes. This is useful when you
need to compose these objects together, but don't want users of the partial
string to have to deal with artificial "indexes" for the holes.
@@ -176,6 +176,16 @@ class PartialString(object):
...
IndexError: tuple index out of range
+ If you don't have specific objects to index the holes with, and don't want
+ to create artifical indexes as in the above examples, here is a slightly
+ simpler way of doing it:
+
+ >>> tmpl = PartialString.numl("{0} {1} {2}", 2, object())
+ >>> tmpl.holes
+ (0, 1, <object ...>)
+ >>> tmpl.pformatl("(first hole)", "(second hole)", "(object hole)")
+ PartialString('(first hole) (second hole) (object hole)',)
+
CAVEATS:
Filling up holes using other PartialStrings, does not play very nicely with
@@ -245,6 +255,10 @@ class PartialString(object):
new_fmtstr, new_holes = self._pformat(mapping)
return self.__class__(new_fmtstr, *new_holes)
+ def pformatl(self, *args):
+ """Partially apply a list, implicitly mapped from self.holes."""
+ return self.pformat(dict(zip(self.holes, args)))
+
def format(self, mapping):
"""Fully apply a mapping, returning a string."""
new_fmtstr, new_holes = self._pformat(mapping)
@@ -263,6 +277,19 @@ class PartialString(object):
return cls("{0}", obj)
@classmethod
+ def numl(cls, fmtstr="", nargs=0, *holes):
+ """Create a partial string with some implicit numeric holes.
+
+ Useful in conjuction with PartialString.pformatl.
+
+ >>> PartialString.numl("{0}{1}{2}{3}", 3, "last object")
+ PartialString('{0}{1}{2}{3}', 0, 1, 2, 'last object')
+ >>> _.pformatl(40, 41, 42, "final")
+ PartialString('404142final',)
+ """
+ return cls(fmtstr, *range(nargs), *holes)
+
+ @classmethod
def cont(cls):
r"""Create a new empty partial string with a continuation token.
@@ -282,7 +309,10 @@ class PartialString(object):
27
"""
def cont(t, fmtstr, *holes):
- return t.pformat({cont: cls(fmtstr, *(holes + (cont,)))})
+ if isinstance(fmtstr, cls):
+ return t.pformat({cont: fmtstr})
+ else:
+ return t.pformat({cont: cls(fmtstr, *(holes + (cont,)))})
return cls("{0}", cont), cont
diff --git a/tests/test_presenters.py b/tests/test_presenters.py
index 8fdaab9..c222993 100644
--- a/tests/test_presenters.py
+++ b/tests/test_presenters.py
@@ -178,3 +178,8 @@ def test_partial_string_cont():
assert (t.format({key[0]: "line1", key[1]: "line2", key[2]: "line3"})
== 'x: line1\ny: line2\nz: line3\n')
assert t.size(hole_size=5) == 27
+def test_partial_string_numl():
+ tmpl = PartialString.numl("{0} {1} {2}", 2, object())
+ assert tmpl.holes[:2] == (0, 1)
+ assert tmpl.pformatl("(1)", "(2)", "(o)") == PartialString('(1) (2) (o)')
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list