import ast


def _node_filter_for_globals(globals):
    """Filters ast nodes in support of setting globals for exec.

    Removes initial assigns of any variables occuring in
    `globals`. This is to allow globals to provide the initial
    value. Subsequent assigns are not removed under the assumption
    that are re-defining the initial variable value.
    """
    names = set(globals.keys())
    removed = set()

    def f(node):
        if isinstance(node, ast.Assign):
            for target in node.targets:
                if not isinstance(target, ast.Name) or target.id in removed:
                    return True
                if target.id in names:
                    removed.add(target.id)
                    return False
        return True

    return f