def annotate_tree_properties(comments):
    """
    iterate through nodes and adds some magic properties to each of them
    representing opening list of children and closing it
    """
    if not comments:
        return

    it = iter(comments)

    # get the first item, this will fail if no items !
    old = next(it)

    # first item starts a new thread
    old.open = True
    last = set()
    for c in it:
        # if this comment has a parent, store its last child for future reference
        if old.last_child_id:
            last.add(old.last_child_id)

        # this is the last child, mark it
        if c.pk in last:
            c.last = True

        # increase the depth
        if c.depth > old.depth:
            c.open = True

        else:  # c.depth <= old.depth
            # close some depths
            old.close = list(range(old.depth - c.depth))

            # new thread
            if old.root_id != c.root_id:
                # close even the top depth
                old.close.append(len(old.close))
                # and start a new thread
                c.open = True
                # empty the last set
                last = set()
        # iterate
        yield old
        old = c

    old.close = range(old.depth)
    yield old
def annotate_tree_properties ( comments ) : if not comments : return it = iter ( comments ) old = next ( it ) old . open = True last = set ( ) for c in it : if old . last_child_id : last . add ( old . last_child_id ) if c . pk in last : c . last = True if c . depth > old . depth : c . open = True else : old . close = list ( range ( old . depth - c . depth ) ) if old . root_id != c . root_id : old . close . append ( len ( old . close ) ) c . open = True last = set ( ) yield old old = c old . close = range ( old . depth ) yield old
iterate through nodes and adds some magic properties to each of them
    representing opening list of children and closing it