Skip to content

Commit e5473a4

Browse files
committed
-
1 parent a612448 commit e5473a4

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

source_py3/python_toolbox/nifty_collections/bagging.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,11 @@ def popitem(self):
719719
Pop an item from this bag, returning `(key, count)` and removing it.
720720
'''
721721
return self._dict.popitem()
722-
723-
722+
723+
def get_frozen(self):
724+
'''Get a frozen version of this bag.'''
725+
return self._frozen_type(self)
726+
724727

725728
class _OrderedBagMixin(Ordered):
726729
'''
@@ -785,6 +788,9 @@ def frozen_bag_bag(self):
785788
from .frozen_bag_bag import FrozenBagBag
786789
return FrozenBagBag(self.values())
787790

791+
def get_mutable(self):
792+
'''Get a mutable version of this bag.'''
793+
return self._mutable_type(self)
788794

789795

790796
class _BaseDictDelegator(collections.MutableMapping):
@@ -890,7 +896,9 @@ class Bag(_MutableBagMixin, _DictDelegator):
890896
objects. This means we do not allow arbitrary values for counts like
891897
`collections.Counter` and we don't have to deal with all the complications
892898
that follow. Only positive integers are allowed as counts.
893-
'''
899+
'''
900+
901+
894902

895903

896904
class OrderedBag(_OrderedBagMixin, _MutableBagMixin, _OrderedDictDelegator):
@@ -935,7 +943,7 @@ def popitem(self, last=True):
935943
'._dict.sort',
936944
doc='Sort the keys in this bag. (With optional `key` function.)'
937945
)
938-
946+
939947

940948

941949
class FrozenBag(_BaseBagMixin, _FrozenBagMixin, FrozenDict):
@@ -1000,4 +1008,9 @@ class FrozenOrderedBag(_OrderedBagMixin, _FrozenBagMixin, _BaseBagMixin,
10001008
'''
10011009
def __hash__(self):
10021010
return hash((type(self), tuple(self.items())))
1003-
1011+
1012+
1013+
Bag._frozen_type = FrozenBag
1014+
OrderedBag._frozen_type = FrozenOrderedBag
1015+
FrozenBag._mutable_type = Bag
1016+
FrozenOrderedBag._mutable_type = OrderedBag

source_py3/test_python_toolbox/test_nifty_collections/test_bagging.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,24 @@ def test_operations(self):
367367

368368

369369
class BaseMutableBagTestCase(BaseBagTestCase):
370+
371+
def test_get_mutable(self):
372+
bag = self.bag_type('abracadabra')
373+
assert not hasattr(bag, 'get_mutable')
374+
with cute_testing.RaiseAssertor(AttributeError):
375+
bag.get_mutable()
376+
377+
def test_get_frozen(self):
378+
bag = self.bag_type('abracadabra')
379+
frozen_bag = bag.get_frozen()
380+
assert isinstance(frozen_bag, collections.Hashable)
381+
if isinstance(bag, nifty_collections.Ordered):
382+
assert tuple(bag.items()) == tuple(frozen_bag.items())
383+
else:
384+
assert set(bag.items()) == set(frozen_bag.items())
385+
assert type(frozen_bag).__name__ == 'Frozen%s' % type(bag).__name__
386+
assert frozen_bag.get_mutable() == bag
387+
370388
def test_hash(self):
371389
bag = self.bag_type('abracadabra')
372390
assert not isinstance(bag, collections.Hashable)
@@ -522,6 +540,25 @@ def test_clear(self):
522540

523541
class BaseFrozenBagTestCase(BaseBagTestCase):
524542

543+
def test_get_mutable(self):
544+
bag = self.bag_type('abracadabra')
545+
mutable_bag = bag.get_mutable()
546+
assert not isinstance(mutable_bag, collections.Hashable)
547+
if isinstance(bag, nifty_collections.Ordered):
548+
assert tuple(bag.items()) == tuple(mutable_bag.items())
549+
else:
550+
assert set(bag.items()) == set(mutable_bag.items())
551+
assert type(bag).__name__ == 'Frozen%s' % type(mutable_bag).__name__
552+
assert mutable_bag.get_frozen() == bag
553+
554+
555+
def test_get_frozen(self):
556+
bag = self.bag_type('abracadabra')
557+
assert not hasattr(bag, 'get_frozen')
558+
with cute_testing.RaiseAssertor(AttributeError):
559+
bag.get_frozen()
560+
561+
525562
def test_hash(self):
526563
bag = self.bag_type('abracadabra')
527564
assert isinstance(bag, collections.Hashable)

0 commit comments

Comments
 (0)