busroute.py

changeset 88
3b86597c5a88
parent 87
9139a94e540c
child 91
209f5930d038
--- a/busroute.py	Thu Mar 29 23:55:36 2018 +0300
+++ b/busroute.py	Fri Apr 13 17:32:40 2018 +0300
@@ -1,38 +1,42 @@
 #!/usr/bin/env python3
 
-from configparser import ConfigParser
-from math import inf
 from misc import profile
-region_info = ConfigParser()
-region_info.read('regions.ini')
+regions = {}
 
-def simplify_name(name):
-	return profile['replacements'].get(name, name)
+def priority(region_name):
+	if region_name in regions:
+		return float(regions[region_name]['priority'])
+	else:
+		return 0
 
-def greatly_simplify_name(name):
-	return profile['more replacements'].get(name, simplify_name(name))
+def simplify_name(region_name):
+	region = regions.get(region_name)
+	if region:
+		return region.get('short_name', region_name)
+	else:
+		return region_name
 
 def reduce_schedule(route, trip_length, whole = False, format = 'medium'):
-	priorities = profile['priorities']
 	length = ((trip_length / 600) * 3 + len(route) * 2) / 5
 	have_already = set()
 	i = 0
 	if not route:
 		return ''
 	while i < len(route):
-		if route[i] in profile['replacements']:
-			route[i] = profile['replacements'][route[i]]
-		if route[i] in have_already:
+		region = regions.get(route[i])
+		if region and region.get('replacement'):
+			route[i] = region['replacement']
+		if not route[i] or route[i] in have_already:
 			del route[i]
 		else:
 			have_already.add(route[i])
 			i += 1
 	from_place = route[0]
 	destination = route[-1]
-	reitti_arvot = {}
+	route_weights = {}
 	f = lambda i: i**-0.3
 	factor = 1 / max(f(i + 1) for i in range(len(route)))
-	while float(priorities.get(route[-1], 0)) < 0:
+	while priority(route[-1]) < 0:
 		del route[-1]
 		if not route:
 			return ''
@@ -41,18 +45,18 @@
 		# muunna indeksi siten että myöhemmät alueet korostuvat
 		i = f(i + 1) * factor
 		# ota prioriteetti huomioon, jotkin alueet ovat tärkeämpiä kyltissä kuin toiset
-		i *= float(priorities.get(stop, 1))
-		reitti_arvot[stop] = i
+		i *= priority(stop)
+		route_weights[stop] = i
 	# nollaa lähtöpaikan arvo ettei se mitenkään tule kylttiin
-	if from_place in reitti_arvot:
-		reitti_arvot[from_place] = 0
+	if from_place in route_weights:
+		route_weights[from_place] = 0
 	# varmista että destination tulee kylttiin
-	reitti_arvot[destination] = 1e10
+	route_weights[destination] = 1e10
 	# muodosta sign-tiedot järjestettynä reittiarvon mukaan
 	weights = sorted([
-		(stop, reitti_arvot[stop], i) \
+		(stop, route_weights[stop], i) \
 		for i, stop in enumerate(route) \
-		if reitti_arvot[stop] >= 1
+		if route_weights[stop] >= 1
 		], key = lambda stop: -stop[1])
 	if format == 'long':
 		weights = weights[:4]

mercurial