Skip to content

Commit

Permalink
fix: avoid usage of packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
jsolaas committed Jun 21, 2024
1 parent 8ea5b2f commit 4121f5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/libecalc/common/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import re
from typing import Optional

import packaging.version
from typing import Optional, Tuple

from libecalc.dto.base import EcalcBaseModel

Expand Down Expand Up @@ -54,38 +52,42 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"Major: {self.major}\nMinor: {self.minor}\nPatch: {self.patch}"

@property
def _version_tuple(self) -> Tuple[int, int, int]:
return self.major, self.minor, self.patch

def __gt__(self, other):
if not isinstance(other, Version):
return NotImplemented

return packaging.version.Version(str(self)).__gt__(packaging.version.Version(str(other)))
return self._version_tuple > other._version_tuple

def __ge__(self, other):
if not isinstance(other, Version):
return NotImplemented

return packaging.version.Version(str(self)).__ge__(packaging.version.Version(str(other)))
return self._version_tuple >= other._version_tuple

def __le__(self, other):
def __lt__(self, other):
if not isinstance(other, Version):
return NotImplemented

return packaging.version.Version(str(self)).__le__(packaging.version.Version(str(other)))
return self._version_tuple < other._version_tuple

def __lt__(self, other):
def __le__(self, other):
if not isinstance(other, Version):
return NotImplemented

return packaging.version.Version(str(self)).__lt__(packaging.version.Version(str(other)))
return self._version_tuple <= other._version_tuple

def __eq__(self, other):
if not isinstance(other, Version):
return NotImplemented

return packaging.version.Version(str(self)).__eq__(packaging.version.Version(str(other)))
return self._version_tuple == other._version_tuple

def __ne__(self, other):
if not isinstance(other, Version):
return NotImplemented

return packaging.version.Version(str(self)).__ne__(packaging.version.Version(str(other)))
return self._version_tuple != other._version_tuple
12 changes: 12 additions & 0 deletions src/tests/libecalc/common/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def test_versions(version_string, expected_version):
("1.1.1", "1.1.2"),
("1.1.2", "1.1.1"),
("1.1.1", "1.1.1"),
("100.1.1", "1.1.1"),
("1.1.1", "1.1.111"),
]


Expand All @@ -57,6 +59,8 @@ def to_version_obj(versions: List[Tuple[str, str]]) -> List[Tuple[Version, Versi
True,
False,
False,
False,
True,
]


Expand All @@ -73,6 +77,8 @@ def test_comparison_less_than(first, second, expected):
False,
True,
False,
True,
False,
]


Expand All @@ -89,6 +95,8 @@ def test_comparison_grater_than(first, second, expected):
False,
False,
True,
False,
False,
]


Expand All @@ -110,6 +118,8 @@ def test_comparison_not_equal(first, second):
True,
False,
True,
False,
True,
]


Expand All @@ -126,6 +136,8 @@ def test_comparison_less_than_or_equal(first, second, expected):
False,
True,
True,
True,
False,
]


Expand Down

0 comments on commit 4121f5b

Please sign in to comment.