-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace naive sorted() implementation with rich comparison methods.
This commit removes the naive ("sort { $a cmp $b }") comparison used by sorted() and replaces it with checks against the rich comparison methods (__lt__, __eq__, etc.) which brings us much closer to the way Python behaves. This also fixes a number of comparison discrepancies between Python and our impelementation and fails hard if no comparison for two data types is implemented.
- Loading branch information
1 parent
fa307a1
commit 15a663b
Showing
9 changed files
with
248 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 9 additions & 6 deletions
15
t/output-comparison-python-interpreter/comparisons-__gt__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
print 1 > 1 | ||
print 1 > 2 | ||
print 2 > 1 | ||
print 'a' > 'a' | ||
print 'a' > 'b' | ||
print 'b' > 'a' | ||
types = [ | ||
1, 2, True, False, [], (), (1, ), {}, [1, 2], [1, 2, 3], | ||
(1, 2), (1, 2, 3), "", "test", {1: 2}, {1: 2, 3: 4}, | ||
"a", "b" | ||
] | ||
|
||
for left in types: | ||
for right in types: | ||
print "%s > %s with '%s' > '%s': %s" % (type(left), type(right), left, right, left > right) |
15 changes: 9 additions & 6 deletions
15
t/output-comparison-python-interpreter/comparisons-__lt__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
print 1 < 1 | ||
print 1 < 2 | ||
print 2 < 1 | ||
print 'a' < 'a' | ||
print 'a' < 'b' | ||
print 'b' < 'a' | ||
types = [ | ||
1, 2, True, False, [], (), (1, ), {}, [1, 2], [1, 2, 3], | ||
(1, 2), (1, 2, 3), "", "test", {1: 2}, {1: 2, 3: 4}, | ||
"a", "b" | ||
] | ||
|
||
for left in types: | ||
for right in types: | ||
print "%s < %s with '%s' < '%s': %s" % (type(left), type(right), left, right, left < right) |
4 changes: 4 additions & 0 deletions
4
t/output-comparison-python-interpreter/function-sorted-tuple.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
x = [(3, 2, 6), (2, 2, 6), (4, 2, 5), (1, 2, 4), ('b', 'c', 'd'), ('a', 'b', 'c'), ('A', 'B', 'C')] | ||
|
||
print sorted(x) | ||
print sorted(x, key=lambda s: s[2]) |