Created
July 14, 2022 14:38
-
-
Save gsauthof/9a00f5fb7b45ac45fcc93b7582c7590b to your computer and use it in GitHub Desktop.
distutils.version.LooseVersion replacement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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