From 5cfcf2f725db9236a1e5ffde1296810458c9024a Mon Sep 17 00:00:00 2001 From: vladsaveliev Date: Thu, 22 Feb 2024 22:07:39 +0100 Subject: [PATCH] strtobool should not fail --- app/__init__.py | 22 ---------------------- app/main.py | 14 +++++++++----- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index da06d20..7bf6a7e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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}") diff --git a/app/main.py b/app/main.py index 29d1bbb..d7ea116 100644 --- a/app/main.py +++ b/app/main.py @@ -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__) @@ -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