coddingtonbear / python-myfitnesspal

Access your meal tracking data stored in MyFitnessPal programatically
MIT License
794 stars 136 forks source link

IndexError: list index out of range accessing day.exercises[0].get_as_list() #108

Closed mwarrildt closed 2 years ago

mwarrildt commented 3 years ago

I'm a retired and now occasional programmer with practically no python experience. I hate to bother you but I'm stuck.

I've got an erratic problem. I'm running a python script on an amazon web service vm using around myfitnesspal package. The script iterates through days extracting data, including recorded exercises. It's worked before. I dig it up every 3-4 months to run. (So, obviously, not urgent.)

Now it crashes on line 106. Sometimes. On different days. And the day it was processing when it crashed, can execute smoothly next run. I'm running it beginning with 6/21/2020. It crashed on 7/11. Then, re-running it, it crashed on 6/24 - which it processed smoothly 30 seconds earlier. Same error, different day.

Thanks for any advice you have

99 if bool( day.exercises): 100 print ("Exercises:", day.exercises) 101 print (day.exercises[0]) 102 if bool (day.exercises[0]): 103 cvex = day.exercises[0].get_as_list() 104 print (cvex ) 105 106 for ex in day.exercises[0].get_as_list(): 107 print (ex) 108 cardiomins += int(ex['nutrition_information']['minutes']) 109 burnedcalories += int(ex['nutrition_information']['calories burned'])

(from second run) 2020-06-24 Exercises: [, ] Traceback (most recent call last): File "./getData.py", line 101, in print (day.exercises[0]) IndexError: list index out of range [~] $

(from first run) 2020-06-24 Exercises: [, ] Cardiovascular [{'name': 'Gardening, general', 'nutrition_information': {'minutes': 30.0, 'calories burned': 139.0}}] {'name': 'Gardening, general', 'nutrition_information': {'minutes': 30.0, 'calories burned': 139.0}}

gregryork commented 3 years ago

I would guess that it means that for that particular day, you have no exercises recorded. So getting the first one gives you the list index out of bounds error. I would change line 99 to

for exercises in day.exercises:

then everywhere you have day.exercises[0] replace with exercises. Finally at the end of this loop put a break in if you only want to run for the first elements.

It looks like it should work how you have it but writing it as above will obviate the need for array indices.

mwarrildt commented 3 years ago

Thanks, I'll try your suggestion. The data for 6/24 (where it crashed on the 2nd run) is listed at the bottom of the original note (output from the first run that made it up to 7/11)

tbrooksbank commented 3 years ago

Hey, not sure if you are still stuck on this but I was trying to do something similar and managed to get the total calories for the day with the code below.

day = client.get_date(2020,7,2) calories = 0 for exercises in day.exercises: for exercise in exercises: calories += exercise['calories burned']

This works because the 'exercises' object is giving you the two types, strength or cardiovascular and then the 'exercise' object is giving you each exercise within the specific category.

Your code seems to fail because you reference 'day.exercises[0]' which contains strength exercises, you could also crudely fix this by changing to 'day.exercises[1] '

mwarrildt commented 3 years ago

Thanks. I had used the loop and can't think of a good reason that I would have hard-coded it except as a troubleshooting mechanism that I neglected to revert. I appreciate the heads up.

On Fri, Jan 1, 2021 at 9:32 AM Tom notifications@github.com wrote:

Hey, not sure if you are still stuck on this but I was trying to do something similar and managed to get the total calories for the day with the code below.

day = client.get_date(2020,7,2) calories = 0 for exercises in day.exercises: for exercise in exercises: calories += exercise['calories burned']

This works because the 'exercises' object is giving you the two types, strength or cardiovascular and then the 'exercise' object is giving you each exercise within the specific category.

Your code seems to fail because you reference 'day.exercises[0]' which contains strength exercises, you could also crudely fix this by changing to 'day.exercises[1] '

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/coddingtonbear/python-myfitnesspal/issues/108#issuecomment-753323935, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALSQEVJU4XVB6R2ULFES2P3SXXMJDANCNFSM4RWEXJ7A .

coddingtonbear commented 2 years ago

I believe you folks sorted out the issue here, but let me know if you're still having trouble. Cheers!