initial commit
Code Quality Pipeline / code-quality (push) Failing after 2m6s
Test Python Package / test (push) Failing after 15s
Type Check / type-check (push) Failing after 14s

This commit is contained in:
Brian Bjarke Jensen
2025-08-27 22:42:09 +02:00
parent 39d843d021
commit 95a8a29e2f
17 changed files with 807 additions and 0 deletions
View File
+33
View File
@@ -0,0 +1,33 @@
"""Definition of check_env function."""
import logging
import os
def check_env(
var_list: str | list[str],
missing_ok: bool = False,
) -> None:
"""Check if environment variables are set.
Args:
var_list (str | list[str]): A single environment variable name or a list of names to check.
missing_ok (bool): If True, do not raise an error if the variable is not set. Defaults to False.
Raises:
OSError: If any environment variable is not set.
"""
if not var_list:
raise ValueError("List of environment variable names is empty.")
# Handle single string input
if isinstance(var_list, str):
var_list = [var_list]
# Avoid double-checking
var_set = set(var_list)
# Perform checks
for var in var_set:
if not os.getenv(var):
msg = f"Environment variable {var} is not set."
logging.error(msg)
if not missing_ok:
raise OSError(msg)
+47
View File
@@ -0,0 +1,47 @@
"""Definition of readable_unit function."""
def readable_unit(value: float, unit: str) -> str:
"""Return the given number formatted as a readable string with the specified unit.
Args:
value (float): The numeric value to format.
unit (str): The unit to append to the formatted value.
Returns:
str: A string representation of the value with the unit, formatted to two decimal places.
"""
if not isinstance(value, (int, float)):
raise ValueError("Value must be a number.")
if value < 0:
raise ValueError("Value cannot be negative.")
if not isinstance(unit, str):
raise ValueError("Unit must be a string.")
if unit.strip() == "":
raise ValueError("Unit cannot be an empty string.")
# Stop early if value is less than 1
if value < 1:
return f"{value:.2f} {unit}"
# List of (suffix, factor), ordered from largest to smallest
factors = [
("Y", 2**80),
("Z", 2**70),
("E", 2**60),
("P", 2**50),
("T", 2**40),
("G", 2**30),
("M", 2**20),
("K", 2**10),
("", 1),
]
# Find the largest factor that fits the value
for suffix, factor in factors:
if factor <= value:
value /= factor
break
# Build response
return f"{value:.2f} {suffix}{unit}"