Skip to content

Commit

Permalink
add helper fn to get bool from bool, int or str
Browse files Browse the repository at this point in the history
The config file will supply all as str, so such a function will be useful
For example, this commit also modifies the Sonde class to import helper
and use this new function to check for the value of the skip argument by first
converting the config provided value to bool.
  • Loading branch information
Geet-George committed Nov 15, 2023
1 parent 8a2e7e0 commit c933a45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/halodrops/helper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def get_bool(s):
if isinstance(s, bool):
return s
elif isinstance(s, int):
return bool(s)
elif isinstance(s, str):
lower_s = s.lower()
if lower_s == "true":
return True
elif lower_s == "false":
return False
elif lower_s in ["0", "1"]:
return bool(int(lower_s))
else:
raise ValueError(f"Cannot convert {s} to boolean")
else:
raise ValueError(f"Cannot convert {s} to boolean")
5 changes: 3 additions & 2 deletions src/halodrops/sonde.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xarray as xr

from halodrops.helper import rawreader as rr
import halodrops.helper as hh

_no_default = object()

Expand Down Expand Up @@ -233,7 +234,7 @@ def weighted_fullness(
float
Fraction of non-nan variable values along time_dimension weighed for sampling frequency
"""
if skip:
if hh.get_bool(skip):
return self
else:
for variable, sampling_frequency in variable_dict.items():
Expand Down Expand Up @@ -276,7 +277,7 @@ def near_surface_coverage(
ValueError
If the attribute `aspen_ds` does not exist.
"""
if skip:
if hh.get_bool(skip):
return self
else:
if not hasattr(self, "aspen_ds"):
Expand Down

0 comments on commit c933a45

Please sign in to comment.