def filter_tree(tree, keep, node='/'):
    '''
    Keep a branch if and only if it is of of a 'keep' type or has a child of
    the 'keep' type
    '''
    filtered = []

    # Filter
    for cgroup in tree.get(node, []):
        if filter_tree(tree, keep, cgroup['cgroup']):
            filtered.append(cgroup)
        elif cgroup['type'] in keep:
            filtered.append(cgroup)

    # Commit
    if filtered:
        tree[node] = filtered
    else:
        tree.pop(node, None)

    return bool(filtered)
dataset/ETHPy150Open yadutaf/ctop/cgroup_top.py/filter_tree/original