genthalili / searoute-py

A python package to calculate the shortest sea route between two points on Earth.
Apache License 2.0
63 stars 14 forks source link

kwargs required in add_edges_from_list #21

Closed IansGithubAcc closed 3 weeks ago

IansGithubAcc commented 9 months ago

I'm adding custom rootes to the default MarNet using add_edges_from_list. This function takes an edge_list as input. However, when unpacking the edge it is assuming it has additional args. This is ofc not always the case and in this case you need to add an empty dict.

I suggest to rewrite this:

    def add_edges_from_list(self, edge_list):
        if not edge_list:
            return

        for edge in edge_list:
            u,v,args = edge
            self.add_edge(u, v, **args)

to this:

    def add_edges_from_list(self, edge_list):
        if not edge_list:
            return

        for edge in edge_list:
            if len(edge) > 2:
                u, v, args = edge
                self.add_edge(u, v, **args)

            else:
                self.add_edge(*edge)
genthalili commented 5 months ago

Hi @IansGithubAcc,

Can you provide a tuple of 3 like ('from', 'to', {}) ? otherwise there would be checks of ifs and will slow the load of data in this way.

Anyways, I suggest you use from_nodes_edges_set function instead:

# nodes representing (1,2) of lon = 1, lat = 2
# required : 'x' for lon, 'y' for lat ; optional 'tt' for terminals (boolean or None)
my_nodes = {
    (1, 2): {'x': 1, 'y': 2},
    (2, 2): {'x': 2, 'y': 2}
}
# (1,2) -> (2,2) with weight, representing the distance, other attributes can be added
# recognized attributes are : `weight` (distance), `passage` (name of the passage to be restricted by restrictions) 
my_edges = {
    (1, 2): {
        (2, 2): {"weight": 10, "other_attr": "some_value"}
    }
}

# Marnet
myM = sr.from_nodes_edges_set(sr.Marnet(), my_nodes, my_edges)
genthalili commented 3 weeks ago

closing this as no followup, please re-open if needed