-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add legacy support (Python 3.7 & 3.8) in Sterzo #27
Add legacy support (Python 3.7 & 3.8) in Sterzo #27
Conversation
I tested the change on Python 3.8 and Python 3.10 with success. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! Thanks for spotting this lack of compatibility with Python 3.7/3.8 and suggesting this fix. It looks good, I've just left a few comments.
fp.seek(self._latest_challenge * 2, 1) | ||
code_1 = int.from_bytes(fp.read(1), 'little') | ||
code_2 = int.from_bytes(fp.read(1), 'little') | ||
# moved .dat file out of 'data' because accessing directories not possible for importlib.resources.path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can keep the data directory, we just need to add __init__.py
to make it a Python package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean putting a __init__.py
to the data
directory like so?
├── pycycling
├── sterzo.py
├── data
│ ├── __init__.py
│ └── sterzo-challenge-codes.dat
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Can be completely empty I believe.
pycycling/sterzo.py
Outdated
code_1 = int.from_bytes(fp.read(1), 'little') | ||
code_2 = int.from_bytes(fp.read(1), 'little') | ||
else: # legacy support < 3.9 | ||
with importlib.resources.path(__package__, 'sterzo-challenge-codes.dat') as file_path: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason not to use importlib.resources.open_binary
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be using that instead.
pycycling/sterzo.py
Outdated
else: # legacy support < 3.9 | ||
with importlib.resources.path(__package__, 'sterzo-challenge-codes.dat') as file_path: | ||
with open(file_path, 'rb') as fp: | ||
fp.seek(self._latest_challenge * 2, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor this to remove the duplication of these lines?
Putting Using:
Throws:
I think this limitation is what the docs are referring to when it says:
|
Since we've created a subpackage, you need to add a |
Thank you for guiding me. Please see latest commit |
I tested both branches |
Fix #28 |
importlib.resources.files
insterzo.py
was newly introduced in Python 3.9.I needed to use this library on Python 3.8, so I implemented a version check to use the correct importlib API. More info at python docs
The only side effect is that the
sterzo-challenge-codes.dat
file had to be moved out of thedata
directory.