Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,22 @@ def get_torch_version_tuple():
return tuple(int(x) for x in torch.__version__.split(".")[:2])


def version_leq(lhs, rhs):
"""Returns True if version `lhs` is earlier or equal to `rhs`."""
def version_leq(lhs: str, rhs: str):
"""
Returns True if version `lhs` is earlier or equal to `rhs`.

Args:
lhs: version name to compare with `rhs`, return True if earlier or equal to `rhs`.
rhs: version name to compare with `lhs`, return True if later or equal to `lhs`.

"""

lhs, rhs = str(lhs), str(rhs)
ver, has_ver = optional_import("pkg_resources", name="parse_version")
if has_ver:
return ver(lhs) <= ver(rhs)

def _try_cast(val):
def _try_cast(val: str):
val = val.strip()
try:
m = match("(\\d+)(.*)", val)
Expand All @@ -390,10 +398,10 @@ def _try_cast(val):
rhs = rhs.split("+", 1)[0]

# parse the version strings in this basic way without `packaging` package
lhs = map(_try_cast, lhs.split("."))
rhs = map(_try_cast, rhs.split("."))
lhs_ = map(_try_cast, lhs.split("."))
rhs_ = map(_try_cast, rhs.split("."))

for l, r in zip(lhs, rhs):
for l, r in zip(lhs_, rhs_):
if l != r:
if isinstance(l, int) and isinstance(r, int):
return l < r
Expand Down
3 changes: 3 additions & 0 deletions tests/test_version_leq.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def _pairwise(iterable):
("0post1", "0.4post1"),
("2.1.0-rc1", "2.1.0"),
("2.1dev", "2.1a0"),
(1.6, "1.6.0"),
("1.6.0", 1.6),
(1.6, 1.7),
) + tuple(_pairwise(reversed(torture.split())))


Expand Down