Skip to content

Commit 9195f76

Browse files
committed
Re-implement current functionality to be a bit more concise
1 parent 972a262 commit 9195f76

1 file changed

Lines changed: 21 additions & 35 deletions

File tree

cassandra/util.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,24 +1709,26 @@ def __init__(self, version):
17091709
self.minor = int(match[2])
17101710

17111711
try:
1712-
self.patch = self._cleanup(match[3])
1712+
self.patch = self._cleanup_int(match[3])
17131713
except:
17141714
self.patch = 0
17151715

17161716
try:
1717-
self.build = self._cleanup(match[4])
1717+
self.build = self._cleanup_int(match[4])
17181718
except:
17191719
self.build = 0
17201720

17211721
try:
1722-
self.prerelease = self._cleanup_prerelease(match[5])
1722+
self.prerelease = self._cleanup_str(match[5])
17231723
except:
17241724
self.prerelease = 0
17251725

1726-
def _cleanup(self, s):
1726+
# Trim off the leading '.' characters and convert the discovered value to an integer
1727+
def _cleanup_int(self, s):
17271728
return int(s[1:]) if s else 0
17281729

1729-
def _cleanup_prerelease(self, str):
1730+
# Trim off the leading '.' or '~' characters and just return the string directly
1731+
def _cleanup_str(self, str):
17301732
return str[1:] if str else 0
17311733

17321734
def __hash__(self):
@@ -1745,48 +1747,32 @@ def __repr__(self):
17451747
def __str__(self):
17461748
return self._version
17471749

1748-
@staticmethod
1749-
def _compare_version_part(version, other_version, cmp):
1750-
if not (isinstance(version, int) and
1751-
isinstance(other_version, int)):
1752-
version = str(version)
1753-
other_version = str(other_version)
1754-
1755-
return cmp(version, other_version)
1756-
17571750
def __eq__(self, other):
17581751
if not isinstance(other, Version):
17591752
return NotImplemented
17601753

17611754
return (self.major == other.major and
17621755
self.minor == other.minor and
17631756
self.patch == other.patch and
1764-
self._compare_version_part(self.build, other.build, lambda s, o: s == o) and
1765-
self._compare_version_part(self.prerelease, other.prerelease, lambda s, o: s == o)
1757+
self.build == other.build and
1758+
self.prerelease == other.prerelease
17661759
)
17671760

17681761
def __gt__(self, other):
17691762
if not isinstance(other, Version):
17701763
return NotImplemented
17711764

1772-
is_major_ge = self.major >= other.major
1773-
is_minor_ge = self.minor >= other.minor
1774-
is_patch_ge = self.patch >= other.patch
1775-
is_build_gt = self._compare_version_part(self.build, other.build, lambda s, o: s > o)
1776-
is_build_ge = self._compare_version_part(self.build, other.build, lambda s, o: s >= o)
1777-
1778-
# By definition, a prerelease comes BEFORE the actual release, so if a version
1779-
# doesn't have a prerelease, it's automatically greater than anything that does
1780-
if self.prerelease and not other.prerelease:
1781-
is_prerelease_gt = False
1765+
if self.major != other.major:
1766+
return self.major > other.major
1767+
elif self.minor != other.minor:
1768+
return self.minor > other.minor
1769+
elif self.patch != other.patch:
1770+
return self.patch > other.patch
1771+
elif self.build != other.build:
1772+
return self.build > other.build
1773+
elif self.prerelease and not other.prerelease:
1774+
return False
17821775
elif other.prerelease and not self.prerelease:
1783-
is_prerelease_gt = True
1776+
return True
17841777
else:
1785-
is_prerelease_gt = self._compare_version_part(self.prerelease, other.prerelease, lambda s, o: s > o) \
1786-
1787-
return (self.major > other.major or
1788-
(is_major_ge and self.minor > other.minor) or
1789-
(is_major_ge and is_minor_ge and self.patch > other.patch) or
1790-
(is_major_ge and is_minor_ge and is_patch_ge and is_build_gt) or
1791-
(is_major_ge and is_minor_ge and is_patch_ge and is_build_ge and is_prerelease_gt)
1792-
)
1778+
return self.prerelease > other.prerelease

0 commit comments

Comments
 (0)