def find_nodes(graph, query, data=False):
    """ Iterator over all nodes matching the data query.
    """
    return ((v, graph.nodes[v]) if data else v 
            for v in graph if query(graph.nodes[v]))