def make_comment_tree(link): tree = {} def _add_comment(comment, parent): tree[comment.id] = [child.id for child in comment.children] for child in comment.children: _add_comment(child, parent=comment) tree[None] = [comment.id for comment in TREE] for comment in TREE: _add_comment(comment, parent=None) cids, depth, parents = get_tree_details(tree) num_children = calc_num_children(tree) num_children = defaultdict(int, num_children) return CommentTree(link, cids, tree, depth, parents, num_children)
def make_comment_tree(link): cids = [] depth = {} tree = {} parents = {} def _add_comment(comment, parent): cids.append(comment.id) depth[comment.id] = 0 if parent is None else depth[parent.id] + 1 tree[comment.id] = [child.id for child in comment.children] parents[comment.id] = None if parent is None else parent.id for child in comment.children: _add_comment(child, parent=comment) tree[None] = [comment.id for comment in TREE] for comment in TREE: _add_comment(comment, parent=None) return CommentTree(link, cids, tree, depth, parents)