ostep-homework icon indicating copy to clipboard operation
ostep-homework copied to clipboard

Fix the paging-multilevel-translate.py to work with Python 3

Open modulitos opened this issue 4 years ago • 2 comments

When running this script with python 3, we get the following error:

❯ python3 paging-multilevel-translate.py ARG seed 0 ARG allocated 64 ARG num 10

Traceback (most recent call last): File "paging-multilevel-translate.py", line 241, in os = OS() File "paging-multilevel-translate.py", line 57, in init for i in range(0, self.maxPageCount): TypeError: 'float' object cannot be interpreted as an integer

This is due to a breaking change in Python 3, where the division operator represents true division, producing a floating point result. Whereas in Python 2, it performs classic division that rounds the result down toward negative infinity (also known as taking the floor).

This PR changes the operator to use //, which according to the docs:

https://www.python.org/dev/peps/pep-0238/

The // operator will be available to request floor division unambiguously.

This should make the script Python 3 compatible, without changing any behavior in Python 2.

I tested this change by running the script in Python 2, and confirming that the output is the same before and after this change.

modulitos avatar Oct 25 '20 09:10 modulitos

@remzi-arpacidusseau Can we merge this in? I think it will save future students the trouble of having to fix this script when they're running on Python 3.

I can confirm that it introduces no behavioral change.

modulitos avatar Nov 18 '20 04:11 modulitos

Will do, thanks!

On Sun, Oct 25, 2020 at 4:35 AM Lucas Swart [email protected] wrote:

When running this script with python 3, we get the following error:

❯ python3 paging-multilevel-translate.py ARG seed 0 ARG allocated 64 ARG num 10

Traceback (most recent call last): File "paging-multilevel-translate.py", line 241, in os = OS() File "paging-multilevel-translate.py", line 57, in init for i in range(0, self.maxPageCount): TypeError: 'float' object cannot be interpreted as an integer

This is due to a breaking change in Python 3, where the division operator represents true division, producing a floating point result. Whereas in Python 2, it performs classic division that rounds the result down toward negative infinity (also known as taking the floor).

This PR changes the operator to use //, which according to the docs:

https://www.python.org/dev/peps/pep-0238/

The // operator will be available to request floor division unambiguously.

This should make the script Python 2 and 3 compatible.

You can view, comment on, or merge this pull request online at:

https://github.com/remzi-arpacidusseau/ostep-homework/pull/20 Commit Summary

  • python3 fixes

File Changes

Patch Links:

  • https://github.com/remzi-arpacidusseau/ostep-homework/pull/20.patch
  • https://github.com/remzi-arpacidusseau/ostep-homework/pull/20.diff

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/remzi-arpacidusseau/ostep-homework/pull/20, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDKP56DNGLBOJ32ZMFBK7LSMPWPZANCNFSM4S6GT7KA .

remzi-arpacidusseau avatar Nov 18 '20 15:11 remzi-arpacidusseau