Fixed looping bug

This commit is contained in:
Alexander Munch-Hansen 2019-04-06 17:49:39 +02:00
parent 6a0d0863b2
commit 44c65183c0

View File

@ -1,5 +1,6 @@
import calendar import calendar
import datetime from datetime import datetime, timedelta
import json import json
import requests import requests
@ -17,7 +18,7 @@ def is_tide(context: Context) -> Prediction:
p = Prediction() p = Prediction()
month, cur_year_total_cars, last_year_total_cars = determine_month() month, cur_year_total_cars, last_year_total_cars = determine_month()
month = int(month)
p.reasons.append(f"Because the month is f{calendar.month_name[month]}") p.reasons.append(f"Because the month is f{calendar.month_name[month]}")
p.reasons.append(f"Because the number of cars having driven on the Storbæltsbro is f{cur_year_total_cars}") p.reasons.append(f"Because the number of cars having driven on the Storbæltsbro is f{cur_year_total_cars}")
p.reasons.append(f"And because the number of cars having driven over it in the last year is f{last_year_total_cars}") p.reasons.append(f"And because the number of cars having driven over it in the last year is f{last_year_total_cars}")
@ -43,21 +44,33 @@ def is_tide(context: Context) -> Prediction:
time_diff = (water_level[0] - last_match[0]).seconds time_diff = (water_level[0] - last_match[0]).seconds
average_inc = time_diff/diff average_inc = time_diff/diff
average_delta = datetime.timedelta(seconds=average_inc) average_delta = timedelta(seconds=average_inc)
if last_match[1] < 0: # Increasing
if last_match[1] < 0 and last_match[1] < current_water_level: # Increasing
time = last_match time = last_match
while time[1] != current_water_level: while time[1] != current_water_level:
time[0] += average_delta time[0] += average_delta
time[1] += 1 time[1] += 1
elif last_match[1] < 0 and last_match[1] > current_water_level:
elif last_match[1] > 0: # Decreasing
time = last_match time = last_match
while time[1] != current_water_level: while time[1] != current_water_level:
time[0] += average_delta time[0] += average_delta
time[1] -= 1 time[1] -= 1
elif last_match[1] > 0 and last_match[1] > current_water_level: # Decreasing
time = last_match
while time[1] != current_water_level:
time[0] += average_delta
time[1] -= 1
elif last_match[1] > 0 and last_match[1] < current_water_level:
time = last_match
while time[1] != current_water_level:
time[0] += average_delta
time[1] += 1
last_match = water_level last_match = water_level
moments.append(time[0]) moments.append(time[0])