Skip to content

Commit f4297b6

Browse files
committed
-
1 parent 94168fa commit f4297b6

File tree

4 files changed

+41
-39
lines changed

4 files changed

+41
-39
lines changed

source_py3/python_toolbox/collection_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _get_all_contained_counters(counter, use_lazy_tuple=True):
3434
counter,
3535
(
3636
collections.Counter,
37-
nifty_collections.bagging._BagMixin
37+
nifty_collections.bagging._MutableBagMixin
3838
)
3939
)
4040
counter_type = type(counter)

source_py3/python_toolbox/cute_testing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,5 @@ def assert_polite_wrapper(wrapper, wrapped=None, same_signature=True):
139139
assert wrapper.__wrapped__ == wrapped
140140

141141

142-
class TestCase(nose.case.Test):
143-
@classmethod
144-
def address(cls):
145-
return '.'.join((cls.__module__, cls.__name__))
142+
class TestCase(unittest2.TestCase):
143+
pass

source_py3/python_toolbox/nifty_collections/bagging.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,18 @@ def __ge__(self, other):
281281
return False
282282
return True
283283

284+
def __repr__(self):
285+
if not self:
286+
return '%s()' % type(self).__name__
287+
return '%s(%s)' % (
288+
type(self).__name__,
289+
'[%s]' % ', '.join('%s' % (item,) for item in self.items())
290+
)
284291

285-
class _BagMixin(_BaseBagMixin):
286-
# blocktodo: add all mutable methods, like __iadd__ and everything
292+
class _MutableBagMixin(_BaseBagMixin):
293+
# blocktodo: ensure all mutable methods, like __iadd__ and everything
287294
pass
288295

289-
290296
class _OrderedBagMixin(Ordered):
291297
def __repr__(self):
292298
if not self:
@@ -345,7 +351,7 @@ def fromkeys(cls, iterable, value=None):
345351

346352

347353

348-
class Bag(_BagMixin, collections.UserDict):
354+
class Bag(_MutableBagMixin, collections.UserDict):
349355
'''
350356
A bag that counts items.
351357
@@ -367,7 +373,7 @@ class Bag(_BagMixin, collections.UserDict):
367373
_dict = property(lambda self: self.data)
368374

369375

370-
class OrderedBag(_OrderedBagMixin, _BagMixin, _OrderedDictDelegator):
376+
class OrderedBag(_OrderedBagMixin, _MutableBagMixin, _OrderedDictDelegator):
371377
'''
372378
An ordered bag that counts items.
373379

source_py3/test_python_toolbox/test_nifty_collections/test_bagging.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import itertools
88
import collections
99
import decimal as decimal_module
10+
from python_toolbox.third_party import unittest2
1011

1112
import nose
1213

@@ -23,12 +24,12 @@
2324
infinity = float('inf')
2425
infinities = (infinity, -infinity)
2526

26-
class BaseBagTestCase(nose.Test): # blocktodo: using cute testing class?1
27-
def test_common(self, bag_type):
27+
class BaseBagTestCase(cute_testing.TestCase):
28+
def test_common(self):
2829
bag = self.bag_type('abracadabra')
2930
assert bag == collections.Counter('abracadabra') == \
3031
collections.Counter(bag) == \
31-
bag_type(collections.Counter('abracadabra'))
32+
self.bag_type(collections.Counter('abracadabra'))
3233

3334
assert len(bag) == 5
3435
assert set(bag) == set(bag.keys()) == set('abracadabra')
@@ -45,27 +46,23 @@ def test_common(self, bag_type):
4546

4647
assert bag + bag == self.bag_type('abracadabra' * 2)
4748
assert bag - bag == self.bag_type()
48-
assert bag - bag_type('a') == self.bag_type('abracadabr')
49-
assert bag - bag_type('a') == self.bag_type('abracadabr')
50-
assert bag | bag_type('a') == bag
49+
assert bag - self.bag_type('a') == self.bag_type('abracadabr')
50+
assert bag - self.bag_type('a') == self.bag_type('abracadabr')
51+
assert bag | self.bag_type('a') == bag
5152
assert bag | bag == bag | bag | bag == bag
52-
assert bag & bag_type('a') == bag_type('a')
53+
assert bag & self.bag_type('a') == self.bag_type('a')
5354
assert bag & bag == \
5455
bag & bag & bag == bag
5556

56-
assert bag_type(bag.elements()) == bag
57+
assert self.bag_type(bag.elements()) == bag
5758

5859
assert +bag == bag
5960
with cute_testing.RaiseAssertor(TypeError):
6061
- bag
6162

6263
assert re.match('^(Frozen)?(Ordered)?Bag\(.*$', repr(bag))
6364

64-
assert bag.copy({'meow': 9}) == \
65-
bag.copy(meow=9) == \
66-
bag_type(OrderedDict(
67-
[('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1), ('meow', 9)])
68-
)
65+
assert bag.copy() == bag
6966

7067
assert pickle.loads(pickle.dumps(bag)) == bag
7168

@@ -93,11 +90,11 @@ def test_comparison(self):
9390
not_a_bag = {}
9491

9592
hierarchy = (
96-
(bag_4, {bag_3, bag_2, bag_1, bag_0}),
97-
(bag_3, {bag_1, bag_0}),
98-
(bag_2, {bag_1, bag_0}),
99-
(bag_1, {bag_0}),
100-
(bag_0, set()),
93+
(bag_4, [bag_3, bag_2, bag_1, bag_0]),
94+
(bag_3, [bag_1, bag_0]),
95+
(bag_2, [bag_1, bag_0]),
96+
(bag_1, [bag_0]),
97+
(bag_0, []),
10198
)
10299

103100
for item, smaller_items in hierarchy:
@@ -110,7 +107,7 @@ def test_comparison(self):
110107
assert item > smaller_item
111108
assert item != smaller_item
112109
not_smaller_items = [item for item in next(zip(*hierarchy)) if
113-
item not in smaller_item]
110+
item not in smaller_items]
114111
for not_smaller_item in not_smaller_items:
115112
assert not item < smaller_item
116113

@@ -155,31 +152,31 @@ def test_only_positive_ints_or_zero(self):
155152
with cute_testing.RaiseAssertor(TypeError):
156153
self.bag_type({'a': ('still', 'nope'),})
157154

158-
def test_ignores_zero(bag_type):
159-
bag_0 = bag_type({'a': 0,})
160-
bag_1 = bag_type()
155+
def test_ignores_zero(self):
156+
bag_0 = self.bag_type({'a': 0,})
157+
bag_1 = self.bag_type()
161158
assert bag_0 == bag_1
162159

163-
if bag_type.is_frozen:
160+
if issubclass(self.bag_type, collections.Hashable):
164161
assert hash(bag_0) == hash(bag_1)
165162
assert {bag_0, bag_1} == {bag_0} == {bag_1}
166163

167164
bag_2 = \
168-
bag_type({'a': 0.0, 'b': 2, 'c': decimal_module.Decimal('0.0'),})
169-
bag_3 = bag_type('bb')
165+
self.bag_type({'a': 0.0, 'b': 2, 'c': decimal_module.Decimal('0.0'),})
166+
bag_3 = self.bag_type('bb')
170167

171-
if bag_type.is_frozen:
168+
if issubclass(self.bag_type, collections.Hashable):
172169
assert hash(bag_2) == hash(bag_3)
173170
assert {bag_2, bag_3} == {bag_2} == {bag_3}
174171

175172

176173

177174

178175
class BaseMutableBagTestCase(BaseBagTestCase):
179-
is_frozen = False
180-
181176
def test_hash(self):
182177
bag = self.bag_type('abracadabra')
178+
assert not isinstance(bag, collections.Hashable)
179+
assert not issubclass(self.bag_type, collections.Hashable)
183180
with cute_testing.RaiseAssertor(TypeError):
184181
{bag}
185182
with cute_testing.RaiseAssertor(TypeError):
@@ -266,10 +263,11 @@ def test_mutating(bag_type):
266263

267264

268265
class BaseFrozenBagTestCase(BaseBagTestCase):
269-
is_frozen = True
270266

271267
def test_hash(self):
272268
bag = self.bag_type('abracadabra')
269+
assert isinstance(bag, collections.Hashable)
270+
assert issubclass(self.bag_type, collections.Hashable)
273271
assert {bag, bag} == {bag}
274272
assert {bag: bag} == {bag: bag}
275273
assert isinstance(hash(bag), int)

0 commit comments

Comments
 (0)