coddingtonbear / python-myfitnesspal

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

"OrderedDict mutated during iteration" - Python 3.5 #26

Closed dchristle closed 8 years ago

dchristle commented 8 years ago

I've found what I believe is a new bug in the code, and I believe it's a new error message introduced in Python 3.5. The issue is that measurements.keys() does not return a copy, so it should not be modified while iterating.

The fix is simple - I changed line 294 from:

for date in measurements.keys():

to:

for date in list(measurements.keys()):

And that solves the problem -- list returns a copy, essentially.

Best, David

Traceback log:


RuntimeError Traceback (most recent call last)

in () 2 lastweek = datetime.date(2015, 12, 30) 3 ----> 4 weight = client.get_measurements('Weight') C:\Users\David\Anaconda3\lib\site-packages\myfitnesspal\client.py in get_measurements(self, measurement, lower_bound, upper_bound) 292 293 # remove entries that are not within the dates specified --> 294 for date in measurements.keys(): 295 if not upper_bound >= date >= lower_bound: 296 del measurements[date] RuntimeError: OrderedDict mutated during iteration
coddingtonbear commented 8 years ago

Thanks for the heads-up @dchristle; a fix for this was released as part of v1.7.1 (4d4b917ee1f055ccad503243851cf37e1f89ca5a).

Just an FYI -- if you bump into something like this in the future; please make the change that fixes the problem in a branch and file a pull request. Although in this specific instance it was a one-line change and it wasn't difficult for me to follow (and, probably, for you to write), you're actually making things quite a lot harder on yourself than they have to when you do it this way :-). Cheers, and let me know if you need help with a pull request in the future!

dchristle commented 8 years ago

Hi Adam,

Yep! Thanks for the response. I'm new to both Python 3 and actually "collaborating" on git -- I use it to have a repo of the code I use for work so my coworkers can view it, but I have no experience doing "pulls" and merging changes, so I wasn't totally clear on the "safe" way to implement the change. Anyway, thanks for doing it and hope it was helpful.

Also, thank you for making the myfitnesspal library! I love being able to just plug in the library and get the data I want without having to figure out the nitty gritty stuff with MFP.

Cheers, David

On Sun, Jan 24, 2016 at 6:45 PM, Adam Coddington notifications@github.com wrote:

Thanks for the heads-up @dchristle https://github.com/dchristle; a fix for this was released as part of v1.7.1 (4d4b917 https://github.com/coddingtonbear/python-myfitnesspal/commit/4d4b917ee1f055ccad503243851cf37e1f89ca5a ).

Just an FYI -- if you bump into something like this in the future; please make the change that fixes the problem in a branch and file a pull request. Although in this specific instance it was a one-line change and it wasn't difficult for me to follow (and, probably, for you to write), you're actually making things quite a lot harder on yourself than they have to when you do it this way :-). Cheers, and let me know if you need help with a pull request in the future!

— Reply to this email directly or view it on GitHub https://github.com/coddingtonbear/python-myfitnesspal/issues/26#issuecomment-174358631 .

coddingtonbear commented 8 years ago

It absolutely was helpful! There definitely is a learning curve to using git, but don't worry too much -- it's not as complicated as it sounds, and people are usually very, very forgiving if you just mention that you're pretty new to the process.

Thanks again for the bug report!

Cheers, Adam

On Wed, Feb 3, 2016 at 9:50 PM, David Christle notifications@github.com wrote:

Hi Adam,

Yep! Thanks for the response. I'm new to both Python 3 and actually "collaborating" on git -- I use it to have a repo of the code I use for work so my coworkers can view it, but I have no experience doing "pulls" and merging changes, so I wasn't totally clear on the "safe" way to implement the change. Anyway, thanks for doing it and hope it was helpful.

Also, thank you for making the myfitnesspal library! I love being able to just plug in the library and get the data I want without having to figure out the nitty gritty stuff with MFP.

Cheers, David

On Sun, Jan 24, 2016 at 6:45 PM, Adam Coddington <notifications@github.com

wrote:

Thanks for the heads-up @dchristle https://github.com/dchristle; a fix for this was released as part of v1.7.1 (4d4b917 < https://github.com/coddingtonbear/python-myfitnesspal/commit/4d4b917ee1f055ccad503243851cf37e1f89ca5a

).

Just an FYI -- if you bump into something like this in the future; please make the change that fixes the problem in a branch and file a pull request. Although in this specific instance it was a one-line change and it wasn't difficult for me to follow (and, probably, for you to write), you're actually making things quite a lot harder on yourself than they have to when you do it this way :-). Cheers, and let me know if you need help with a pull request in the future!

— Reply to this email directly or view it on GitHub < https://github.com/coddingtonbear/python-myfitnesspal/issues/26#issuecomment-174358631

.

— Reply to this email directly or view it on GitHub https://github.com/coddingtonbear/python-myfitnesspal/issues/26#issuecomment-179656213 .

raghavddps2 commented 5 years ago

@dchristle That was really helpful. Thank you :)