Skip to content

Commit

Permalink
Add an ancestors and descendants methods on node, and fix manifest.in
Browse files Browse the repository at this point in the history
  • Loading branch information
k1897038 committed Nov 29, 2018
1 parent 7fb5e35 commit b994f4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion MANIFEST.in
@@ -1,2 +1,2 @@
include README.md
include data/*
include icd9cms/data/*
24 changes: 24 additions & 0 deletions icd9cms/icd9.py
Expand Up @@ -55,6 +55,30 @@ def collect_leaf_nodes(node: Node) -> Iterable:

return collect_leaf_nodes(self)

def ancestors(self, depth=None) -> List:
def accu_ancestors(node, ancestors, curr_depth, max_depth) -> List:
if node is None or \
(max_depth is not None and curr_depth >= max_depth):
return ancestors
ancestors.append(node.code)
return accu_ancestors(node.parent, ancestors, curr_depth+1, max_depth)

if self.parent is None:
return []
return accu_ancestors(self.parent, [], 0, depth)

def descendants(self, depth=None):
def acc_descendants(nodes, descendants, curr_depth, max_depth) -> List:
if max_depth is not None and curr_depth >= max_depth:
return descendants
if nodes is not None:
descendants.extend(n.code for n in nodes)
for node in nodes:
acc_descendants(node.children, descendants, curr_depth+1, max_depth)
return descendants

return acc_descendants(self.children, [], 0, depth)


def _strip_elements(ul: List[BSoup]) -> List[Node]:
"""Strip non-leaf pages"""
Expand Down

0 comments on commit b994f4d

Please sign in to comment.