Skip to content

Instantly share code, notes, and snippets.

@bmwalters
Created December 24, 2016 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmwalters/35190589ad939c23af870970de7543df to your computer and use it in GitHub Desktop.
Save bmwalters/35190589ad939c23af870970de7543df to your computer and use it in GitHub Desktop.
class SimpleVersion():
def __init__(self, major, minor, patch):
self.major = major
self.minor = minor
self.patch = patch
@classmethod
def fromstring(c, version_str):
split = [int(x or "0") for x in version_str.split(".")]
while len(split) < 3:
split.append(0)
return c(split[0], split[1], split[2])
def __lt__(self, other):
if self.major != other.major:
return self.major < other.major
elif self.minor != other.minor:
return self.minor < other.minor
elif self.patch != other.patch:
return self.patch < other.patch
return False
def __gt__(self, other):
if self.major != other.major:
return self.major > other.major
elif self.minor != other.minor:
return self.minor > other.minor
elif self.patch != other.patch:
return self.patch > other.patch
return False
def __eq__(self, other):
return self.major == other.major and self.minor == other.minor and self.patch == other.patch
def __le__(self, other):
return self.__eq__(other) or self.__lt__(other)
def __ge__(self, other):
return self.__eq__(other) or self.__gt__(other)
def __str__(self):
return ".".join([str(self.major), str(self.minor), str(self.patch)])
def __repr__(self):
return "SimpleVersion({}, {}, {})".format(self.major, self.minor, self.patch)
def __hash__(self):
return hash((self.major, self.minor, self.patch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment