[Checkins] SVN: BTrees/branches/pure_python/ weightedIntersection must work even when merging is impossible (e.g. two sets).
Tres Seaver
cvs-admin at zope.org
Tue Dec 4 20:46:35 UTC 2012
Log message for revision 128513:
weightedIntersection must work even when merging is impossible (e.g. two sets).
Changed:
_U BTrees/branches/pure_python/
U BTrees/branches/pure_python/BTrees/_base.py
U BTrees/branches/pure_python/BTrees/tests/test__base.py
-=-
Modified: BTrees/branches/pure_python/BTrees/_base.py
===================================================================
--- BTrees/branches/pure_python/BTrees/_base.py 2012-12-04 20:17:56 UTC (rev 128512)
+++ BTrees/branches/pure_python/BTrees/_base.py 2012-12-04 20:46:34 UTC (rev 128513)
@@ -1286,7 +1286,6 @@
MERGE_DEFAULT = getattr(o1, 'MERGE_DEFAULT', None)
i1 = _SetIteration(o1, True, MERGE_DEFAULT)
i2 = _SetIteration(o2, True, MERGE_DEFAULT)
- result = o1._mapping_type()
MERGE = getattr(o1, 'MERGE', None)
if MERGE is None and i1.useValues and i2.useValues:
raise TypeError("invalid set operation")
@@ -1300,16 +1299,24 @@
raise TypeError("invalid set operation")
else:
raise TypeError("invalid set operation")
- def copy(i, w):
- result._keys.append(i.key)
- result._values.append(MERGE_WEIGHT(i.value, w))
+ _merging = i1.useValues or i2.useValues
+ if _merging:
+ result = o1._mapping_type()
+ def copy(i, w):
+ result._keys.append(i.key)
+ result._values.append(MERGE_WEIGHT(i.value, w))
+ else:
+ result = o1._set_type()
+ def copy(i, w):
+ result._keys.append(i.key)
while i1.active and i2.active:
cmp_ = cmp(i1.key, i2.key)
if cmp_ < 0:
i1.advance()
elif cmp_ == 0:
result._keys.append(i1.key)
- result._values.append(MERGE(i1.value, w1, i2.value, w2))
+ if _merging:
+ result._values.append(MERGE(i1.value, w1, i2.value, w2))
i1.advance()
i2.advance()
else:
Modified: BTrees/branches/pure_python/BTrees/tests/test__base.py
===================================================================
--- BTrees/branches/pure_python/BTrees/tests/test__base.py 2012-12-04 20:17:56 UTC (rev 128512)
+++ BTrees/branches/pure_python/BTrees/tests/test__base.py 2012-12-04 20:46:34 UTC (rev 128513)
@@ -2653,7 +2653,15 @@
self.assertEqual(result['d'], 33)
self.assertEqual(result['e'], 11)
+ def test_w_lhs_Set_rhs_Set(self):
+ from BTrees.IIBTree import IISetPy
+ lhs = IISetPy([1, 2, 3])
+ rhs = IISetPy([1, 4])
+ weight, result = self._callFUT(lhs.__class__, lhs, rhs)
+ self.assertEqual(weight, 1)
+ self.assertEqual(list(result), [1, 2, 3, 4])
+
class Test_weightedIntersection(unittest.TestCase, _SetObBase):
def _callFUT(self, *args, **kw):
@@ -2695,7 +2703,15 @@
self.assertEqual(list(result), ['a'])
self.assertEqual(result['a'], 23)
+ def test_w_lhs_Set_rhs_Set(self):
+ from BTrees.IIBTree import IISetPy
+ lhs = IISetPy([1, 2, 3])
+ rhs = IISetPy([1, 4])
+ weight, result = self._callFUT(lhs.__class__, lhs, rhs)
+ self.assertEqual(weight, 2)
+ self.assertEqual(list(result), [1])
+
class Test_multiunion(unittest.TestCase, _SetObBase):
def _callFUT(self, *args, **kw):
More information about the checkins
mailing list