Skip to content

Commit

Permalink
Remove use of deprecated python 3.12 strtobool
Browse files Browse the repository at this point in the history
distutils is not available in python 3.12, so a substitute is needed
for the strtobool code.

Use same resolution as implemented by the ITK project.

I, Hans Johnson <[email protected]>, hereby add my Signed-off-by to
this commit: 89212f5

Signed-off-by: Hans Johnson <[email protected]>
  • Loading branch information
hjmjohnson committed Jul 2, 2024
1 parent 410109a commit 44f555a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions monai/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import warnings
from ast import literal_eval
from collections.abc import Callable, Iterable, Sequence
from distutils.util import strtobool
from math import log10
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypeVar, cast, overload
Expand Down Expand Up @@ -78,6 +77,24 @@
"run_cmd",
]

def _strtobool(val: str) -> bool:
"""
Replaces deprecated (pre python 3.12)
distutils strtobool function.
True values are y, yes, t, true, on and 1;
False values are n, no, f, false, off and 0.
Raises ValueError if val is anything else.
"""
val = val.lower()
if val in ("y", "yes", "t", "true", "on", "1"):
return 1
elif val in ("n", "no", "f", "false", "off", "0"):
return 0
else:
raise ValueError(f"invalid truth value {val}")


_seed = None
_flag_deterministic = torch.backends.cudnn.deterministic
_flag_cudnn_benchmark = torch.backends.cudnn.benchmark
Expand Down Expand Up @@ -400,7 +417,7 @@ def _parse_var(s):
d[key] = literal_eval(value)
except ValueError:
try:
d[key] = bool(strtobool(str(value)))
d[key] = bool(_strtobool(str(value)))
except ValueError:
d[key] = value
return d
Expand Down

0 comments on commit 44f555a

Please sign in to comment.