import networkx


def diameter(graph):
    """Calculate the diameter of the graph.

    Edge lengths are assumed to be in the 'length' attribute.
    """
    max_l = 0
    for source, length_by_target in networkx.shortest_path_length(graph, weight='length'):
        max_l = max(max_l, *length_by_target.values())
    return max_l