Skip to content

Instantly share code, notes, and snippets.

@gsauthof
Created July 14, 2022 14:38
Show Gist options
  • Save gsauthof/9a00f5fb7b45ac45fcc93b7582c7590b to your computer and use it in GitHub Desktop.
Save gsauthof/9a00f5fb7b45ac45fcc93b7582c7590b to your computer and use it in GitHub Desktop.
distutils.version.LooseVersion replacement
import operator
import re
split_version_re = re.compile('[.+-]')
class LooseVersion:
def __init__(self, s):
self.vs = split_version_re.split(s)
self.n = max(len(x) for x in self.vs)
def cmp(self, other, op):
n = max(self.n, other.n)
return op(tuple(x.zfill(n) for x in self.vs), tuple(x.zfill(n) for x in other.vs))
def __lt__(self, other):
return self.cmp(other, operator.lt)
def __le__(self, other):
return self.cmp(other, operator.le)
def __gt__(self, other):
return self.cmp(other, operator.gt)
def __ge__(self, other):
return self.cmp(other, operator.ge)
def __eq__(self, other):
return self.cmp(other, operator.eq)
def __ne__(self, other):
return self.cmp(other, operator.ne)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment