cmp3 0.6.2__py3-none-any.whl → 0.6.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cmp3/core/__init__.py +13 -5
- cmp3/tests/test_cmp3_core.py +118 -0
- {cmp3-0.6.2.dist-info → cmp3-0.6.3.dist-info}/METADATA +1 -1
- cmp3-0.6.3.dist-info/RECORD +9 -0
- {cmp3-0.6.2.dist-info → cmp3-0.6.3.dist-info}/WHEEL +1 -1
- cmp3/tests/test_1984.py +0 -11
- cmp3-0.6.2.dist-info/RECORD +0 -9
- {cmp3-0.6.2.dist-info → cmp3-0.6.3.dist-info}/licenses/LICENSE.txt +0 -0
- {cmp3-0.6.2.dist-info → cmp3-0.6.3.dist-info}/top_level.txt +0 -0
cmp3/core/__init__.py
CHANGED
|
@@ -11,14 +11,22 @@ def cmp(x: Any, y: Any, /, *, mode: str = "portingguide") -> float | int:
|
|
|
11
11
|
if mode == "portingguide":
|
|
12
12
|
return (x > y) - (x < y)
|
|
13
13
|
if mode == "poset":
|
|
14
|
-
|
|
14
|
+
return cmp_poset(x, y)
|
|
15
|
+
raise ValueError("%r is not an appropriate value for mode." % mode)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def cmp_poset(x: Any, y: Any, /) -> float | int:
|
|
19
|
+
"This function returns a value that compares to 0 as x compares to y under the assumption that both arguments belong to a poset."
|
|
20
|
+
if x <= y:
|
|
21
|
+
if y <= x:
|
|
15
22
|
return 0
|
|
16
|
-
|
|
23
|
+
else:
|
|
17
24
|
return -1
|
|
18
|
-
|
|
25
|
+
else:
|
|
26
|
+
if y <= x:
|
|
19
27
|
return 1
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
else:
|
|
29
|
+
return float("nan")
|
|
22
30
|
|
|
23
31
|
|
|
24
32
|
def cmpDeco(cls: type, /) -> type:
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import unittest
|
|
3
|
+
from typing import *
|
|
4
|
+
|
|
5
|
+
from cmp3 import core
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestCmpPortingGuide(unittest.TestCase):
|
|
9
|
+
def test_equal(self: Self) -> None:
|
|
10
|
+
self.assertEqual(core.cmp(1, 1), 0)
|
|
11
|
+
self.assertEqual(core.cmp("a", "a"), 0)
|
|
12
|
+
|
|
13
|
+
def test_less(self: Self) -> None:
|
|
14
|
+
self.assertEqual(core.cmp(1, 2), -1)
|
|
15
|
+
self.assertEqual(core.cmp("a", "b"), -1)
|
|
16
|
+
|
|
17
|
+
def test_greater(self: Self) -> None:
|
|
18
|
+
self.assertEqual(core.cmp(2, 1), 1)
|
|
19
|
+
self.assertEqual(core.cmp("b", "a"), 1)
|
|
20
|
+
|
|
21
|
+
def test_invalid_mode_raises(self: Self) -> None:
|
|
22
|
+
with self.assertRaises(ValueError):
|
|
23
|
+
core.cmp(1, 2, mode="invalid-mode")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TestCmpPoset(unittest.TestCase):
|
|
27
|
+
def test_poset_equal(self: Self) -> None:
|
|
28
|
+
# subset ordering: same set
|
|
29
|
+
self.assertEqual(core.cmp_poset({1, 2}, {1, 2}), 0)
|
|
30
|
+
|
|
31
|
+
def test_poset_less(self: Self) -> None:
|
|
32
|
+
# subset ordering: strict subset
|
|
33
|
+
self.assertEqual(core.cmp_poset({1}, {1, 2}), -1)
|
|
34
|
+
|
|
35
|
+
def test_poset_greater(self: Self) -> None:
|
|
36
|
+
# subset ordering: strict superset
|
|
37
|
+
self.assertEqual(core.cmp_poset({1, 2}, {1}), 1)
|
|
38
|
+
|
|
39
|
+
def test_poset_incomparable(self: Self) -> None:
|
|
40
|
+
# incomparable in subset ordering
|
|
41
|
+
res = core.cmp_poset({1}, {2})
|
|
42
|
+
self.assertTrue(math.isnan(res))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class TestCmpModePoset(unittest.TestCase):
|
|
46
|
+
def test_cmp_uses_poset_mode(self: Self) -> None:
|
|
47
|
+
self.assertEqual(core.cmp({1}, {1, 2}, mode="poset"), -1)
|
|
48
|
+
self.assertEqual(core.cmp({1, 2}, {1}, mode="poset"), 1)
|
|
49
|
+
self.assertTrue(math.isnan(core.cmp({1}, {2}, mode="poset")))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Number(core.CmpABC):
|
|
53
|
+
def __init__(self: Self, value: int) -> None:
|
|
54
|
+
self.value = value
|
|
55
|
+
|
|
56
|
+
def __cmp__(self: Self, other: Any) -> Any:
|
|
57
|
+
if not isinstance(other, Number):
|
|
58
|
+
return NotImplemented
|
|
59
|
+
# classic 3-way comparison
|
|
60
|
+
return (self.value > other.value) - (self.value < other.value)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class TestCmpDecoAndCmpABC(unittest.TestCase):
|
|
64
|
+
def setUp(self: Self) -> None:
|
|
65
|
+
self.a = Number(1)
|
|
66
|
+
self.b = Number(2)
|
|
67
|
+
self.c = Number(1)
|
|
68
|
+
|
|
69
|
+
def test_eq_ne(self: Self) -> None:
|
|
70
|
+
self.assertTrue(self.a == self.c)
|
|
71
|
+
self.assertFalse(self.a == self.b)
|
|
72
|
+
self.assertTrue(self.a != self.b)
|
|
73
|
+
self.assertFalse(self.a != self.c)
|
|
74
|
+
|
|
75
|
+
def test_lt_le(self: Self) -> None:
|
|
76
|
+
self.assertTrue(self.a < self.b)
|
|
77
|
+
self.assertTrue(self.a <= self.b)
|
|
78
|
+
self.assertTrue(self.a <= self.c)
|
|
79
|
+
self.assertFalse(self.b < self.a)
|
|
80
|
+
|
|
81
|
+
def test_gt_ge(self: Self) -> None:
|
|
82
|
+
self.assertTrue(self.b > self.a)
|
|
83
|
+
self.assertTrue(self.b >= self.a)
|
|
84
|
+
self.assertTrue(self.b >= self.c)
|
|
85
|
+
self.assertFalse(self.a > self.b)
|
|
86
|
+
|
|
87
|
+
def test_cmpabc_is_abstract(self: Self) -> None:
|
|
88
|
+
with self.assertRaises(TypeError):
|
|
89
|
+
core.CmpABC() # abstract, cannot be instantiated
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class TestCmpDecoOnPlainClass(unittest.TestCase):
|
|
93
|
+
def test_decorator_on_non_abc_class(self: Self) -> None:
|
|
94
|
+
@core.cmpDeco
|
|
95
|
+
class Plain:
|
|
96
|
+
def __init__(self: Self, x: int) -> None:
|
|
97
|
+
self.x = x
|
|
98
|
+
|
|
99
|
+
def __cmp__(self: Self, other: Any) -> Any:
|
|
100
|
+
if not isinstance(other, Plain):
|
|
101
|
+
return NotImplemented
|
|
102
|
+
return (self.x > other.x) - (self.x < other.x)
|
|
103
|
+
|
|
104
|
+
p1: Plain
|
|
105
|
+
p2: Plain
|
|
106
|
+
p3: Plain
|
|
107
|
+
p1 = Plain(1)
|
|
108
|
+
p2 = Plain(2)
|
|
109
|
+
p3 = Plain(1)
|
|
110
|
+
|
|
111
|
+
self.assertTrue(p1 < p2)
|
|
112
|
+
self.assertTrue(p2 > p1)
|
|
113
|
+
self.assertTrue(p1 == p3)
|
|
114
|
+
self.assertTrue(p1 != p2)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
if __name__ == "__main__":
|
|
118
|
+
unittest.main()
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
cmp3/__init__.py,sha256=hxvwX8wZhft-DkMAoU3xz65RMZib8nPQQiAbxba27iQ,49
|
|
2
|
+
cmp3/core/__init__.py,sha256=_xdO8zsRzwDegppawfT3zohm6xSHt5KC7C9jELqRYXQ,2199
|
|
3
|
+
cmp3/tests/__init__.py,sha256=HqmxhnTFFqh9Nzl7STV5GeDYqDmhrc9e1TCHssUbC1s,426
|
|
4
|
+
cmp3/tests/test_cmp3_core.py,sha256=tiBhDM5uby7I_Z-AmCI90D67bwoURDPJqJjnOCwNpjA,3662
|
|
5
|
+
cmp3-0.6.3.dist-info/licenses/LICENSE.txt,sha256=Jb9jm2E2hEO2LqnWwHeHt4cneyCbnZ1SBte0bgb9qoY,1074
|
|
6
|
+
cmp3-0.6.3.dist-info/METADATA,sha256=I8ug4WZLWTG25xcMEuVWFCPMcjMm1Jj9JuoUY9w3Sig,2300
|
|
7
|
+
cmp3-0.6.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
8
|
+
cmp3-0.6.3.dist-info/top_level.txt,sha256=FzJWsf0Xj_eeRJuFle30ZFjVZs8ahy7oLhYdWx8YkgA,5
|
|
9
|
+
cmp3-0.6.3.dist-info/RECORD,,
|
cmp3/tests/test_1984.py
DELETED
cmp3-0.6.2.dist-info/RECORD
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
cmp3/__init__.py,sha256=hxvwX8wZhft-DkMAoU3xz65RMZib8nPQQiAbxba27iQ,49
|
|
2
|
-
cmp3/core/__init__.py,sha256=6uXJPePzO0rZVCYASVP2A3FU05F_87g1eTfkNdfOFCE,1947
|
|
3
|
-
cmp3/tests/__init__.py,sha256=HqmxhnTFFqh9Nzl7STV5GeDYqDmhrc9e1TCHssUbC1s,426
|
|
4
|
-
cmp3/tests/test_1984.py,sha256=_ami0cAP8vU0C8RpQhP1tLWnLFGTaeHlOG5O60RVKv8,222
|
|
5
|
-
cmp3-0.6.2.dist-info/licenses/LICENSE.txt,sha256=Jb9jm2E2hEO2LqnWwHeHt4cneyCbnZ1SBte0bgb9qoY,1074
|
|
6
|
-
cmp3-0.6.2.dist-info/METADATA,sha256=yUjZmwuPzD3MxbaB-8JgtyuXXXq-JJFIww7LKaO-cqI,2300
|
|
7
|
-
cmp3-0.6.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
8
|
-
cmp3-0.6.2.dist-info/top_level.txt,sha256=FzJWsf0Xj_eeRJuFle30ZFjVZs8ahy7oLhYdWx8YkgA,5
|
|
9
|
-
cmp3-0.6.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|