Skip to content
New issue

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

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

Already on GitHub? # to your account

dev(hansbug): fix get and pop method #49

Merged
merged 3 commits into from
Jun 26, 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
3 changes: 1 addition & 2 deletions test/tree/tree/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ def test_get(self):
assert tv1.get('b') == 2
assert tv1.get('c') == TreeValue({'x': 2, 'y': 3})
assert tv1.get('d') == {'x': 2, 'y': 3}
with pytest.raises(KeyError):
_ = tv1.get('e')
assert tv1.get('e') is None
assert tv1.get('e', 233) == 233

tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
Expand Down
35 changes: 15 additions & 20 deletions treevalue/tree/tree/tree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -102,40 +102,35 @@ cdef class TreeValue:
return obj

@cython.binding(True)
cpdef get(self, str key, object default=_GET_NO_DEFAULT):
cpdef get(self, str key, object default=None):
r"""
Overview:
Get item from the tree node.

Arguments:
- key (:obj:`str`): Item's name.
- default (:obj:`default`): Default value when this item is not found, default is \
`_GET_NO_DEFAULT` which means just raise `KeyError` when not found.
:param key: Item's name.
:param default: Default value when this item is not found, default is ``None``.
:return: Item's value.

Returns:
- value: Item's value.
.. note::
The method :meth:`get` will never raise ``KeyError``, like the behaviour in \
`dict.get <https://docs.python.org/3/library/stdtypes.html#dict.get>`_.
"""
cdef object value
if default is _GET_NO_DEFAULT:
value = self._st.get(key)
else:
value = self._st.get_or_default(key, default)

return self._unraw(value)
return self._unraw(self._st.get_or_default(key, default))

@cython.binding(True)
cpdef pop(self, str key, object default=_GET_NO_DEFAULT):
"""
Overview:
Pop item from the tree node.

Arguments:
- key (:obj:`str`): Item's name.
- default (:obj:`default`): Default value when this item is not found, default is \
`_GET_NO_DEFAULT` which means just raise `KeyError` when not found.
:param key: Item's name.
:param default: Default value when this item is not found, default is ``_GET_NO_DEFAULT`` which means \
just raise ``KeyError`` when not found.
:return: Item's value.

Returns:
- value: Item's value.
.. note::
The method :meth:`pop` will raise ``KeyError`` when ``key`` is not found, like the behaviour in \
`dict.pop <https://docs.python.org/3/library/stdtypes.html#dict.pop>`_.
"""
cdef object value
if default is _GET_NO_DEFAULT:
Expand Down