Skip to content

Instantly share code, notes, and snippets.

@toinetoine
Created October 2, 2015 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toinetoine/2066a4f9637318d851f2 to your computer and use it in GitHub Desktop.
Save toinetoine/2066a4f9637318d851f2 to your computer and use it in GitHub Desktop.
Create lynx timetables
station_times = list()
station_names = list()
def load_station_names(filename):
times_file = open(filename)
for line in times_file:
# skip empty lines
if len(line.strip()) > 0:
line_elements = line.split(" ")
# first append the station names
for station_name in line_elements:
if len(station_name.strip().strip("\n")) > 0:
station_names.append(station_name.lstrip().rstrip().strip("\n"))
return
def read_times(filename, direction, day_indexes):
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
times_file = open(filename)
for line in times_file:
if len(line.strip()) > 0:
line_elements = line.split(" ")
# append each time to the various stations
station_index = 0 if direction == "down" else len(station_names) - 1
for time in line_elements:
# if encountered placeholder, continue to next station
if time.find("-----") > -1:
# onto next station
if direction == "down":
station_index+=1
else:
station_index-=1
continue
elif len(time.split()) > 0 and time.find(":") > -1:
hour = int(time.split(":")[0].strip())
minute = time.split(":")[1].strip()[:2]
next_day = False
if time.split(":")[1].strip()[2].lower() == "p" or time.split(":")[1].strip()[2].lower() == "pm":
hour += 12
elif time.split(":")[1].strip()[2].lower() == "a" or time.split(":")[1].strip()[2].lower() == "am":
if hour >= 12:
hour = hour % 12
next_day = True
# if neither am or pm, go onto next time
else:
continue
station_time = {"stationId": station_index, "station": station_names[station_index], "direction": direction, "time": (str(hour) + ":" + str(minute))}
for day_index in day_indexes:
this_station_time = dict(station_time)
this_station_time["day"] = days[(day_index + 1) % 7] if next_day else days[day_index]
station_times.append(this_station_time)
# onto next station
if direction == "down":
station_index+=1
else:
station_index-=1
times_file.close()
print(len(station_times))
load_station_names("station_names.txt")
read_times("up_times_weekday.txt", "down", range(5))
read_times("down_times_weekday.txt", "up", range(5))
read_times("up_times_sat.txt", "down", [5])
read_times("down_times_sat.txt", "up", [5])
read_times("up_times_sun.txt", "down", [6])
read_times("down_times_sun.txt", "up", [6])
timetable_info = {"name": "lynx", "locationTimes": station_times, "city": "Charlotte", "state": "NC", "country": "United States"};
server_post_payload = {}
server_post_payload["timetable_data"] = timetable_info
# write to a json file
import json
with open('ouput.json', 'w') as outfile:
json.dump(timetable_info, outfile)
print("done")
# post the json to the server
#import requests
#r = requests.post("http://104.131.219.239:3030/createTimetable", data=json.dumps(server_post_payload))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment