Skip to content

Commit

Permalink
Added to_float and to_integer to misc_utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Nov 20, 2023
1 parent 9b0df7a commit 4d40798
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions dcicutils/misc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,20 @@ def str_to_bool(x: Optional[str]) -> Optional[bool]:
raise ValueError(f"An argument to str_or_bool must be a string or None: {x!r}")


def to_integer(value: str, fallback: Optional[Any] = None) -> Optional[Any]:
try:
return int(value)
except Exception:
return fallback


def to_float(value: str, fallback: Optional[Any] = None) -> Optional[Any]:
try:
return float(value)
except Exception:
return fallback


@contextlib.contextmanager
def override_environ(**overrides):
"""
Expand Down

0 comments on commit 4d40798

Please sign in to comment.