Пример #1
0
 def _add_station(self, station):
     is_error = False
     try:
         start_time = time.time()
         stations = self.DICTIONARY.keys()
         if station not in stations:
             self.DICTIONARY[station] = {}
             print(
                 str(time.time() - start_time) + " Dictionary add station")
             return True
         else:
             is_error = True
             return ERRORS.get("duplicate")
     except KeyError:
         is_error = True
         return ERRORS.get("key_error")
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         pass
         if is_error == False:
             try:
                 self.db._insert_station_data(station)
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #2
0
 def _update_station_connector(self, station, next_station, distance):
     is_error = False
     try:
         start_time = time.time()
         if self.DICTIONARY[station][next_station] != None:
             self.DICTIONARY[station][next_station] = int(distance)
             print(
                 str(time.time() - start_time) +
                 " Dictionary update station connection")
             return True
         else:
             return ERRORS.get("not_exist")
     except KeyError:
         is_error = True
         return ERRORS.get("key_error")
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._update_connected_station_data(
                     station, next_station, distance)
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #3
0
 def _add_station_connector(self, station, next_station, distance):
     is_error = False
     try:
         try:
             start_time = time.time()
             self.DICTIONARY[station][next_station]
             is_error = True
             return ERRORS.get("duplicate")
         except KeyError:
             self.DICTIONARY[station][next_station] = int(distance)
             print(
                 str(time.time() - start_time) +
                 " Dictionary add station connection")
             return True
     except KeyError:
         is_error = True
         return ERRORS.get("key_error")
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._insert_connected_station_data(
                     station, next_station, int(distance))
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #4
0
 def _update_station(self, key, station_name):
     is_error = False
     try:
         start_time = time.time()
         target_data = self.DICTIONARY[key]
         self.DICTIONARY[station_name] = target_data
         self.DICTIONARY.pop(key)
         matched_stations = []
         for station in self.DICTIONARY:
             for connector in self.DICTIONARY[station]:
                 if connector == key:
                     matched_stations.append(station)
         for station in matched_stations:
             target_data = self.DICTIONARY[station][key]
             self.DICTIONARY.pop(station)
             self.DICTIONARY[station] = {}
             self.DICTIONARY[station][station_name] = target_data
         print(str(time.time() - start_time) + " Dictionary update station")
         return True
     except KeyError:
         is_error = True
         return ERRORS.get("key_error")
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._update_station_data(key, station_name)
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #5
0
 def _update_station_distance(self,
                              src,
                              dest=None,
                              station_name=None,
                              distance=None):
     try:
         if src is not None:
             index = self.stations.index(src)
             if index is not None:
                 if station_name is None:
                     node_list = self.stations_arr[index]
                     result = node_list._update(dest, None, distance)
                     if result == True:
                         return True
                     else:
                         return result
                 if dest is None:
                     for station in self.stations_arr:
                         station._update(src, station_name)
                     self.stations[index] = station_name
                     return True
             else:
                 return ERRORS.get("not_exist")
         else:
             return ERRORS.get("null_error")
     except Exception:
         return ERRORS.get("error")
Пример #6
0
 def _delete(self, key):
     is_error = False
     try:
         start_time = time.time()
         self.DICTIONARY.pop(key)
         matched_stations = []
         for station in self.DICTIONARY:
             for connector in self.DICTIONARY[station]:
                 if connector == key:
                     matched_stations.append(station)
         for station in matched_stations:
             self.DICTIONARY[station].pop(key)
         print(str(time.time() - start_time) + " Dictionary delete station")
         return True
     except KeyError:
         is_error = True
         return ERRORS.get("key_error")
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._delete_station_data(key)
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #7
0
 def _build(self):
     connected_station_data_set = self.db._get_all_connected_stations()
     station_data_set = self.db._get_all_stations()
     try:
         if connected_station_data_set[
                 'status'] and station_data_set['status'] is True:
             for data in station_data_set['data']:
                 station = data[1]
                 self.DICTIONARY[station] = {}
             for data in connected_station_data_set['data']:
                 station = data[0]
                 next_station = data[1]
                 distance = int(data[2])
                 target_data = self.DICTIONARY.get(station)
                 if target_data is not None:
                     self.DICTIONARY[station][next_station] = distance
                 else:
                     self.DICTIONARY[station] = {next_station: distance}
         else:
             return ERRORS.get("database")
     except KeyError:
         return ERRORS.get("key_error")
     except IndexError:
         return ERRORS.get("index")
     except Exception:
         return ERRORS.get("error")
 def _add_station_connector(self, station, next_stations, distance):
     is_error = False
     try:
         start_time = time.time()
         all_connections = self._get_all_connections()
         for connection in all_connections['data']:
             station_name = connection["station_name"]
             next_station = connection["next_station"]
             if station_name == station and next_station == next_stations:
                 return ERRORS.get("duplicate")
         result = self.LINKEDLIST._add(station, next_stations,
                                       int(distance))
         print(
             str(time.time() - start_time) +
             " Linkedlist add station connection")
         if result is True:
             return True
         else:
             is_error = True
             return result
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 print(
                     self.db._insert_connected_station_data(
                         station, next_stations, str(distance)))
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #9
0
 def _get(self, key):
     try:
         if key is not None:
             index = self.stations.index(key)
             if index is not None:
                 node_list = self.stations_arr[index]
                 return node_list._get_all()
         else:
             return ERRORS.get("null_error")
     except Exception:
         return ERRORS.get("error")
 def _update_station(self,key,data):
     try:
         if key in self.stations:
             index = self.stations.index(key)
             self.stations[index]=data
             return True
         else:
             return ERRORS.get('not_exist')
     except KeyError:
         return ERRORS.get('key_error')
     except Exception:
         return ERRORS.get('error')
