Add a get_first convenience function to ODict.

This commit is contained in:
Aldo Cortesi 2012-08-18 18:14:13 +12:00
parent 1c21a28e64
commit 877a3e2062
2 changed files with 12 additions and 0 deletions

View File

@ -80,6 +80,12 @@ class ODict:
else: else:
return d return d
def get_first(self, k, d=None):
if k in self:
return self[k][0]
else:
return d
def items(self): def items(self):
return self.lst[:] return self.lst[:]

View File

@ -85,6 +85,12 @@ class TestODict:
assert self.od.get("one") == ["two"] assert self.od.get("one") == ["two"]
assert self.od.get("two") == None assert self.od.get("two") == None
def test_get_first(self):
self.od.add("one", "two")
self.od.add("one", "three")
assert self.od.get_first("one") == "two"
assert self.od.get_first("two") == None
class TestODictCaseless: class TestODictCaseless:
def setUp(self): def setUp(self):