File size: 1,163 Bytes
1c015d5
 
 
 
ced8d95
1c015d5
 
 
 
ced8d95
1c015d5
ced8d95
1c015d5
ced8d95
1c015d5
ced8d95
1c015d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import pandas as pd
from typing import Dict

class CSVTravelGraph():
    def __init__(self, csv_file: str):
        """
        Read csv and create the graph in function of the given mode
        """
        self.csv = pd.read_csv(csv_file, sep="\t")
        self.data = self.generateGraph()
        
    def generateGraph(self):
        """
        Create a graph by browsing the data retrieved in the csv
        Returns:
            (Dict[str, Dict[str, float]]): The graph
        """
        graph: Dict[str, Dict[str, float]] = {}
        
        def addTravelToTheGraph(depart, arrive, duree):
            if depart in graph:
                graph[depart][arrive] = duree
            else:
                graph[depart] = {arrive: duree}
            
        for index, row in self.csv.iterrows():
            trip_id, trajet, duree = row
            points = trajet.split(' - ')
            depart = ' - '.join(points[:-1])
            arrive = points[-1]
            duree = float(duree)
            
            addTravelToTheGraph(depart, arrive, duree)
            addTravelToTheGraph(arrive, depart, duree)
            
        return graph