code styling: add comments and fix pep8

This commit is contained in:
Marco Thomas
2022-07-27 17:58:29 +02:00
parent 68885ff560
commit 8a0a59c222
4 changed files with 49 additions and 12 deletions

View File

@@ -1,14 +1,24 @@
"""
All types used in the project
"""
from enum import Enum
import math
class ParsingMode(Enum):
"""
Used for parsing
"""
LOCATIONS = 1
FLIGHTSCHEDULE = 2
FINDCONNECTION = 3
class LocationType(Enum):
"""
Differenciate between types of location
"""
LOCATION = 1
HALTESTELLE = 2
FLUGHAFEN = 3
@@ -27,6 +37,10 @@ class TransportData():
class TransportMethod(Enum):
"""
More precise methods of transportation.
They are subgroups of TransportKind.
"""
WALK = TransportData(0, 4)
CITY = TransportData(0.189, 30)
AUTOBAHN = TransportData(0.189, 100)
@@ -37,7 +51,8 @@ class TransportMethod(Enum):
class TransportKind(Enum):
"""
distance_penalty
General transportation kind.
Value: distance_penalty
"""
INDIVIDUAL = 0.2
TRAIN = 0.1
@@ -57,14 +72,20 @@ class Coordinate:
class Location:
def __init__(self, coord: Coordinate, continent: int, name: str, type: LocationType):
def __init__(
self,
coord: Coordinate,
continent: int,
name: str,
type: LocationType):
self.coord = coord
self.continent = continent
self.name = name
self.type = type
def __str__(self):
return f"{{Location: {self.coord}, continent: {self.continent}, name: {self.name}, type: {self.type}}}"
return f"{{Location: {self.coord}, continent: {self.continent}, \
name: {self.name}, type: {self.type}}}"
def __repr__(self):
return self.__str__()
@@ -88,13 +109,18 @@ class Location:
class DataSet:
"""
Represent the parsed data
"""
def __init__(self, locations: dict, flights: dict, connection: tuple):
self.locations = locations
self.flights = flights
self.connection = connection
def __str__(self):
return f"locations: {self.locations}, flights: {self.flights}, connection: {self.connection}"
return f"locations: {self.locations}, flights: {self.flights}, \
connection: {self.connection}"
def __repr__(self):
return self.__str__()

View File

@@ -12,7 +12,9 @@ class Dijkstra():
self.graph = graph
self.table = {}
for vertex in graph:
self.table.update({vertex: {"distance": (9999999, 999999), "prev": None}})
self.table.update({vertex: {
"distance": (9999999, 999999),
"prev": None}})
self.table[connection[0]]["distance"] = (0, 0)
def algorithm(self, switch: str):
@@ -21,6 +23,7 @@ class Dijkstra():
print("Performing Dijkstra on the following graph...")
for vertex in self.graph:
print(f"{vertex}: {self.graph[vertex]}")
print("")
for v1 in self.graph.keys():
neighbors = self.graph[v1]
@@ -41,7 +44,7 @@ class Dijkstra():
self.table[key]["distance"] = (time, co2)
self.table[key]["prev"] = v1
print("self.table:", self.table)
print("Final Dijkstra table:", self.table)
def print_result(self, connection: tuple):
sequence = []

View File

@@ -41,19 +41,23 @@ def calc_time(
return distance / TransportMethod.WALK.value.speed
elif distance <= 10 + 1:
speed = 1 / TransportMethod.WALK.value.speed
return speed + ((distance - 1) / TransportMethod.CITY.value.speed)
new_dist = (distance - 1)
speed += (new_dist / TransportMethod.CITY.value.speed)
return speed
elif distance < 2000 + 10 + 1:
speed = 1 / TransportMethod.WALK.value.speed
speed += 10 / TransportMethod.CITY.value.speed
return speed + ((distance - 1 - 10) / TransportMethod.AUTOBAHN.value.speed)
new_dist = (distance - 1 - 10)
speed += (new_dist / TransportMethod.AUTOBAHN.value.speed)
return speed
case TransportKind.TRAIN:
if distance <= 25:
return distance / TransportMethod.OEPNV.value.speed
else:
return (distance / TransportMethod.ICE.value.speed)
case TransportKind.FLYING:
assert(flights != None)
assert(frm != None)
assert(flights)
assert(frm)
stops = flights[frm]["stops"]
domestic = flights[frm]["domestic"]
if domestic:
@@ -100,9 +104,10 @@ def create_graph(dataset: DataSet) -> dict:
# Train
is_haltestelle = locations[start].type == LocationType.HALTESTELLE
is_airport = locations[start].type == LocationType.FLUGHAFEN
is_same_continent = locations[start].is_same_continent(locations[dest])
# trains only drive from haltestelle and same continent
if is_haltestelle and is_same_continent:
if (is_haltestelle or is_airport) and is_same_continent:
dest_type = locations[dest].type
# there are only trains between haltestellen or airports
is_dest_haltestelle = dest_type == LocationType.HALTESTELLE

View File

@@ -72,7 +72,10 @@ def parse(filename: str) -> DataSet:
domestic = True
else:
domestic = False
flights.update({id1: {"to": id2, "stops": stops, "domestic": domestic}})
flights.update({id1: {
"to": id2,
"stops": stops,
"domestic": domestic}})
continue
case ParsingMode.FINDCONNECTION:
print("Parsing connection...")