Пример #11
0
 def _print(self):
     try:
         current_station = self.head
         while current_station != None:
             print(
                 str(current_station.station_name) + ":" +
                 str(current_station.distance))
             current_station = current_station.next_station
     except ArithmeticError:
         return ERRORS.get('data_type')
     except Exception:
         return ERRORS.get("error")
Пример #12
0
 def _add_station(self, key):
     try:
         if key is not None:
             if key not in self.stations:
                 self.stations_arr.append(SinglyLinkedList())
                 self.stations.append(key)
                 return True
             else:
                 return ERRORS.get("duplicate")
         else:
             return ERRORS.get('null_error')
     except Exception:
         return ERRORS.get("error")
Пример #13
0
 def _add(self, src, dest, distance):
     try:
         if src and dest and distance is not None:
             if src in self.stations:
                 index = self.stations.index(src)
                 self.stations_arr[index]._add(dest, None, distance)
                 return True
             else:
                 return ERRORS.get("not_exist")
         else:
             return ERRORS.get("null_error")
     except Exception:
         return ERRORS.get("error")
Пример #14
0
 def _pop(self, key):
     try:
         if key is not None and key in self.stations:
             index = self.stations.index(key)
             if index is not None:
                 self.stations_arr.pop(index)
                 self.stations.pop(index)
                 for station in self.stations_arr:
                     station._pop(key)
                 return True
         else:
             return ERRORS.get("not_exist")
     except Exception:
         return ERRORS.get("error")
Пример #15
0
 def _pop_station_connection(self, key, next_station):
     try:
         if key is not None and key in self.stations:
             index = self.stations.index(key)
             if index is not None:
                 target = self.stations_arr[index]._pop(next_station)
                 if target == True:
                     return True
                 else:
                     return ERRORS.get('not_exist')
         else:
             return ERRORS.get("not_exist")
     except Exception:
         return ERRORS.get("error")
 def _pop(self,key):
     try:
         if key in self.stations:
             index = self.stations.index(key)
             self.stations.pop(index)
             self.adjacency_matrix.pop(index)
             for stations in self.adjacency_matrix:
                 stations.pop(index)
             return True
         else:
             return ERRORS.get('not_exist')
     except KeyError:
         return ERRORS.get('key_error')
     except Exception:
         return ERRORS.get('error')
 def _add_station(self,station):
     try:
         if station is not None:
             self.stations_count+=1
             self.stations.append(station)
             for station in self.adjacency_matrix:
                 station.append(0)
             self.adjacency_matrix.append([0 for i in range(self.stations_count)])
             return True
         else:
             return ERRORS.get('null_error')
     except KeyError:
         return ERRORS.get('key_error')
     except Exception:
         return ERRORS.get('error')
 def _get(self,key):
     try:
         if key is not None and key in self.stations:
             index = self.stations.index(key)
             vertex_list=[] 
             for i in range(0,self.stations_count):
                 if not i==index and not self.adjacency_matrix[index][i] ==0:
                     vertex_list.insert(0,{self.stations_copy[i]:self.adjacency_matrix[index][i]})
             return vertex_list
         else:
             return ERRORS.get('null_error')
     except KeyError:
         return ERRORS.get('key_error')
     except Exception:
         return ERRORS.get('error')
Пример #19
0
 def _get(self, key):
     try:
         target_node = None
         temp = self.head
         while temp != None:
             if temp.station_name == key:
                 target_node = temp
                 break
             temp = temp.next_station
         if target_node is not None:
             return target_node
         else:
             return ERRORS.get("not_exist")
     except Exception:
         return ERRORS.get("error")
