Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from time import perf_counter
- script_start = perf_counter()
- GOAL_DISTANCE = 4000
- def is_leap_year(year):
- return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
- def goal_progression(goal, steps):
- step = goal / steps
- return [i * step for i in range(steps + 1)]
- ###############################################################################
- ####### Calculate yearly progression ##########################################
- ###############################################################################
- # As soon as we filter the activities in any way, the speed drops from about
- # 1s to just over 20s. Also happens if filtering is done through the interface.
- # I guess the cache is useless at that point...
- full_metrics = GC.seasonMetrics(all=True, filter='Sport="Run"')
- metrics = list(zip(full_metrics['date'], full_metrics['Distance']))
- today = datetime.date.today()
- start = datetime.date(year = full_metrics['date'][0].year, month = 1, day = 1)
- a_day = datetime.timedelta(days = 1)
- distances = {}
- while start <= today:
- if not start.year in distances:
- distances[start.year] = [0]
- distance_today = 0
- while metrics and metrics[0][0] <= start:
- distance_today += metrics[0][1]
- metrics = metrics[1:]
- distances[start.year].append(distances[start.year][-1] + distance_today)
- start = start + a_day
- ###############################################################################
- ####### Normalise yearly progression ##########################################
- ###############################################################################
- for year in distances:
- if is_leap_year(year):
- # Treat 28 and 29 Feb as if they were a single day
- distances[year][59] = distances[year].pop(60)
- if len(distances[year]) < 366:
- distances[year] += (366 - len(distances[year])) * [None]
- ###############################################################################
- ####### Plotting ##############################################################
- ###############################################################################
- import plotly
- import plotly.graph_objs as go
- data = [go.Scatter(x = list(range(366)), y = distances[year], mode = 'lines', name = year) for year in distances]
- data.append(go.Scatter(x = list(range(366)), y = goal_progression(GOAL_DISTANCE, 365), mode = 'lines', name = 'Goal'))
- f = plotly.offline.plot({
- 'data': data
- }, auto_open = False)
- GC.webpage(f)
- script_end = perf_counter()
- print("script duration:", script_end - script_start, "seconds")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement