Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Better repr for Bboxes. #3419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from nose.tools import assert_equal, assert_raises
import numpy.testing as np_test
from numpy.testing import assert_almost_equal
from numpy.testing import assert_almost_equal, assert_array_equal
from matplotlib.transforms import Affine2D, BlendedGenericTransform
from matplotlib.path import Path
from matplotlib.scale import LogScale
Expand Down Expand Up @@ -450,14 +450,14 @@ def test_line_extents_for_non_affine_transData(self):
expeted_data_lim)


def assert_bbox_eq(bbox1, bbox2):
assert_array_equal(bbox1.bounds, bbox2.bounds)


def test_bbox_intersection():
bbox_from_ext = mtrans.Bbox.from_extents
inter = mtrans.Bbox.intersection

from numpy.testing import assert_array_equal as assert_a_equal
def assert_bbox_eq(bbox1, bbox2):
assert_a_equal(bbox1.bounds, bbox2.bounds)

r1 = bbox_from_ext(0, 0, 1, 1)
r2 = bbox_from_ext(0.5, 0.5, 1.5, 1.5)
r3 = bbox_from_ext(0.5, 0, 0.75, 0.75)
Expand All @@ -476,6 +476,18 @@ def assert_bbox_eq(bbox1, bbox2):
assert_bbox_eq(inter(r1, r5), bbox_from_ext(1, 1, 1, 1))


def test_bbox_as_strings():
b = mtrans.Bbox([[.5, 0], [.75, .75]])
assert_bbox_eq(b, eval(repr(b), {'Bbox': mtrans.Bbox}))
asdict = eval(str(b), {'Bbox': dict})
for k, v in asdict.items():
assert_equal(getattr(b, k), v)
fmt = '.1f'
asdict = eval(format(b, fmt), {'Bbox': dict})
for k, v in asdict.items():
assert_equal(eval(format(getattr(b, k), fmt)), v)


if __name__=='__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
10 changes: 9 additions & 1 deletion lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,16 @@ def from_extents(*args):
points = np.array(args, dtype=np.float_).reshape(2, 2)
return Bbox(points)

def __format__(self, fmt):
return (
'Bbox(x0={0.x0:{1}}, y0={0.y0:{1}}, x1={0.x1:{1}}, y1={0.y1:{1}})'.
format(self, fmt))

def __str__(self):
return format(self, '')

def __repr__(self):
return 'Bbox(%r)' % repr(self._points)
return 'Bbox([[{0.x0}, {0.y0}], [{0.x1}, {0.y1}]])'.format(self)

def ignore(self, value):
"""
Expand Down