From 877a3e206263edbd8a973689b08f8c004de0225f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 18 Aug 2012 18:14:13 +1200 Subject: [PATCH] Add a get_first convenience function to ODict. --- netlib/odict.py | 6 ++++++ test/test_odict.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/netlib/odict.py b/netlib/odict.py index afc33caa6..629fcade0 100644 --- a/netlib/odict.py +++ b/netlib/odict.py @@ -80,6 +80,12 @@ class ODict: else: return d + def get_first(self, k, d=None): + if k in self: + return self[k][0] + else: + return d + def items(self): return self.lst[:] diff --git a/test/test_odict.py b/test/test_odict.py index e7453e2dd..f27f6f8bf 100644 --- a/test/test_odict.py +++ b/test/test_odict.py @@ -85,6 +85,12 @@ class TestODict: assert self.od.get("one") == ["two"] 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: def setUp(self):