Skip to content

Commit b46d740

Browse files
committed
-
1 parent 7513315 commit b46d740

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

source_py3/python_toolbox/nifty_collections/bagging.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def __add__(self, other):
239239
return NotImplemented
240240
return type(self)(self._dict_type(
241241
(key, self[key] + other[key])
242-
for key in OrderedSet(self) & OrderedSet(other))
242+
for key in OrderedSet(self) | OrderedSet(other))
243243
)
244244

245245
def __sub__(self, other):
@@ -258,6 +258,9 @@ def __mul__(self, other):
258258
return NotImplemented
259259
return type(self)(self._dict_type((key, count * other) for
260260
key, count in self.items()))
261+
262+
__rmul__ = lambda self, other: self * other
263+
261264
def __floordiv__(self, other):
262265
if math_tools.is_integer(other):
263266
return (

source_py3/test_python_toolbox/test_nifty_collections/test_bagging.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ def test_operations_with_foreign_operands(self):
264264
with cute_testing.RaiseAssertor(TypeError): 'foo' * bag
265265
with cute_testing.RaiseAssertor(TypeError): bag / 'foo'
266266
with cute_testing.RaiseAssertor(TypeError): 'foo' / bag
267+
with cute_testing.RaiseAssertor(TypeError): bag / 3
268+
with cute_testing.RaiseAssertor(TypeError): 3 / bag
267269
with cute_testing.RaiseAssertor(TypeError): bag // 'foo'
268270
with cute_testing.RaiseAssertor(TypeError): 'foo' // bag
269271
with cute_testing.RaiseAssertor(TypeError): bag % 'foo'
@@ -287,6 +289,7 @@ def test_operations(self):
287289
bag_0 = self.bag_type('abbccc')
288290
bag_1 = self.bag_type('bcc')
289291
bag_2 = self.bag_type('cddddd')
292+
290293
assert bag_0 + bag_1 == self.bag_type('abbccc' + 'bcc')
291294
assert bag_1 + bag_0 == self.bag_type('bcc' + 'abbccc')
292295
assert bag_0 + bag_2 == self.bag_type('abbccc' + 'cddddd')
@@ -301,6 +304,17 @@ def test_operations(self):
301304
assert bag_1 - bag_2 == self.bag_type('bc')
302305
assert bag_2 - bag_1 == self.bag_type('ddddd')
303306

307+
assert bag_0 * 2 == self.bag_type('abbccc' * 2)
308+
assert bag_1 * 2 == self.bag_type('bcc' * 2)
309+
assert bag_2 * 2 == self.bag_type('cddddd' * 2)
310+
assert 3 * bag_0 == self.bag_type('abbccc' * 3)
311+
assert 3 * bag_1 == self.bag_type('bcc' * 3)
312+
assert 3 * bag_2 == self.bag_type('cddddd' * 3)
313+
314+
assert bag_0 // 2 == self.bag_type('bc')
315+
assert bag_1 // 2 == self.bag_type('c')
316+
assert bag_2 // 2 == self.bag_type('dd')
317+
304318
# blocktodo: continue for all operations
305319

306320

0 commit comments

Comments
 (0)