marcusvolz / strava_py

Create artistic visualisations with your exercise data (Python version)
MIT License
163 stars 19 forks source link

Fix encoding on Windows #20

Closed hugovk closed 2 years ago

hugovk commented 2 years ago

Fixes https://github.com/marcusvolz/strava_py/issues/19, same thing as https://github.com/tkrajina/gpxpy/issues/178#issuecomment-638835751.

This also adds an emoji to one of the test files, which before the fix fails on the CI on Windows (but not macOS or Linux): https://github.com/hugovk/strava_py/actions/runs/2987678688

After the fix, all pass.


Also use a "context manager" to open the file, instead of:

activity = gpxpy.parse(open(gpxfile, encoding="utf-8"))

Do this:

    with open(gpxfile, encoding="utf-8") as f:
        activity = gpxpy.parse(f)

A context manager takes care of closing file handles in case of errors, and is equivalent to

f = open(gpxfile, encoding="utf-8")
try:
    activity = gpxpy.parse(f)
finally:
    file.close()  # this runs no matter what happens

More info: https://towardsdatascience.com/why-you-should-use-context-managers-in-python-4f10fe231206