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

add str, repr, keys, values, items, contains to DictObject

parent 4d44a154
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,5 @@ ...@@ -2,3 +2,5 @@
* special-cased passing a DictObject to another DictObject * special-cased passing a DictObject to another DictObject
* added str and repr to DictObject * added str and repr to DictObject
* added items, keys and values to DictObject
* added __contains__ to DictObject
\ No newline at end of file
__version__ = '2.8.8_a3' __version__ = '2.8.8_a4'
...@@ -21,6 +21,9 @@ class DictObject(tp.MutableMapping[str, T]): ...@@ -21,6 +21,9 @@ class DictObject(tp.MutableMapping[str, T]):
>>> self.assertEqual(a.test, 5) >>> self.assertEqual(a.test, 5)
""" """
def __contains__(self, item: str) -> bool:
return item in self.__data
def __str__(self) -> str: def __str__(self) -> str:
return 'DictObject(%s)' % (str(self.__data), ) return 'DictObject(%s)' % (str(self.__data), )
...@@ -117,6 +120,15 @@ class DictObject(tp.MutableMapping[str, T]): ...@@ -117,6 +120,15 @@ class DictObject(tp.MutableMapping[str, T]):
except KeyError: except KeyError:
return v return v
def keys(self):
return self.__data.keys()
def values(self):
return self.__data.values()
def items(self):
return self.__data.items()
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]:
""" """
......
...@@ -27,6 +27,9 @@ class TestMisc(unittest.TestCase): ...@@ -27,6 +27,9 @@ class TestMisc(unittest.TestCase):
self.assertIn('DictObject', str(b)) self.assertIn('DictObject', str(b))
self.assertIn('DictObject', repr(b)) self.assertIn('DictObject', repr(b))
self.assertIn('a', b.keys())
self.assertIn(5, b.values())
self.assertIn(('a', 5), b.items())
def test_dictobject_setdefault(self): def test_dictobject_setdefault(self):
a = DictObject() a = DictObject()
...@@ -35,6 +38,7 @@ class TestMisc(unittest.TestCase): ...@@ -35,6 +38,7 @@ class TestMisc(unittest.TestCase):
self.assertIsNone(a.get('v')) self.assertIsNone(a.get('v'))
self.assertEqual(a.get('k'), 2) self.assertEqual(a.get('k'), 2)
self.assertIn('k', a)
def test_expiration_dict_manual_expiring(self): def test_expiration_dict_manual_expiring(self):
eed = ExpiringEntryDict(expiration_timeout=5) eed = ExpiringEntryDict(expiration_timeout=5)
......
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