Skip to content

Commit 291952c

Browse files
committed
-
1 parent e647493 commit 291952c

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

source_py3/python_toolbox/nifty_collections/bagging.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -338,53 +338,40 @@ def __lt__(self, other):
338338
if not isinstance(other, _BaseBagMixin):
339339
return NotImplemented
340340
found_strict_difference = False # Until challenged.
341-
for element, count in self.items():
342-
try:
343-
other_count = other[element]
344-
except KeyError:
345-
return False
346-
if not (count <= other_count):
341+
all_elements = set(other) | set(self)
342+
for element in all_elements:
343+
if self[element] > other[element]:
347344
return False
348-
elif count < other_count:
345+
elif self[element] < other[element]:
349346
found_strict_difference = True
350347
return found_strict_difference
351348

352349
def __le__(self, other):
353350
if not isinstance(other, _BaseBagMixin):
354351
return NotImplemented
355352
for element, count in self.items():
356-
try:
357-
other_count = other[element]
358-
except KeyError:
359-
return False
360-
if not (count <= other_count):
353+
if count > other[element]:
361354
return False
362355
return True
363356

364357
def __gt__(self, other):
365358
if not isinstance(other, _BaseBagMixin):
366359
return NotImplemented
367360
found_strict_difference = False # Until challenged.
368-
for element, count in self.items():
369-
try:
370-
other_count = other[element]
371-
except KeyError:
372-
continue
373-
if not (count >= other_count):
361+
all_elements = set(other) | set(self)
362+
for element in all_elements:
363+
if self[element] < other[element]:
374364
return False
375-
elif count > other_count:
365+
elif self[element] > other[element]:
376366
found_strict_difference = True
377367
return found_strict_difference
378368

379369
def __ge__(self, other):
380370
if not isinstance(other, _BaseBagMixin):
381371
return NotImplemented
382-
for element, count in self.items():
383-
try:
384-
other_count = other[element]
385-
except KeyError:
386-
continue
387-
if not (count >= other_count):
372+
all_elements = set(other) | set(self)
373+
for element in all_elements:
374+
if self[element] < other[element]:
388375
return False
389376
return True
390377

source_py3/test_python_toolbox/test_nifty_collections/test_bagging.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def test_repr(self):
128128
assert eval(repr(bag)) == bag
129129
assert re.match(self._repr_result_pattern, repr(bag))
130130

131+
empty_bag = self.bag_type()
132+
assert eval(repr(empty_bag)) == empty_bag
133+
assert repr(empty_bag) == '%s()' % self.bag_type.__name__
134+
131135

132136
def test_no_subtract(self):
133137
# It's a silly method, yo.
@@ -159,6 +163,11 @@ def test_comparison(self):
159163
assert item >= smaller_item
160164
assert item > smaller_item
161165
assert item != smaller_item
166+
assert smaller_item <= item
167+
assert smaller_item < item
168+
assert not smaller_item >= item
169+
assert not smaller_item > item
170+
assert smaller_item != item
162171
not_smaller_items = [item for item in next(zip(*hierarchy)) if
163172
item not in smaller_items]
164173
for not_smaller_item in not_smaller_items:

0 commit comments

Comments
 (0)