Mercurial > p > roundup > code
annotate roundup/anypy/cmp_.py @ 8540:e8d1da6e3571
bug: fix traceback in roundup-admin init with bad config values
initialize accepts setting values for config.ini file settings. If
they are not valid, you got a python traceback.
ConfigurationError exceptions are now trapped. The admin.py's
usageError_feedback method is used to inform the user. Also the
feedback message now starts with a newline making it easier to read by
separating it from command that caused the issue.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 23 Mar 2026 13:18:41 -0400 |
| parents | dec7de56f253 |
| children |
| 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: |
|
7765
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
2 # will raise TypeError under python 3 |
|
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
3 None < 0 # noqa: PLR0133, B015 |
| 6047 | 4 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
5 def NoneAndDictComparable(v): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
6 return v |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
7 except TypeError: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
8 # 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
|
9 # 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
|
10 # in Python 3 any more) |
|
7765
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
11 class NoneAndDictComparable(object): # noqa: PLW1641 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
12 def __init__(self, value): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
13 self.value = value |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
14 |
|
7765
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
15 def __cmp__(self, other): # noqa: PLW3201, PLR0911 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
16 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
|
17 raise TypeError('not comparable') |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
18 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
19 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
|
20 return 0 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
21 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
22 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
|
23 return -1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
24 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
25 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
|
26 return 1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
27 |
|
7765
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
28 elif type(self.value) is tuple and type(other.value) is tuple: |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
29 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
|
30 lhsCmp = NoneAndDictComparable(lhs) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
31 rhsCmp = NoneAndDictComparable(rhs) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
32 result = lhsCmp.__cmp__(rhsCmp) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
33 if result != 0: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
34 return result |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
35 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
36 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
|
37 |
|
7765
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
38 elif type(self.value) is dict and type(other.value) is dict: |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
39 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
|
40 if diff == 0: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
41 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
|
42 key=NoneAndDictComparable)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
43 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
|
44 key=NoneAndDictComparable)) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
45 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
|
46 else: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
47 return diff |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
48 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
49 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
|
50 return -1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
51 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
52 else: |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
53 return 1 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
54 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
55 def __eq__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
56 return self.__cmp__(other) == 0 |
| 6047 | 57 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
58 def __ne__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
59 return self.__cmp__(other) != 0 |
| 6047 | 60 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
61 def __lt__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
62 return self.__cmp__(other) < 0 |
| 6047 | 63 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
64 def __le__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
65 return self.__cmp__(other) <= 0 |
| 6047 | 66 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
67 def __ge__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
68 return self.__cmp__(other) >= 0 |
| 6047 | 69 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
70 def __gt__(self, other): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
71 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
|
72 |
| 6047 | 73 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
74 def _test(): |
|
7765
dec7de56f253
chore: more ruff lint stuff
John Rouillard <rouilj@ieee.org>
parents:
7570
diff
changeset
|
75 # ruff: noqa: S101, B011, PLC0415, PLR2004 |
|
7550
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
76 import sys |
|
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
77 _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
|
78 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
79 Comp = NoneAndDictComparable |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
80 |
|
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(0) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
82 assert Comp(None) < Comp('') |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
83 assert Comp(None) < Comp({}) |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 |
|
7550
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
90 try: |
|
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
91 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
|
92 if _py3: |
|
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
93 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
|
94 except TypeError: |
|
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
95 pass |
|
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
96 |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
97 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
|
98 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
|
99 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 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
|
104 |
|
7550
706f3be6ffbb
Add tests for <= >= and check exception is raised for python3.
John Rouillard <rouilj@ieee.org>
parents:
6047
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 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
|
109 |
| 6047 | 110 assert Comp({0: None}) < Comp({0: 0}) |
| 111 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
|
112 |
| 6047 | 113 assert Comp({0: 0}) == Comp({0: 0}) |
| 114 assert Comp({0: 0}) != Comp({0: 1}) | |
| 115 assert Comp({0: 0, 1: 1}) > Comp({0: 1}) | |
| 116 assert Comp({0: 0, 1: 1}) < Comp({0: 0, 2: 2}) | |
| 117 | |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
118 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
119 if __name__ == '__main__': |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
diff
changeset
|
120 _test() |
