8000 Improve reprs of transforms. · matplotlib/matplotlib@3603921 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3603921

Browse files
committed
Improve reprs of transforms.
This patch should make debugging the transform a bit stack easier, by changing, for example, the repr of the transform of a line created with `plot([1, 2])` from CompositeGenericTransform(TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())), CompositeGenericTransform(BboxTransformFrom(TransformedBbox(Bbox([[-0.05, 0.95], [1.05, 2.05]]), TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [6.4, 4.8]]), Affine2D(array([[ 100., 0., 0.], [ 0., 100., 0.], [ 0., 0., 1.]])))))))) to CompositeGenericTransform( TransformWrapper( BlendedAffine2D( IdentityTransform(), IdentityTransform())), CompositeGenericTransform( BboxTransformFrom( TransformedBbox( Bbox(x0=-0.05, y0=0.95, x1=1.05, y1=2.05), TransformWrapper( BlendedAffine2D( IdentityTransform(), IdentityTransform())))), BboxTransformTo( TransformedBbox( Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88), BboxTransformTo( TransformedBbox( Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.8), Affine2D( [[ 100. 0. 0.] [ 0. 100. 0.] [ 0. 0. 1.]]))))))) This will, of course, invalidate code that relied on the exact strs, but what can you do.
1 parent 1b5223a commit 3603921

File tree

1 file changed

+107
-32
lines changed

1 file changed

+107
-32
lines changed

lib/matplotlib/transforms.py

Lines changed: 107 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
update_path_extents)
4444
from numpy.linalg import inv
4545

46+
import re
4647
import weakref
4748
import warnings
4849

@@ -52,6 +53,10 @@
5253
DEBUG = False
5354

5455

