Skip to content

Commit

Permalink
strtobool should not fail
Browse files Browse the repository at this point in the history
  • Loading branch information
vladsavelyev committed Feb 22, 2024
1 parent 5ef7c88 commit 5cfcf2f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
22 changes: 0 additions & 22 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,3 @@
],
)
logging.debug(f"Logging to {log_path}")


def strtobool(val) -> bool:
"""
Replaces deprecated https://docs.python.org/3.9/distutils/apiref.html#distutils.util.strtobool
The deprecation recommendation is to re-implement the function https://peps.python.org/pep-0632/
------------------------------------------------------------
Convert a string representation of truth to true (1) or false (0).
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_str = str(val).lower()
if val_str in ("y", "yes", "t", "true", "on", "1"):
return True
elif val_str in ("n", "no", "f", "false", "off", "0"):
return False
else:
raise ValueError(f"invalid truth value {val!r}")
14 changes: 9 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from plotly.graph_objs import Layout
from sqlalchemy.exc import ProgrammingError

from app import __version__, db, models, strtobool
from app import __version__, db, models
from app.downloads import daily

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -212,10 +212,14 @@ def _summarize_visits(interval="5min") -> Response:
df["end"] = df["start"] + pd.to_timedelta(interval)
df["start"] = df["start"].dt.strftime("%Y-%m-%d %H:%M")
df["end"] = df["end"].dt.strftime("%Y-%m-%d %H:%M")
df["is_docker"] = df["is_docker"].apply(lambda val: strtobool(val) if val else False)
df["is_singularity"] = df["is_singularity"].apply(lambda val: strtobool(val) if val else False)
df["is_conda"] = df["is_conda"].apply(lambda val: strtobool(val) if val else False)
df["is_ci"] = df["is_ci"].apply(lambda val: strtobool(val) if val else False)

def strtobool(val) -> bool:
return str(val).lower() in ("y", "yes", "t", "true", "on", "1")

df["is_docker"] = df["is_docker"].apply(strtobool)
df["is_singularity"] = df["is_singularity"].apply(strtobool)
df["is_conda"] = df["is_conda"].apply(strtobool)
df["is_ci"] = df["is_ci"].apply(strtobool)
df = df.drop(columns=["timestamp"])

# Summarize visits per user per time interval
Expand Down

0 comments on commit 5cfcf2f

Please sign in to comment.