Skip to content

dev(hansbug): add clear method into TreeValue #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/api_doc/tree/tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions test/tree/common/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ def test_del_(self):
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')
Expand Down
5 changes: 5 additions & 0 deletions test/tree/tree/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ def test_popitem(self):
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
Expand Down
1 change: 1 addition & 0 deletions treevalue/tree/common/storage.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions treevalue/tree/common/storage.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions treevalue/tree/tree/tree.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions treevalue/tree/tree/tree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down