Add an .extend method for ODicts

This commit is contained in:
Aldo Cortesi 2015-04-14 13:50:57 +12:00
parent 6db5e0a4a1
commit d739882bf2
2 changed files with 12 additions and 1 deletions

View File

@ -108,6 +108,12 @@ class ODict(object):
lst = copy.deepcopy(self.lst) lst = copy.deepcopy(self.lst)
return self.__class__(lst) return self.__class__(lst)
def extend(self, other):
"""
Add the contents of other, preserving any duplicates.
"""
self.lst.extend(other.lst)
def __repr__(self): def __repr__(self):
elements = [] elements = []
for itm in self.lst: for itm in self.lst:

View File

@ -109,6 +109,12 @@ class TestODict:
assert self.od.get_first("one") == "two" assert self.od.get_first("one") == "two"
assert self.od.get_first("two") == None assert self.od.get_first("two") == None
def test_extend(self):
a = odict.ODict([["a", "b"], ["c", "d"]])
b = odict.ODict([["a", "b"], ["e", "f"]])
a.extend(b)
assert len(a) == 4
assert a["a"] == ["b", "b"]
class TestODictCaseless: class TestODictCaseless:
def setUp(self): def setUp(self):
@ -144,4 +150,3 @@ class TestODictCaseless:
assert self.od.keys() == ["foo"] assert self.od.keys() == ["foo"]
self.od.add("bar", 2) self.od.add("bar", 2)
assert len(self.od.keys()) == 2 assert len(self.od.keys()) == 2