oliverkurth/runquery

update to use pint instead of units

Closed this issue · 3 comments

stravalib has switched to using the python module pint instead of units.

Currently we use a fork from before that switch. This is a temporary fix.

The error when using the latest version of stravalib with pint:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/flask/app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/usr/src/app/runquery/query.py", line 612, in api_set_search
    stats = calc_stats(athlete, matched)
  File "/usr/src/app/runquery/query.py", line 417, in calc_stats
    stats_total['dist'] += a.distance
TypeError: unsupported operand type(s) for +=: 'Quantity' and 'Quantity'

Relevant code section:

def calc_stats(athlete, activities):
    stats = {}
    subs = ['total', 'per_activity', 'per_time', 'per_dist']
    metrics = ['dist', 'time', 'elevation']

    if athlete.measurement_preference != 'feet':
        units_used = {
            #'dist': unithelper.kilometers, # km results in 0 per_dist stats, reason unknown
            'dist': unithelper.meters,
            'time' : unithelper.seconds,
            'elevation': unithelper.meters
        }
    else:
        units_used = {
            'dist': unithelper.miles,
            'time' : unithelper.seconds,
            'elevation' : unithelper.feet
        }

    count = len(activities)

    for sub in subs:
        stats[sub] = {}
        for m in metrics:
            stats[sub][m] = units_used[m](0.0)

    stats_total = stats['total']
    for a in activities:
        stats_total['dist'] += a.distance

where a ultimately comes from stravalib.model.Activity().

Fixed with f558509.