-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_cmp.py
More file actions
67 lines (57 loc) · 2.38 KB
/
version_cmp.py
File metadata and controls
67 lines (57 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from itertools import zip_longest
class Version:
def __init__(self, version, delimiter="."):
self.version = version
self.delimiter = delimiter
def __eq__(self, other):
return self.version == other.version
def __ne__(self, other):
return self.version != other.version
def __lt__(self, other):
for i in zip_longest(self.version.split(self.delimiter), other.version.split(self.delimiter), fillvalue=-2147483648):
if int(i[0]) < int(i[1]):
return True
elif int(i[0]) > int(i[1]):
return False
return False
def __le__(self, other):
for i in zip_longest(self.version.split(self.delimiter), other.version.split(self.delimiter), fillvalue=-2147483648):
if int(i[0]) < int(i[1]):
return True
elif int(i[0]) > int(i[1]):
return False
return True
def __gt__(self, other):
for i in zip_longest(self.version.split(self.delimiter), other.version.split(self.delimiter), fillvalue=-2147483648):
if int(i[0]) > int(i[1]):
return True
elif int(i[0]) < int(i[1]):
return False
return False
def __ge__(self, other):
for i in zip_longest(self.version.split(self.delimiter), other.version.split(self.delimiter), fillvalue=-2147483648):
if int(i[0]) > int(i[1]):
return True
elif int(i[0]) < int(i[1]):
return False
return True
if __name__ == "__main__":
versions = [
("1.0", "1.1"),
("2.1", "2.1.2"),
("9.1", "10.0"),
("2.0.1234.5", "3.0"),
("2.0.124.5", "2.1"),
("2.123", "2.123"),
("3.5", "3.4"),
("4.0", "3.1"),
("40.1", "40.0.9")
]
for v in versions:
print(f"{str(v[0]):>10} == {str(v[1]):10}\t{str(Version(v[0]) == Version(v[1]))}")
print(f"{str(v[0]):>10} != {str(v[1]):10}\t{str(Version(v[0]) != Version(v[1]))}")
print(f"{str(v[0]):>10} < {str(v[1]):10}\t{str(Version(v[0]) < Version(v[1]))}")
print(f"{str(v[0]):>10} <= {str(v[1]):10}\t{str(Version(v[0]) <= Version(v[1]))}")
print(f"{str(v[0]):>10} > {str(v[1]):10}\t{str(Version(v[0]) > Version(v[1]))}")
print(f"{str(v[0]):>10} >= {str(v[1]):10}\t{str(Version(v[0]) >= Version(v[1]))}")
print()