提交 888f5890 编写于 作者: HansBug's avatar HansBug 😆

dev(hansbug): add keys, values and items for TreeValue class

上级 472c0213
......@@ -7,7 +7,7 @@ FastTreeValue
-------------------
.. autoclass:: treevalue.tree.general.FastTreeValue
:members: _attr_extern, json, clone, __add__, __radd__, __sub__, __rsub__, __mul__, __rmul__, __matmul__, __rmatmul__, __truediv__, __rtruediv__, __floordiv__, __rfloordiv__, __mod__, __rmod__, __pow__, __rpow__, __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __lshift__, __rlshift__, __rshift__, __rrshift__, __pos__, __neg__, __invert__, __getitem__, __setitem__, __delitem__, __call__, __getattribute__, __setattr__, __delattr__, __repr__, __iter__, __hash__, __eq__, map, type, mask, filter, __str__, reduce, rise, union, subside, __getstate__, __setstate__, __iadd__, __isub__, __imul__, __imatmul__, __ifloordiv__, __itruediv__, __ipow__, __imod__, __iand__, __ior__, __ixor__, __ilshift__, __irshift__, graph, graphics, func
:members: _attr_extern, json, clone, __add__, __radd__, __sub__, __rsub__, __mul__, __rmul__, __matmul__, __rmatmul__, __truediv__, __rtruediv__, __floordiv__, __rfloordiv__, __mod__, __rmod__, __pow__, __rpow__, __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __lshift__, __rlshift__, __rshift__, __rrshift__, __pos__, __neg__, __invert__, __getitem__, __setitem__, __delitem__, __call__, __getattribute__, __setattr__, __delattr__, __repr__, __iter__, __hash__, __eq__, map, type, mask, filter, __str__, reduce, rise, union, subside, __getstate__, __setstate__, __iadd__, __isub__, __imul__, __imatmul__, __ifloordiv__, __itruediv__, __ipow__, __imod__, __iand__, __ior__, __ixor__, __ilshift__, __irshift__, graph, graphics, func, keys, values, items
.. _apidoc_tree_general_generaltreevalue:
......
......@@ -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
:members: __init__, __getattribute__, __setattr__, __delattr__, __contains__, __repr__, __iter__, __hash__, __eq__, _attr_extern, __len__, __bool__, __str__, __getstate__, __setstate__, get, keys, values, items
.. _apidoc_tree_tree_delayed:
......
......@@ -204,3 +204,34 @@ class TestTreeTreeTree:
with pytest.raises(KeyError):
_ = tv1.get('e')
assert tv1.get('e', 233) == 233
def test_keys(self):
tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
assert set(tv1.keys()) == {'a', 'b', 'c', 'd'}
def test_values(self):
tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}})
assert set(tv1.c.values()) == {2, 3}
assert len(list(tv1.values())) == 3
assert 1 in tv1.values()
assert 2 in tv1.values()
def test_items(self):
tv1 = TreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
assert sorted(tv1.items()) == [
('a', 1),
('b', 2),
('c', TreeValue({'x': 2, 'y': 3})),
('d', {'x': 2, 'y': 3}),
]
class MyTreeValue(TreeValue):
pass
tv2 = MyTreeValue({'a': 1, 'b': 2, 'c': {'x': 2, 'y': 3}, 'd': raw({'x': 2, 'y': 3})})
assert sorted(tv2.items()) == [
('a', 1),
('b', 2),
('c', MyTreeValue({'x': 2, 'y': 3})),
('d', {'x': 2, 'y': 3}),
]
......@@ -370,6 +370,50 @@ cdef class TreeValue:
"""
return self._st
@cython.binding(True)
def keys(self):
"""
Overview:
Get keys of this treevalue object, like the :class:`dict`.
Returns:
- keys: A generator of all the keys.
"""
return self._st.keys()
@cython.binding(True)
def values(self):
"""
Overview:
Get value of this treevalue object, like the :class:`dict`.
Returns:
- values: A generator of all the values
"""
cdef object v
for v in self._st.values():
if isinstance(v, TreeStorage):
yield self._type(v)
else:
yield v
@cython.binding(True)
def items(self):
"""
Overview:
Get pairs of keys and values of this treevalue object, like the :class:`items`.
Returns:
- items: A generator of pairs of keys and values.
"""
cdef str k
cdef object v
for k, v in self._st.items():
if isinstance(v, TreeStorage):
yield k, self._type(v)
else:
yield k, v
cdef str _prefix_fix(object text, object prefix):
cdef list lines = []
cdef int i
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册