Unlike set.union(), which returns a new set, DictOfSets.union() modified
the DictOfSets in-place. The name collision bit me when I changed some
code from using DictOfSets to a normal set, and expected that
set.union() would modify the set in-place. Since there was only one user
of DictOfSets.union, I figured it was safer to just get rid of it.
ds.discard(2, "c")
self.failIf(2 in ds)
- ds.union(1, ["a", "e"])
- ds.union(3, ["f"])
- self.failUnlessEqual(ds[1], set(["a","e"]))
- self.failUnlessEqual(ds[3], set(["f"]))
+ ds.add(3, "f")
ds2 = dictutil.DictOfSets()
ds2.add(3, "f")
ds2.add(3, "g")
ds2.add(4, "h")
ds.update(ds2)
- self.failUnlessEqual(ds[1], set(["a","e"]))
+ self.failUnlessEqual(ds[1], set(["a"]))
self.failUnlessEqual(ds[3], set(["f", "g"]))
self.failUnlessEqual(ds[4], set(["h"]))
else:
self[key] = set([value])
- def union(self, key, values):
- if key in self:
- self[key].update(values)
- else:
- self[key] = set(values)
-
def update(self, otherdictofsets):
- for key, value in otherdictofsets.iteritems():
- self.union(key, value)
+ for key, values in otherdictofsets.iteritems():
+ if key in self:
+ self[key].update(values)
+ else:
+ self[key] = set(values)
def discard(self, key, value):
if not key in self: