未验证 提交 bd4d17f5 编写于 作者: HansBug's avatar HansBug 😆 提交者: GitHub

Merge pull request #53 from opendilab/dev/mutablemapping

dev(hansbug): add clear method into TreeValue
......@@ -9,7 +9,7 @@ TreeValue
---------------
.. autoclass:: TreeValue
:members: __init__, __getattribute__, __setattr__, __delattr__, __contains__, __repr__, __iter__, __hash__, __eq__, _attr_extern, __len__, __bool__, __str__, __getstate__, __setstate__, get, pop, keys, values, items, __getitem__, __setitem__, __delitem__, _getitem_extern, _setitem_extern, _delitem_extern, popitem
:members: __init__, __getattribute__, __setattr__, __delattr__, __contains__, __repr__, __iter__, __hash__, __eq__, _attr_extern, __len__, __bool__, __str__, __getstate__, __setstate__, get, pop, keys, values, items, __getitem__, __setitem__, __delitem__, _getitem_extern, _setitem_extern, _delitem_extern, popitem, clear
.. _apidoc_tree_tree_delayed:
......
......@@ -273,6 +273,19 @@ class TestTreeStorage:
with pytest.raises(KeyError):
t.del_('fff')
def test_clear(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
t.clear()
assert t == create_storage({})
assert t.size() == 0
d1 = delayed_partial(lambda: 1)
d2 = delayed_partial(lambda x: x + 1, d1)
t1 = create_storage({'a': d1, 'b': d2, 'c': d1, 'd': 100})
t1.clear()
assert t1 == create_storage({})
assert t1.size() == 0
def test_contains(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
assert t.contains('a')
......
......@@ -355,6 +355,11 @@ class TestTreeTreeTree:
with pytest.raises(KeyError):
tv2.popitem()
def test_clear(self):
tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
assert tv1.clear() is None
assert not tv1
def test_keys(self):
tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
assert len(tv1.keys()) == 4
......
......@@ -16,6 +16,7 @@ cdef class TreeStorage:
cpdef public object pop_or_default(self, str key, object default)
cpdef public tuple popitem(self)
cpdef public void del_(self, str key) except *
cpdef public void clear(self)
cpdef public boolean contains(self, str key)
cpdef public uint size(self)
cpdef public boolean empty(self)
......
......@@ -67,6 +67,9 @@ cdef class TreeStorage:
except KeyError:
raise KeyError(f"Key {repr(key)} not found in this tree.")
cpdef public void clear(self):
self.map.clear()
cpdef public boolean contains(self, str key):
return key in self.map
......
......@@ -23,6 +23,7 @@ cdef class TreeValue:
cpdef get(self, str key, object default= *)
cpdef pop(self, str key, object default= *)
cpdef popitem(self)
cpdef void clear(self)
cpdef treevalue_keys keys(self)
cpdef treevalue_values values(self)
......
......@@ -161,6 +161,14 @@ cdef class TreeValue:
except KeyError:
raise KeyError(f'popitem(): {self._type.__name__} is empty.')
@cython.binding(True)
cpdef void clear(self):
"""
Overview:
Clear all the items in this treevalue object.
"""
self._st.clear()
@cython.binding(True)
cpdef _attr_extern(self, str key):
r"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册