annotate roundup/anypy/cmp_.py @ 7558:c8a931aa7514

fix(i18n): issue2551184 - improve i18n handling Apply patch to make sure that the dates tests use the locale files in the deployed tracker and not other roundup files.
author Marcus Preisch
date Thu, 20 Jul 2023 20:08:28 -0400
parents 706f3be6ffbb
children b0c459d1e2c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
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():
7550
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
74 import sys
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
75 _py3 = sys.version_info[0] > 2
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
76
5481
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
77 Comp = NoneAndDictComparable
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
78
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
79 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
80 assert Comp(None) < Comp('')
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
81 assert Comp(None) < Comp({})
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
82 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
83 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
84 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
85 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
86 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
87
7550
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
88 try:
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
89 not Comp("") < Comp((0, None))
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
90 if _py3:
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
91 assert False, "Incompatible types are reporting comparable."
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
92 except TypeError:
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
93 pass
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
94
5481
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
95 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
96 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
97
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
98 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
99 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
100 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
101 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
102
7550
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
103
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
104 assert Comp(0) <= Comp(1)
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
105 assert Comp(1) >= Comp(0)
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
106 assert not Comp(1) <= Comp(0)
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
107 assert Comp(0) >= Comp(0)
706f3be6ffbb Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents: 6047
diff changeset
108
6047
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
109 assert Comp({0: None}) < Comp({0: 0})
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
110 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
111
6047
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
112 assert Comp({0: 0}) == Comp({0: 0})
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
113 assert Comp({0: 0}) != Comp({0: 1})
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
114 assert Comp({0: 0, 1: 1}) > Comp({0: 1})
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
115 assert Comp({0: 0, 1: 1}) < Comp({0: 0, 2: 2})
142553f58694 flake8 whitepace fixes.
John Rouillard <rouilj@ieee.org>
parents: 5481
diff changeset
116
5481
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
117
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
118 if __name__ == '__main__':
9a09719b0d8e helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff changeset
119 _test()

Roundup Issue Tracker: http://roundup-tracker.org/