Skip to content
Snippets Groups Projects
Commit df857b5b authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

add setdefault to DictObject, v2.8.7

parent a8b1ab93
No related branches found
No related tags found
No related merge requests found
# v2.8.7 # v2.8.7
* added setdefault to `DictObject` * added setdefault and get to `DictObject`
__version__ = '2.8.7_a2' __version__ = '2.8.7'
...@@ -102,6 +102,12 @@ class DictObject(tp.MutableMapping[str, T]): ...@@ -102,6 +102,12 @@ class DictObject(tp.MutableMapping[str, T]):
else: else:
return True return True
def get(self, k: str, v: tp.Optional[tp.Any] = None) -> tp.Any:
try:
return self.__data[k]
except KeyError:
return v
def apply_dict_object(v: tp.Union[tp.Any, tp.Dict[str, T]]) -> tp.Union[DictObject, tp.Any]: def apply_dict_object(v: tp.Union[tp.Any, tp.Dict[str, T]]) -> tp.Union[DictObject, tp.Any]:
""" """
......
...@@ -22,6 +22,9 @@ class TestMisc(unittest.TestCase): ...@@ -22,6 +22,9 @@ class TestMisc(unittest.TestCase):
self.assertEqual(a.setdefault('k', 2), 2) self.assertEqual(a.setdefault('k', 2), 2)
self.assertEqual(a.k, 2) self.assertEqual(a.k, 2)
self.assertIsNone(a.get('v'))
self.assertEqual(a.get('k'), 2)
def test_expiration_dict_manual_expiring(self): def test_expiration_dict_manual_expiring(self):
eed = ExpiringEntryDict(expiration_timeout=5) eed = ExpiringEntryDict(expiration_timeout=5)
eed['test'] = 2 eed['test'] = 2
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment