Skip to content

make filetools tests pass with Python 3#2729

Merged
wpoely86 merged 14 commits intoeasybuilders:4.xfrom
boegel:py3_test_filetools
Jan 19, 2019
Merged

make filetools tests pass with Python 3#2729
wpoely86 merged 14 commits intoeasybuilders:4.xfrom
boegel:py3_test_filetools

Conversation

@boegel
Copy link
Member

@boegel boegel commented Jan 18, 2019

No description provided.

@boegel boegel added the python3 Python 3 compatibility label Jan 18, 2019
@boegel boegel added this to the 4.0 milestone Jan 18, 2019
@boegel boegel requested a review from akesandgren January 18, 2019 20:40
diff = False
for i in range(len(self.base_lines)):
lines = filter(None, self.get_line(i))
lines = [line for line in self.get_line(i) if line]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this exactly the same? Shouldn't you use if line is not None?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, with None as first element to function this is exactly equivalent, it's even mentioned explicitly to be so in the documentation: https://docs.python.org/2/library/functions.html#filter and https://docs.python.org/3/library/functions.html#filter

# sort lines with most changes last;
# sort lines with equal number of changes alphabetically to ensure consistent output;
# limit number to MAX_DIFF_GROUPS
lines = sorted(lines, key=lambda line: (len(changes_dict[line]), line))[:MAX_DIFF_GROUPS]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this? Your lambda returns a tuple? What does happen then by default if you compare tuples?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuples are sorted by 1st element, and 2nd element is taken into account if the 1st elements are equal:

>>> (1,2) < (2,3)
True
>>> (2,2) < (2,3)
True
>>> (2,4) < (2,3)
False

So here, primary sorting is done based on # changes, secondary sorting (for lines with equal number of changes) is done alphabetically, which guarantees consistent ordering.

For some reason this was not a problem with Python 2.x (or Python 3.6 according to Travis), but with Python 3.7 I was getting different ordering sometimes for lines with equal number of changes... Not entirely sure why that happening, but this change fixes it.

@wpoely86 wpoely86 merged commit 4babe02 into easybuilders:4.x Jan 19, 2019
@boegel boegel deleted the py3_test_filetools branch January 19, 2019 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python3 Python 3 compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants