|
33 | 33 | # `np.minimum` instead of the builtin `min`, and likewise for `max`. This is
|
34 | 34 | # done so that `nan`s are propagated, instead of being silently dropped.
|
35 | 35 |
|
36 |
| -import re |
| 36 | +import functools |
| 37 | +import textwrap |
37 | 38 | import weakref
|
38 | 39 |
|
39 | 40 | import numpy as np
|
|
47 | 48 | DEBUG = False
|
48 | 49 |
|
49 | 50 |
|
50 |
| -def _indent_str(obj): # textwrap.indent(str(obj), 4) on Py3. |
51 |
| - return re.sub("(^|\n)", r"\1 ", str(obj)) |
| 51 | +def _make_str_method(*args, **kwargs): |
| 52 | + """ |
| 53 | + Generate a ``__str__`` method for a `.Transform` subclass. |
| 54 | +
|
| 55 | + After :: |
| 56 | +
|
| 57 | + class T: |
| 58 | + __str__ = _make_str_method("attr", key="other") |
| 59 | +
|
| 60 | + ``str(T(...))`` will be |
| 61 | +
|
| 62 | + .. code-block:: text |
| 63 | +
|
| 64 | + {type(T).__name__}( |
| 65 | + {self.attr}, |
| 66 | + key={self.other}) |
| 67 | + """ |
| 68 | + indent = functools.partial(textwrap.indent, prefix=" " * 4) |
| 69 | + def strrepr(x): return repr(x) if isinstance(x, str) else str(x) |
| 70 | + return lambda self: ( |
| 71 | + type(self).__name__ + "(" |
| 72 | + + ",".join([*(indent("\n" + strrepr(getattr(self, arg))) |
| 73 | + for arg in args), |
| 74 | + *(indent("\n" + k + "=" + strrepr(getattr(self, arg))) |
| 75 | + for k, arg in kwargs.items())]) |
| 76 | + + ")") |
52 | 77 |
|
53 | 78 |
|
54 | 79 | class TransformNode:
|
@@ -1001,13 +1026,7 @@ def __init__(self, bbox, transform, **kwargs):
|
1001 | 1026 | self.set_children(bbox, transform)
|
1002 | 1027 | self._points = None
|
1003 | 1028 |
|
1004 |
| - def __str__(self): |
1005 |
| - return ("{}(\n" |
1006 |
| - "{},\n" |
1007 |
| - "{})" |
1008 |
| - .format(type(self).__name__, |
1009 |
| - _indent_str(self._bbox), |
1010 |
| - _indent_str(self._transform))) |
| 1029 | + __str__ = _make_str_method("_bbox", "_transform") |
1011 | 1030 |
|
1012 | 1031 | def get_points(self):
|
1013 | 1032 | # docstring inherited
|
@@ -1086,13 +1105,7 @@ def __init__(self, bbox, x0=None, y0=None, x1=None, y1=None, **kwargs):
|
1086 | 1105 | mask = [val is None for val in fp]
|
1087 | 1106 | self._locked_points = np.ma.array(fp, float, mask=mask).reshape((2, 2))
|
1088 | 1107 |
|
1089 |
| - def __str__(self): |
1090 |
| - return ("{}(\n" |
1091 |
| - "{},\n" |
1092 |
| - "{})" |
1093 |
| - .format(type(self).__name__, |
1094 |
| - _indent_str(self._bbox), |
1095 |
| - _indent_str(self._locked_points))) |
| 1108 | + __str__ = _make_str_method("_bbox", "_locked_points") |
1096 | 1109 |
|
1097 | 1110 | def get_points(self):
|
1098 | 1111 | # docstring inherited
|
@@ -1625,11 +1638,7 @@ def _init(self, child):
|
1625 | 1638 | def __eq__(self, other):
|
1626 | 1639 | return self._child.__eq__(other)
|
1627 | 1640 |
|
1628 |
| - def __str__(self): |
1629 |
| - return ("{}(\n" |
1630 |
| - "{})" |
1631 |
| - .format(type(self).__name__, |
1632 |
| - _indent_str(self._child))) |
| 1641 | + __str__ = _make_str_method("_child") |
1633 | 1642 |
|
1634 | 1643 | def frozen(self):
|
1635 | 1644 | # docstring inherited
|
@@ -1831,11 +1840,7 @@ def __init__(self, matrix=None, **kwargs):
|
1831 | 1840 | self._mtx = matrix
|
1832 | 1841 | self._invalid = 0
|
1833 | 1842 |
|
1834 |
| - def __str__(self): |
1835 |
| - return ("{}(\n" |
1836 |
| - "{})" |
1837 |
| - .format(type(self).__name__, |
1838 |
| - _indent_str(self._mtx))) |
| 1843 | + __str__ = _make_str_method("_mtx") |
1839 | 1844 |
|
1840 | 1845 | @staticmethod
|
1841 | 1846 | def from_values(a, b, c, d, e, f):
|
@@ -2032,9 +2037,7 @@ def frozen(self):
|
2032 | 2037 | # docstring inherited
|
2033 | 2038 | return self
|
2034 | 2039 |
|
2035 |
| - def __str__(self): |
2036 |
| - return ("{}()" |
2037 |
| - .format(type(self).__name__)) |
| 2040 | + __str__ = _make_str_method() |
2038 | 2041 |
|
2039 | 2042 | def get_matrix(self):
|
2040 | 2043 | # docstring inherited
|
@@ -2088,13 +2091,7 @@ def contains_branch_seperately(self, transform):
|
2088 | 2091 | return (self._x.contains_branch(transform),
|
2089 | 2092 | self._y.contains_branch(transform))
|
2090 | 2093 |
|
2091 |
| - def __str__(self): |
2092 |
| - return ("{}(\n" |
2093 |
| - "{},\n" |
2094 |
| - "{})" |
2095 |
| - .format(type(self).__name__, |
2096 |
| - _indent_str(self._x), |
2097 |
| - _indent_str(self._y))) |
| 2094 | + __str__ = _make_str_method("_x", "_y") |
2098 | 2095 |
|
2099 | 2096 |
|
2100 | 2097 | class BlendedGenericTransform(_BlendedMixin, Transform):
|
@@ -2333,13 +2330,7 @@ def _iter_break_from_left_to_right(self):
|
2333 | 2330 | has_inverse = property(
|
2334 | 2331 | lambda self: self._a.has_inverse and self._b.has_inverse)
|
2335 | 2332 |
|
2336 |
| - def __str__(self): |
2337 |
| - return ("{}(\n" |
2338 |
| - "{},\n" |
2339 |
| - "{})" |
2340 |
| - .format(type(self).__name__, |
2341 |
| - _indent_str(self._a), |
2342 |
| - _indent_str(self._b))) |
| 2333 | + __str__ = _make_str_method("_a", "_b") |
2343 | 2334 |
|
2344 | 2335 | def transform_affine(self, points):
|
2345 | 2336 | # docstring inherited
|
@@ -2419,13 +2410,7 @@ def _iter_break_from_left_to_right(self):
|
2419 | 2410 | for left, right in self._b._iter_break_from_left_to_right():
|
2420 | 2411 | yield self._a + left, right
|
2421 | 2412 |
|
2422 |
| - def __str__(self): |
2423 |
| - return ("{}(\n" |
2424 |
| - "{},\n" |
2425 |
| - "{})" |
2426 |
| - .format(type(self).__name__, |
2427 |
| - _indent_str(self._a), |
2428 |
| - _indent_str(self._b))) |
| 2413 | + __str__ = _make_str_method("_a", "_b") |
2429 | 2414 |
|
2430 | 2415 | def get_matrix(self):
|
2431 | 2416 | # docstring inherited
|
@@ -2486,13 +2471,7 @@ def __init__(self, boxin, boxout, **kwargs):
|
2486 | 2471 | self._mtx = None
|
2487 | 2472 | self._inverted = None
|
2488 | 2473 |
|
2489 |
| - def __str__(self): |
2490 |
| - return ("{}(\n" |
2491 |
| - "{},\n" |
2492 |
| - "{})" |
2493 |
| - .format(type(self).__name__, |
2494 |
| - _indent_str(self._boxin), |
2495 |
| - _indent_str(self._boxout))) |
| 2474 | + __str__ = _make_str_method("_boxin", "_boxout") |
2496 | 2475 |
|
2497 | 2476 | def get_matrix(self):
|
2498 | 2477 | # docstring inherited
|
@@ -2534,11 +2513,7 @@ def __init__(self, boxout, **kwargs):
|
2534 | 2513 | self._mtx = None
|
2535 | 2514 | self._inverted = None
|
2536 | 2515 |
|
2537 |
| - def __str__(self): |
2538 |
| - return ("{}(\n" |
2539 |
| - "{})" |
2540 |
| - .format(type(self).__name__, |
2541 |
| - _indent_str(self._boxout))) |
| 2516 | + __str__ = _make_str_method("_boxout") |
2542 | 2517 |
|
2543 | 2518 | def get_matrix(self):
|
2544 | 2519 | # docstring inherited
|
@@ -2592,11 +2567,7 @@ def __init__(self, boxin, **kwargs):
|
2592 | 2567 | self._mtx = None
|
2593 | 2568 | self._inverted = None
|
2594 | 2569 |
|
2595 |
| - def __str__(self): |
2596 |
| - return ("{}(\n" |
2597 |
| - "{})" |
2598 |
| - .format(type(self).__name__, |
2599 |
| - _indent_str(self._boxin))) |
| 2570 | + __str__ = _make_str_method("_boxin") |
2600 | 2571 |
|
2601 | 2572 | def get_matrix(self):
|
2602 | 2573 | # docstring inherited
|
@@ -2628,11 +2599,7 @@ def __init__(self, xt, yt, scale_trans, **kwargs):
|
2628 | 2599 | self._mtx = None
|
2629 | 2600 | self._inverted = None
|
2630 | 2601 |
|
2631 |
| - def __str__(self): |
2632 |
| - return ("{}(\n" |
2633 |
| - "{})" |
2634 |
| - .format(type(self).__name__, |
2635 |
| - _indent_str(self._t))) |
| 2602 | + __str__ = _make_str_method("_t") |
2636 | 2603 |
|
2637 | 2604 | def get_matrix(self):
|
2638 | 2605 | # docstring inherited
|
|
0 commit comments