Пример #20
0
 def _make_undirected_graph(self, graph):
     all_connections = graph._get_all_connections()
     if all_connections['status'] is True:
         for connection in all_connections['data']:
             station_name = connection["station_name"]
             next_station = connection["next_station"]
             distance = connection["distance"]
             if station_name not in self._undirected_graph.keys():
                 self._undirected_graph[station_name] = {}
                 self._undirected_graph[station_name][
                     next_station] = distance
             else:
                 self._undirected_graph[station_name][
                     next_station] = distance
         for connection in all_connections['data']:
             station_name = connection["station_name"]
             next_station = connection["next_station"]
             distance = connection["distance"]
             if next_station not in self._undirected_graph.keys():
                 self._undirected_graph[next_station] = {}
                 self._undirected_graph[next_station][
                     station_name] = distance
             else:
                 self._undirected_graph[next_station][
                     station_name] = distance
     else:
         return ERRORS.get("error")
 def _update_station_distance(self,src,dest,distance):
     try:
         if src and dest in self.stations:
             index_src = self.stations.index(src)
             index_dest = self.stations.index(dest)
             if(self.adjacency_matrix[index_src][index_dest]!=0):
                 self.adjacency_matrix[index_src][index_dest]=distance
                 return True
             else:
                 return ERRORS.get('not_exist')
         else:
             return ERRORS.get('not_exist')
     except KeyError:
         return ERRORS.get('key_error')
     except Exception:
         return ERRORS.get('error')
 def _pop_station_connection(self,key,next_station):
     try:
         if key and next_station in self.stations:
             indexKey = self.stations.index(key)
             indexNextStation= self.stations.index(next_station)
             if(self.adjacency_matrix[indexKey][indexNextStation]!=0):
                 self.adjacency_matrix[indexKey][indexNextStation]=0
                 return True 
             else:
                 return ERRORS.get('not_exist')
         else:
             return ERRORS.get('not_exist')
     except KeyError:
         return ERRORS.get('key_error')
     except Exception:
         return ERRORS.get('error')
Пример #23
0
 def _update(self, key, station_name=None, distance=None):
     try:
         target_station = self._get(key)
         if type(target_station
                 ) == dict and target_station['status'] == False:
             return ERRORS.get("not_exist")
         else:
             if station_name is None:
                 target_station.distance = distance
             elif distance is None:
                 target_station.station_name = station_name
             elif station_name and distance is not None:
                 target_station.distance = distance
                 target_station.station_name = station_name
             return True
     except Exception:
         return ERRORS.get("error")
 def _get_all_connections(self):
     try:
         start_time = time.time()
         result = self.LINKEDLIST._get_all()
         print(
             str(time.time() - start_time) +
             " Linkedlist get all station connections")
         return result
     except Exception:
         return ERRORS.get("error")
Пример #25
0
 def _pop(self, key):
     try:
         target_node = self.head
         target_node_prev = None
         while target_node.station_name != key:
             target_node_prev = target_node
             target_node = target_node.next_station
             if target_node is None:
                 break
         if target_node != None:
             if target_node_prev != None:
                 target_node_prev.next_station = target_node.next_station
             else:
                 self.head = target_node.next_station
             return True
         else:
             return ERRORS.get("not_exist")
     except Exception:
         return ERRORS.get("error")
 def _add_station(self, station):
     is_error = False
     try:
         start_time = time.time()
         result = self.ADJACENCY._add_station(station)
         print(str(time.time() - start_time)+" Adjacency Insert Station")
         if result is True:
             return True
         else:
             is_error = True
             return result
     except Exception:
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._insert_station_data(station)
             except psycopg2.Error:
                 return ERRORS.get("database")
 def _delete(self, key):
     is_error = False
     try:
         start_time = time.time()
         result = self.ADJACENCY._pop(key)
         print(str(time.time() - start_time)+" Adjacency Delete Station")
         if result is True:
             return True
         else:
             is_error = True
             return result
     except Exception:
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._delete_station_data(key)
             except psycopg2.Error:
                 return ERRORS.get("database")
Пример #28
0
 def _print_graph(self):
     try:
         for i in range(len(self.stations_arr)):
             print(str(self.stations[i]) + "->")
             print(self.stations_arr[i]._print())
             print(" \n")
         if len(self.stations_arr) is 0:
             return None
     except Exception:
         return ERRORS.get("error")
Пример #29
0
 def _get_all(self):
     try:
         station_list = []
         current_station = self.head
         while current_station != None:
             station_list.append(
                 {current_station.station_name: current_station.distance})
             current_station = current_station.next_station
         return station_list
     except Exception:
         return ERRORS.get("error")
 def _delete(self, key):
     is_error = False
     try:
         start_time = time.time()
         result = self.LINKEDLIST._pop(key)
         print(str(time.time() - start_time) + " Linkedlist delete station")
         if result is True:
             return True
         else:
             is_error = True
             return result
     except Exception:
         is_error = True
         return ERRORS.get("error")
     finally:
         if is_error == False:
             try:
                 self.db._delete_station_data(key)
             except psycopg2.Error:
                 return ERRORS.get("database")