more coverage related
This commit is contained in:
127
rowers/utils.py
127
rowers/utils.py
@@ -178,20 +178,11 @@ rankingdurations.append(datetime.time(minute=30))
|
||||
rankingdurations.append(datetime.time(hour=1,minute=15))
|
||||
rankingdurations.append(datetime.time(hour=1))
|
||||
|
||||
|
||||
def is_ranking_piece(workout):
|
||||
if workout.distance in rankingdistances:
|
||||
return True
|
||||
elif workout.duration in rankingdurations:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def range_to_color_hex(groupcols,palette='monochrome_blue'):
|
||||
|
||||
try:
|
||||
plt = palettes[palette]
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
plt = palettes['monochrome_blue']
|
||||
|
||||
rgb = [colorsys.hsv_to_rgb((plt[0]+plt[1]*x)/360.,
|
||||
@@ -203,7 +194,7 @@ def range_to_color_hex(groupcols,palette='monochrome_blue'):
|
||||
|
||||
return colors
|
||||
|
||||
def str2bool(v):
|
||||
def str2bool(v): # pragma: no cover
|
||||
return v.lower() in ("yes", "true", "t", "1")
|
||||
|
||||
def uniqify(seq, idfun=None):
|
||||
@@ -222,11 +213,11 @@ def uniqify(seq, idfun=None):
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
def serialize_list(value,token=','):
|
||||
def serialize_list(value,token=','): # pragma: no cover
|
||||
assert(isinstance(value, list) or isinstance(value, tuple) or isinstance(value,np.ndarray))
|
||||
return token.join([str(s) for s in value])
|
||||
|
||||
def deserialize_list(value,token=','):
|
||||
def deserialize_list(value,token=','): # pragma: no cover
|
||||
if isinstance(value, list):
|
||||
return value
|
||||
elif isinstance(value, np.ndarray):
|
||||
@@ -316,15 +307,15 @@ def myqueue(queue,function,*args,**kwargs):
|
||||
self.result = 1
|
||||
self.id = 1
|
||||
|
||||
def revoke(self):
|
||||
def revoke(self): # pragma: no cover
|
||||
return 1
|
||||
|
||||
if settings.TESTING:
|
||||
return MockJob()
|
||||
elif settings.CELERY:
|
||||
elif settings.CELERY: # pragma: no cover
|
||||
kwargs['debug'] = True
|
||||
job = function.delay(*args,**kwargs)
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
if settings.DEBUG:
|
||||
kwargs['debug'] = True
|
||||
|
||||
@@ -335,7 +326,7 @@ def myqueue(queue,function,*args,**kwargs):
|
||||
|
||||
job = queue.enqueue(function,*args,**kwargs)
|
||||
|
||||
return job
|
||||
return job # pragma: no cover
|
||||
|
||||
|
||||
from datetime import date
|
||||
@@ -361,7 +352,7 @@ def my_dict_from_instance(instance,model):
|
||||
|
||||
try:
|
||||
verbosename = f.verbose_name
|
||||
except:
|
||||
except: # pragma: no cover
|
||||
verbosename = f.name
|
||||
|
||||
get_choice = 'get_'+fname+'_display'
|
||||
@@ -370,7 +361,7 @@ def my_dict_from_instance(instance,model):
|
||||
else:
|
||||
try:
|
||||
value = getattr(instance,fname)
|
||||
except AttributeError:
|
||||
except AttributeError: # pragma: no cover
|
||||
value = None
|
||||
|
||||
if f.editable and value:
|
||||
@@ -390,33 +381,33 @@ def wavg(group, avg_name, weight_name):
|
||||
return d.mean()
|
||||
try:
|
||||
return (d * w).sum() / w.sum()
|
||||
except ZeroDivisionError:
|
||||
except ZeroDivisionError: # pragma: no cover
|
||||
return d.mean()
|
||||
|
||||
def totaltime_sec_to_string(totaltime,shorten=False):
|
||||
if np.isnan(totaltime):
|
||||
return ''
|
||||
hours = int(totaltime / 3600.)
|
||||
if hours > 23:
|
||||
if hours > 23: # pragma: no cover
|
||||
message = 'Warning: The workout duration was longer than 23 hours. '
|
||||
hours = 23
|
||||
|
||||
minutes = int((totaltime - 3600. * hours) / 60.)
|
||||
if minutes > 59:
|
||||
if minutes > 59: # pragma: no cover
|
||||
minutes = 59
|
||||
if not message:
|
||||
if not message: # pragma: no cover
|
||||
message = 'Warning: there is something wrong with the workout duration'
|
||||
|
||||
seconds = int(totaltime - 3600. * hours - 60. * minutes)
|
||||
if seconds > 59:
|
||||
if seconds > 59: # pragma: no cover
|
||||
seconds = 59
|
||||
if not message:
|
||||
if not message: # pragma: no cover
|
||||
message = 'Warning: there is something wrong with the workout duration'
|
||||
|
||||
tenths = int(10 * (totaltime - 3600. * hours - 60. * minutes - seconds))
|
||||
if tenths > 9:
|
||||
if tenths > 9: # pragma: no cover
|
||||
tenths = 9
|
||||
if not message:
|
||||
if not message: # pragma: no cover
|
||||
message = 'Warning: there is something wrong with the workout duration'
|
||||
|
||||
duration = ""
|
||||
@@ -428,7 +419,7 @@ def totaltime_sec_to_string(totaltime,shorten=False):
|
||||
tenths=tenths
|
||||
)
|
||||
else:
|
||||
if hours != 0:
|
||||
if hours != 0: # pragma: no cover
|
||||
duration = "{hours}:{minutes:02d}:{seconds:02d}".format(
|
||||
hours=hours,
|
||||
minutes=minutes,
|
||||
@@ -446,7 +437,7 @@ def totaltime_sec_to_string(totaltime,shorten=False):
|
||||
return duration
|
||||
|
||||
|
||||
def iscoach(m,r):
|
||||
def iscoach(m,r): # pragma: no cover
|
||||
result = False
|
||||
result = m in r.coaches
|
||||
|
||||
@@ -468,7 +459,7 @@ def ewmovingaverage(interval,window_size):
|
||||
|
||||
interval2 = np.vstack((i_ewma1,i_ewma2[::-1]))
|
||||
interval2 = np.mean( interval2, axis=0) # average
|
||||
except ValueError:
|
||||
except ValueError: # pragma: no cover
|
||||
interval2 = interval
|
||||
|
||||
return interval2
|
||||
@@ -479,10 +470,10 @@ class NoTokenError(Exception):
|
||||
def __init__(self,value):
|
||||
self.value=value
|
||||
|
||||
def __str__(self):
|
||||
def __str__(self): # pragma: no cover
|
||||
return repr(self.value)
|
||||
|
||||
class ProcessorCustomerError(Exception):
|
||||
class ProcessorCustomerError(Exception): # pragma: no cover
|
||||
def __init__(self, value):
|
||||
self.value=value
|
||||
|
||||
@@ -516,7 +507,7 @@ def get_strava_stream(r,metric,stravaid,series_type='time',fetchresolution='high
|
||||
'Content-Type': 'application/json',
|
||||
'resolution': 'medium',}
|
||||
|
||||
if metric == 'power':
|
||||
if metric == 'power': # pragma: no cover
|
||||
metric = 'watts'
|
||||
|
||||
url = "https://www.strava.com/api/v3/activities/{stravaid}/streams/{metric}?resolution={fetchresolution}&series_type={series_type}".format(
|
||||
@@ -530,7 +521,7 @@ def get_strava_stream(r,metric,stravaid,series_type='time',fetchresolution='high
|
||||
s = requests.get(url,headers=headers)
|
||||
|
||||
|
||||
if metric=='power':
|
||||
if metric=='power': # pragma: no cover
|
||||
with open('data.txt', 'w') as outfile:
|
||||
json.dump(s.json(), outfile)
|
||||
print('saved to file')
|
||||
@@ -540,7 +531,7 @@ def get_strava_stream(r,metric,stravaid,series_type='time',fetchresolution='high
|
||||
try:
|
||||
if data['type'] == metric:
|
||||
return np.array(data['data'])
|
||||
except TypeError:
|
||||
except TypeError: # pragma: no cover
|
||||
return None
|
||||
|
||||
return None
|
||||
@@ -565,13 +556,13 @@ def steps_read_fit(filename,name='',sport='Custom'):
|
||||
|
||||
response = requests.post(url=url,headers=headers,json={'filename':filename})
|
||||
|
||||
if response.status_code != 200:
|
||||
if response.status_code != 200: # pragma: no cover
|
||||
return None
|
||||
|
||||
w = response.json()
|
||||
try:
|
||||
d = w['workout']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
return None
|
||||
|
||||
return d
|
||||
@@ -583,13 +574,13 @@ def steps_write_fit(steps):
|
||||
|
||||
response = requests.post(url=url,headers=headers,json=steps)
|
||||
|
||||
if response.status_code != 200:
|
||||
if response.status_code != 200: # pragma: no cover
|
||||
return None
|
||||
|
||||
w = response.json()
|
||||
try:
|
||||
filename = w['filename']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
return None
|
||||
|
||||
return filename
|
||||
@@ -599,12 +590,12 @@ def step_to_time_dist(step,avgspeed = 3.7):
|
||||
distance = 0
|
||||
durationtype = step['durationType']
|
||||
|
||||
if step['durationValue'] == 0:
|
||||
if step['durationValue'] == 0: # pragma: no cover
|
||||
return 0,0
|
||||
|
||||
try:
|
||||
targettype = step['targetType']
|
||||
except KeyError:
|
||||
except KeyError: # pragma: no cover
|
||||
targettype = 0
|
||||
|
||||
|
||||
@@ -620,13 +611,13 @@ def step_to_time_dist(step,avgspeed = 3.7):
|
||||
valuelow = step['targetValueLow']
|
||||
valuehigh = step['targetValueHigh']
|
||||
|
||||
if value != 0:
|
||||
if value != 0: # pragma: no cover
|
||||
distance = seconds*value
|
||||
elif valuelow != 0 and valuehigh != 0:
|
||||
elif valuelow != 0 and valuehigh != 0: # pragma: no cover
|
||||
distance = seconds*(valuelow+valuehigh)/2.
|
||||
|
||||
return seconds,distance
|
||||
elif durationtype == 'Distance':
|
||||
elif durationtype == 'Distance': # pragma: no cover
|
||||
value = step['durationValue']
|
||||
distance = value/100.
|
||||
seconds = distance/avgspeed
|
||||
@@ -636,21 +627,21 @@ def step_to_time_dist(step,avgspeed = 3.7):
|
||||
valuelow = step['targetValueLow']
|
||||
valuehigh = step['targetValueHigh']
|
||||
|
||||
if value != 0:
|
||||
if value != 0: # pragma: no cover
|
||||
seconds = distance/value
|
||||
elif valuelow != 0 and valuehigh != 0:
|
||||
elif valuelow != 0 and valuehigh != 0: # pragma: no cover
|
||||
midspeed = (valuelow+valuehigh)/2.
|
||||
seconds = distance/midspeed
|
||||
|
||||
return seconds, distance
|
||||
elif durationtype in ['PowerLessThan','PowerGreaterThan','HrLessThan','HrGreaterThan']:
|
||||
elif durationtype in ['PowerLessThan','PowerGreaterThan','HrLessThan','HrGreaterThan']: # pragma: no cover
|
||||
seconds = 600
|
||||
distance = seconds*avgspeed
|
||||
return seconds,distance
|
||||
|
||||
return seconds,distance
|
||||
|
||||
def get_step_type(step):
|
||||
def get_step_type(step): # pragma: no cover
|
||||
t = 'WorkoutStep'
|
||||
|
||||
if step['durationType'] in ['RepeatUntilStepsCmplt','RepeatUntilHrLessThan','RepeatUntilHrGreaterThan']:
|
||||
@@ -659,7 +650,7 @@ def get_step_type(step):
|
||||
return t
|
||||
|
||||
def peel(l):
|
||||
if len(l)==0:
|
||||
if len(l)==0: # pragma: no cover
|
||||
return None,None
|
||||
if len(l)==1:
|
||||
return l[0],None
|
||||
@@ -667,7 +658,7 @@ def peel(l):
|
||||
first = l[0]
|
||||
rest = l[1:]
|
||||
|
||||
if first['type'] == 'Step':
|
||||
if first['type'] == 'Step': # pragma: no cover
|
||||
return first, rest
|
||||
# repeatstep
|
||||
theID = -1
|
||||
@@ -765,7 +756,7 @@ def ps_dict_order(d,short=False):
|
||||
factor /= multiplier.pop()
|
||||
spaces = spaces[:-18]
|
||||
holduntil.pop()
|
||||
else:
|
||||
else: # pragma: no cover
|
||||
prevstep = sdict3.pop()
|
||||
prevstep['string'] = prevstep['string'][18:]
|
||||
prevprevstep = sdict3.pop()
|
||||
@@ -799,13 +790,13 @@ def step_to_string(step,short=False):
|
||||
|
||||
durationtype = step['durationType']
|
||||
if step['durationValue'] == 0:
|
||||
if durationtype not in ['RepeatUntilStepsCmplt','RepeatUntilHrLessThan','RepeatUntilHrGreaterThan']:
|
||||
if durationtype not in ['RepeatUntilStepsCmplt','RepeatUntilHrLessThan','RepeatUntilHrGreaterThan']: # pragma: no cover
|
||||
return '',type, -1, -1,1
|
||||
|
||||
if durationtype == 'Time':
|
||||
unit = 'min'
|
||||
value = step['durationValue']
|
||||
if value/1000. >= 3600:
|
||||
if value/1000. >= 3600: # pragma: no cover
|
||||
unit = 'h'
|
||||
dd = timedelta(seconds=value/1000.)
|
||||
#duration = humanize.naturaldelta(dd, minimum_unit="seconds")
|
||||
@@ -814,7 +805,7 @@ def step_to_string(step,short=False):
|
||||
unit = 'm'
|
||||
value = step['durationValue']/100.
|
||||
duration = int(value)
|
||||
elif durationtype == 'HrLessThan':
|
||||
elif durationtype == 'HrLessThan': # pragma: no cover
|
||||
value = step['durationValue']
|
||||
if value <= 100:
|
||||
duration = 'until heart rate lower than {v}% of max'.format(v=value)
|
||||
@@ -824,7 +815,7 @@ def step_to_string(step,short=False):
|
||||
duration = 'until heart rate lower than {v}'.format(v=value-100)
|
||||
if short:
|
||||
duration = 'until HR<{v}'.format(v=value-100)
|
||||
elif durationtype == 'HrGreaterThan':
|
||||
elif durationtype == 'HrGreaterThan': # pragma: no cover
|
||||
value = step['durationValue']
|
||||
if value <= 100:
|
||||
duration = 'until heart rate greater than {v}% of max'.format(v=value)
|
||||
@@ -834,7 +825,7 @@ def step_to_string(step,short=False):
|
||||
duration = 'until heart rate greater than {v}'.format(v=value-100)
|
||||
if short:
|
||||
duration = 'until HR>{v}'.format(v=value/100)
|
||||
elif durationtype == 'PowerLessThan':
|
||||
elif durationtype == 'PowerLessThan': # pragma: no cover
|
||||
value = step['durationValue']
|
||||
targetvalue = step['targetvalue']
|
||||
if value <= 1000:
|
||||
@@ -849,7 +840,7 @@ def step_to_string(step,short=False):
|
||||
)
|
||||
if short:
|
||||
'until < {targetvalue} W'.format(targetvalue=targetvalue-1000)
|
||||
elif durationtype == 'PowerGreaterThan':
|
||||
elif durationtype == 'PowerGreaterThan': # pragma: no cover
|
||||
value = step['durationValue']
|
||||
targetvalue = step['targetvalue']
|
||||
if value <= 1000:
|
||||
@@ -864,13 +855,13 @@ def step_to_string(step,short=False):
|
||||
)
|
||||
if short:
|
||||
duration = 'until > {targetvalue} W'.format(targetvalue=targetvalue)
|
||||
elif durationtype == 'RepeatUntilStepsCmplt':
|
||||
elif durationtype == 'RepeatUntilStepsCmplt': # pragma: no cover
|
||||
type = 'RepeatStep'
|
||||
ntimes = ': {v}x'.format(v=step['targetValue'])
|
||||
repeatID = step['durationValue']
|
||||
duration =ntimes
|
||||
repeatValue = step['targetValue']
|
||||
elif durationtype == 'RepeatUntilHrGreaterThan':
|
||||
elif durationtype == 'RepeatUntilHrGreaterThan': # pragma: no cover
|
||||
type = 'RepeatStep'
|
||||
targetvalue = step['targetValue']
|
||||
if targetvalue <= 100:
|
||||
@@ -886,7 +877,7 @@ def step_to_string(step,short=False):
|
||||
if short:
|
||||
duration = ': untl HR>{targetvalue}'.format(targetvalue=targetvalue-100)
|
||||
repeatID = step['durationValue']
|
||||
elif durationtype == 'RepeatUntilHrLessThan':
|
||||
elif durationtype == 'RepeatUntilHrLessThan': # pragma: no cover
|
||||
type = 'RepeatStep'
|
||||
targetvalue = step['targetValue']
|
||||
if targetvalue <= 100:
|
||||
@@ -911,7 +902,7 @@ def step_to_string(step,short=False):
|
||||
except KeyError:
|
||||
targettype = None
|
||||
|
||||
if targettype == 'HeartRate':
|
||||
if targettype == 'HeartRate': # pragma: no cover
|
||||
try:
|
||||
value = step['targetValue']
|
||||
except KeyError:
|
||||
@@ -938,7 +929,7 @@ def step_to_string(step,short=False):
|
||||
l = valuelow - 100,
|
||||
h = valuehigh - 100,
|
||||
)
|
||||
elif targettype == 'Power':
|
||||
elif targettype == 'Power': # pragma: no cover
|
||||
try:
|
||||
value = step['targetValue']
|
||||
except KeyError:
|
||||
@@ -969,7 +960,7 @@ def step_to_string(step,short=False):
|
||||
l = valuelow-1000,
|
||||
h = valuehigh-1000,
|
||||
)
|
||||
elif targettype == 'Speed':
|
||||
elif targettype == 'Speed': # pragma: no cover
|
||||
try:
|
||||
value = step['targetValue']
|
||||
except KeyError:
|
||||
@@ -990,9 +981,9 @@ def step_to_string(step,short=False):
|
||||
target = '@ {v} m/s {p}, per 500m'.format(
|
||||
v=value/1000.,
|
||||
p=pacestring)
|
||||
if short:
|
||||
if short: # pragma: no cover
|
||||
target = '@ {p}'.format(p=pacestring)
|
||||
elif valuelow != 0 and valuehigh != 0:
|
||||
elif valuelow != 0 and valuehigh != 0: # pragma: no cover
|
||||
v = valuelow/1000.
|
||||
pace = 500./v
|
||||
pacestringlow = to_pace(pace)
|
||||
@@ -1012,7 +1003,7 @@ def step_to_string(step,short=False):
|
||||
pl = pacestringlow,
|
||||
ph = pacestringhigh,
|
||||
)
|
||||
elif targettype == 'Cadence':
|
||||
elif targettype == 'Cadence': # pragma: no cover
|
||||
try:
|
||||
value = step['targetValue']
|
||||
except KeyError:
|
||||
@@ -1041,7 +1032,7 @@ def step_to_string(step,short=False):
|
||||
|
||||
notes = ''
|
||||
try:
|
||||
if len(step['description']):
|
||||
if len(step['description']): # pragma: no cover
|
||||
notes = ' - '+step['description']
|
||||
except KeyError:
|
||||
notes = ''
|
||||
@@ -1093,7 +1084,7 @@ def step_to_string(step,short=False):
|
||||
|
||||
return s,type, nr, repeatID, repeatValue
|
||||
|
||||
def strfdelta(tdelta):
|
||||
def strfdelta(tdelta): # pragma: no cover
|
||||
try:
|
||||
minutes, seconds = divmod(tdelta.seconds, 60)
|
||||
tenths = int(tdelta.microseconds / 1e5)
|
||||
|
||||
Reference in New Issue
Block a user