56+
def _indent_str(obj): # textwrap.indent(str(obj), 4) on Py3.
57+
return re.sub("(^|\n)", r"\1 ", str(obj))
58+
59+
5560
class TransformNode(object):
5661
"""
5762
:class:`TransformNode` is the base class for anything that
@@ -1053,8 +1058,15 @@ def __init__(self, bbox, transform, **kwargs):
10531058
self.set_children(bbox, transform)
10541059
self._points = None
10551060

1056-
def __repr__(self):
1057-
return "TransformedBbox(%r, %r)" % (self._bbox, self._transform)
1061+
def __str__(self):
1062+
return ("{}(\n"
1063+
"{},\n"
1064+
"{})"
1065+
.format(type(self).__name__,
1066+
_indent_str(self._bbox),
1067+
_indent_str(self._transform)))
1068+
1069+
__repr__ = __str__
10581070

10591071
def get_points(self):
10601072
if self._invalid:
@@ -1134,8 +1146,15 @@ def __init__(self, bbox, x0=None, y0=None, x1=None, y1=None, **kwargs):
11341146
self._locked_points = np.ma.array(fp, np.float_,
11351147
mask=mask).reshape((2, 2))
11361148

1137-
def __repr__(self):
1138-
return "LockableBbox(%r, %r)" % (self._bbox, self._locked_points)
1149+
def __str__(self):
1150+
return ("{}(\n"
1151+
"{},\n"
1152+
"{})"
1153+
.format(type(self).__name__,
1154+
_indent_str(self._bbox),
1155+
_indent_str(self._locked_points)))
1156+
1157+
__repr__ = __str__
11391158

11401159
def get_points(self):
11411160
if self._invalid:
@@ -1686,8 +1705,13 @@ def __setstate__(self, state):
16861705
self._parents = dict((k, weakref.ref(v)) for (k, v) in
16871706
six.iteritems(state['parents']) if v is not None)
16881707

1689-
def __repr__(self):
1690-
return "TransformWrapper(%r)" % self._child
1708+
def __str__(self):
1709+
return ("{}(\n"
1710+
"{})"
1711+
.format(type(self).__name__,
1712+
_indent_str(self._child)))
1713+
1714+
__repr__ = __str__
16911715

16921716
def frozen(self):
16931717
return self._child.frozen()
@@ -1914,8 +1938,13 @@ def __init__(self, matrix=None, **kwargs):
19141938
self._mtx = matrix
19151939
self._invalid = 0
19161940

1917-
def __repr__(self):
1918-
return "Affine2D(%s)" % repr(self._mtx)
1941+
def __str__(self):
1942+
return ("{}(\n"
1943+
"{})"
1944+
.format(type(self).__name__,
1945+
_indent_str(self._mtx)))
1946+
1947+
__repr__ = __str__
19191948

19201949
@staticmethod
19211950
def from_values(a, b, c, d, e, f):
@@ -2119,8 +2148,11 @@ def frozen(self):
21192148
return self
21202149
frozen.__doc__ = Affine2DBase.frozen.__doc__
21212150

2122-
def __repr__(self):
2123-
return "IdentityTransform()"
2151+
def __str__(self):
2152+
return ("{}()"
2153+
.format(type(self).__name__))
2154+
2155+
__repr__ = __str__
21242156

21252157
def get_matrix(self):
21262158
return self._mtx
@@ -2219,8 +2251,15 @@ def frozen(self):
22192251
return blended_transform_factory(self._x.frozen(), self._y.frozen())
22202252
frozen.__doc__ = Transform.frozen.__doc__
22212253

2222-
def __repr__(self):
2223-
return "BlendedGenericTransform(%s,%s)" % (self._x, self._y)
2254+
def __str__(self):
2255+
return ("{}(\n"
2256+
"{},\n"
2257+
"{})"
2258+
.format(type(self).__name__,
2259+
_indent_str(self._x),
2260+
_indent_str(self._y)))
2261+
2262+
__repr__ = __str__
22242263

22252264
def transform_non_affine(self, points):
22262265
if self._x.is_affine and self._y.is_affine:
@@ -2324,8 +2363,15 @@ def contains_branch_seperately(self, transform):
23242363
# Note, this is an exact copy of BlendedTransform.contains_branch_seperately
23252364
return self._x.contains_branch(transform), self._y.contains_branch(transform)
23262365

2327-
def __repr__(self):
2328-
return "BlendedAffine2D(%s,%s)" % (self._x, self._y)
2366+
def __str__(self):
2367+
return ("{}(\n"
2368+
"{},\n"
2369+
"{})"
2370+
.format(type(self).__name__,
2371+
_indent_str(self._x),
2372+
_indent_str(self._y)))
2373+
2374+
__repr__ = __str__
23292375

23302376
def get_matrix(self):
23312377
if self._invalid:
@@ -2439,12 +2485,15 @@ def _get_is_separable(self):
24392485
return self._a.is_separable and self._b.is_separable
24402486
is_separable = property(_get_is_separable)
24412487

2442-
if DEBUG:
2443-
def __str__(self):
2444-
return '(%s, %s)' % (self._a, self._b)
2488+
def __str__(self):
2489+
return ("{}(\n"
2490+
"{},\n"
2491+
"{})"
2492+
.format(type(self).__name__,
2493+
_indent_str(self._a),
2494+
_indent_str(self._b)))
24452495

2446-
def __repr__(self):
2447-
return "CompositeGenericTransform(%r, %r)" % (self._a, self._b)
2496+
__repr__ = __str__
24482497

24492498
def transform_affine(self, points):
24502499
return self.get_affine().transform(points)
@@ -2535,8 +2584,15 @@ def _iter_break_from_left_to_right(self):
25352584
for lh_compliment, rh_compliment in self._b._iter_break_from_left_to_right():
25362585
yield self._a + lh_compliment, rh_compliment
25372586

2538-
def __repr__(self):
2539-
return "CompositeAffine2D(%r, %r)" % (self._a, self._b)
2587+
def __str__(self):
2588+
return ("{}(\n"
2589+
"{},\n"
2590+
"{})"
2591+
.format(type(self).__name__,
2592+
_indent_str(self._a),
2593+
_indent_str(self._b)))
2594+
2595+
__repr__ = __str__
25402596

25412597
def get_matrix(self):
25422598
if self._invalid:
@@ -2599,8 +2655,15 @@ def __init__(self, boxin, boxout, **kwargs):
25992655
self._mtx = None
26002656
self._inverted = None
26012657

2602-
def __repr__(self):
2603-
return "BboxTransform(%r, %r)" % (self._boxin, self._boxout)
2658+
def __str__(self):
2659+
return ("{}(\n"
2660+
"{},\n"
2661+
"{})"
2662+
.format(type(self).__name__,
2663+
_indent_str(self._boxin),
2664+
_indent_str(self._boxout)))
2665+
2666+
__repr__ = __str__
26042667

26052668
def get_matrix(self):
26062669
if self._invalid:
@@ -2642,8 +2705,13 @@ def __init__(self, boxout, **kwargs):
26422705
self._mtx = None
26432706
self._inverted = None
26442707

2645-
def __repr__(self):
2646-
return "BboxTransformTo(%r)" % (self._boxout)
2708+
def __str__(self):
2709+
return ("{}(\n"
2710+
"{})"
2711+
.format(type(self).__name__,
2712+
_indent_str(self._boxout)))
2713+
2714+
__repr__ = __str__
26472715

26482716
def get_matrix(self):
26492717
if self._invalid:
@@ -2666,9 +2734,6 @@ class BboxTransformToMaxOnly(BboxTransformTo):
26662734
transforms points from the unit bounding box to a given
26672735
:class:`Bbox` with a fixed upper left of (0, 0).
26682736
"""
2669-
def __repr__(self):
2670-
return "BboxTransformToMaxOnly(%r)" % (self._boxout)
2671-
26722737
def get_matrix(self):
26732738
if self._invalid:
26742739
xmax, ymax = self._boxout.max
@@ -2701,8 +2766,13 @@ def __init__(self, boxin, **kwargs):
27012766
self._mtx = None
27022767
self._inverted = None
27032768

2704-
def __repr__(self):
2705-
return "BboxTransformFrom(%r)" % (self._boxin)
2769+
def __str__(self):
2770+
return ("{}(\n"
2771+
"{})"
2772+
.format(type(self).__name__,
2773+
_indent_str(self._boxin)))
2774+
2775+
__repr__ = __str__
27062776

27072777
def get_matrix(self):
27082778
if self._invalid:
@@ -2734,8 +2804,13 @@ def __init__(self, xt, yt, scale_trans, **kwargs):
27342804
self._mtx = None
27352805
self._inverted = None
27362806

2737-
def __repr__(self):
2738-
return "ScaledTranslation(%r)" % (self._t,)
2807+
def __str__(self):
2808+
return ("{}(\n"
2809+
"{})"
2810+
.format(type(self).__name__,
2811+
_indent_str(self._t)))
2812+
2813+
__repr__ = __str__
27392814

27402815
def get_matrix(self):
27412816
if self._invalid:

0 commit comments

Comments
 (0)
0