Skip to content

Commit 2dfb60b

Browse files
committed
ENH: add remove method to cbook.Grouper
- also add tests
1 parent 162e25b commit 2dfb60b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/matplotlib/cbook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,14 @@ def joined(self, a, b):
17211721
except KeyError:
17221722
return False
17231723

1724+
def remove(self, a):
1725+
self.clean()
1726+
1727+
mapping = self._mapping
1728+
seta = mapping.pop(ref(a), None)
1729+
if seta is not None:
1730+
seta.remove(ref(a))
1731+
17241732
def __iter__(self):
17251733
"""
17261734
Iterate over each of the disjoint sets as a list.

lib/matplotlib/tests/test_cbook.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
3+
import itertools
34

45
from matplotlib.externals import six
56

@@ -376,3 +377,23 @@ def test_step_fails():
376377
np.arange(12))
377378
assert_raises(ValueError, cbook._step_validation,
378379
np.arange(12), np.arange(3))
380+
381+
382+
def test_grouper():
383+
class dummy():
384+
pass
385+
a, b, c, d, e = objs = [dummy() for j in range(5)]
386+
g = cbook.Grouper()
387+
g.join(*objs)
388+
assert set(list(g)[0]) == set(objs)
389+
assert set(g.get_siblings(a)) == set(objs)
390+
391+
for other in objs[1:]:
392+
assert g.joined(a, other)
393+
394+
g.remove(a)
395+
for other in objs[1:]:
396+
assert not g.joined(a, other)
397+
398+
for A, B in itertools.product(objs[1:], objs[1:]):
399+
assert g.joined(A, B)

0 commit comments

Comments
 (0)