searoute-py icon indicating copy to clipboard operation
searoute-py copied to clipboard

kwargs required in add_edges_from_list

Open IansGithubAcc opened this issue 2 years ago • 1 comments

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)

IansGithubAcc avatar Sep 25 '23 12:09 IansGithubAcc

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 avatar Jan 11 '24 00:01 genthalili

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

genthalili avatar Jun 05 '24 21:06 genthalili