Skip to content
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

New Tree.children_itr method #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def test_children(self):
children = self.tree.children(nid)
for child in children:
self.assertEqual(child in self.tree.all_nodes(), True)
children_from_itr = list(self.tree.children_itr(nid))
self.assertEqual(children, children_from_itr)
try:
self.tree.is_branch("alien")
except NodeIDAbsentError:
Expand Down
7 changes: 7 additions & 0 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ def children(self, nid):
"""
return [self[i] for i in self.is_branch(nid)]
Copy link

@crabhi crabhi Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High, just a quick note - this method could now be implemented equivalently as list(self.children_itr(nid)). That might address @caesar0301's comment re. redundancy.


def children_itr(self, nid):
"""
Return the children (Node) of nid as an iterator.
Empty iterator is returned if nid does not exist
"""
return (self[i] for i in self.is_branch(nid))

def contains(self, nid):
"""Check if the tree contains node of given id"""
return True if nid in self._nodes else False
Expand Down