coddingtonbear / python-myfitnesspal

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

day.exercises[0].get_as_list() but count activities #114

Closed prankousky closed 3 years ago

prankousky commented 3 years ago

Hi,

I recently found this fantastic library here and now want to write a script to crawl all my activities and burned calories to write them to a database once per day.

So far I have always just have single activities (= one per day). So I can use day.exercises[0].get_as_list(). But what if there are multiple activities? I tried `day.exercises.get_as_list(), which won't work. How can I find out how many activities a day there are? Let's say there were three exercises, then I could write the 3 to a variable and use it in a while loop to read each any every one of them.

Thank you in advance for your help :)

hannahburkhardt commented 3 years ago

day.exercises currently contains 2 entries: One for cardiovascular and one for strength exercises. For your purposes it may or may not be ok to assume that this will stay the same going forward, so you could simply check day.exercises[0] as well as day.exercises[1]. Ideally though, you'd want a more flexible solution.

This will give you a list with a list of exercises in each exercise category, using list comprehension:

all_exercises = [exercise_category.get_as_list() for exercise_category in day.exercises]

If you want one flat list of all the exercises, here's one solution using the += shorthand, which will append all elements from the list on the right of the += to the list on the left.

all_exercises = []
for exercise_category in day.exercises: 
    all_exercises += exercise_category 
prankousky commented 3 years ago

Thank you @hannahburkhardt