Mercurial > p > roundup > code
annotate roundup/anypy/cmp_.py @ 7531:913a73b9fab5 2.3.0
Update for 2.3.0 release
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 12 Jul 2023 23:00:25 -0400 |
| parents | 142553f58694 |
| children | 706f3be6ffbb |
| rev | line source |
|---|---|
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
1 try: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
2 None < 0 |
| 6047 | 3 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
4 def NoneAndDictComparable(v): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
5 return v |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
6 except TypeError: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
7 # comparator to allow comparisons against None and dict |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
8 # comparisons (these were allowed in Python 2, but aren't allowed |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
9 # in Python 3 any more) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
10 class NoneAndDictComparable(object): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
11 def __init__(self, value): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
12 self.value = value |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
13 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
14 def __cmp__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
15 if not isinstance(other, self.__class__): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
16 raise TypeError('not comparable') |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
17 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
18 if self.value == other.value: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
19 return 0 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
20 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
21 elif self.value is None: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
22 return -1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
23 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
24 elif other.value is None: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
25 return 1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
26 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
27 elif type(self.value) == tuple and type(other.value) == tuple: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
28 for lhs, rhs in zip(self.value, other.value): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
29 lhsCmp = NoneAndDictComparable(lhs) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
30 rhsCmp = NoneAndDictComparable(rhs) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
31 result = lhsCmp.__cmp__(rhsCmp) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
32 if result != 0: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
33 return result |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
34 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
35 return len(self.value) - len(other.value) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
36 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
37 elif type(self.value) == dict and type(other.value) == dict: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
38 diff = len(self.value) - len(other.value) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
39 if diff == 0: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
40 lhsItems = tuple(sorted(self.value.items(), |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
41 key=NoneAndDictComparable)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
42 rhsItems = tuple(sorted(other.value.items(), |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
43 key=NoneAndDictComparable)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
44 return -1 if NoneAndDictComparable(lhsItems) < NoneAndDictComparable(rhsItems) else 1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
45 else: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
46 return diff |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
47 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
48 elif self.value < other.value: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
49 return -1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
50 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
51 else: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
52 return 1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
53 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
54 def __eq__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
55 return self.__cmp__(other) == 0 |
| 6047 | 56 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
57 def __ne__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
58 return self.__cmp__(other) != 0 |
| 6047 | 59 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
60 def __lt__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
61 return self.__cmp__(other) < 0 |
| 6047 | 62 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
63 def __le__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
64 return self.__cmp__(other) <= 0 |
| 6047 | 65 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
66 def __ge__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
67 return self.__cmp__(other) >= 0 |
| 6047 | 68 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
69 def __gt__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
70 return self.__cmp__(other) > 0 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
71 |
| 6047 | 72 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
73 def _test(): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
74 Comp = NoneAndDictComparable |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
75 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
76 assert Comp(None) < Comp(0) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
77 assert Comp(None) < Comp('') |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
78 assert Comp(None) < Comp({}) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
79 assert Comp((0, None)) < Comp((0, 0)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
80 assert not Comp(0) < Comp(None) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
81 assert not Comp('') < Comp(None) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
82 assert not Comp({}) < Comp(None) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
83 assert not Comp((0, 0)) < Comp((0, None)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
84 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
85 assert Comp((0, 0)) < Comp((0, 0, None)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
86 assert Comp((0, None, None)) < Comp((0, 0, 0)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
87 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
88 assert Comp(0) < Comp(1) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
89 assert Comp(1) > Comp(0) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
90 assert not Comp(1) < Comp(0) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
91 assert not Comp(0) > Comp(0) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
92 |
| 6047 | 93 assert Comp({0: None}) < Comp({0: 0}) |
| 94 assert Comp({0: 0}) < Comp({0: 1}) | |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
95 |
| 6047 | 96 assert Comp({0: 0}) == Comp({0: 0}) |
| 97 assert Comp({0: 0}) != Comp({0: 1}) | |
| 98 assert Comp({0: 0, 1: 1}) > Comp({0: 1}) | |
| 99 assert Comp({0: 0, 1: 1}) < Comp({0: 0, 2: 2}) | |
| 100 | |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
101 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
102 if __name__ == '__main__': |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
103 _test() |
