diff options
author | David Timber <dxdt@dev.snart.me> | 2024-11-14 04:57:20 +0100 |
---|---|---|
committer | David Timber <dxdt@dev.snart.me> | 2024-11-14 05:03:42 +0100 |
commit | 03e678b8e3b643a29afcb0432dbe7ab3d4fcd78f (patch) | |
tree | cb32b307aa4c3726eaa92fd9a6e2130d9d2c94d9 /common |
Initial commit
Diffstat (limited to 'common')
-rw-r--r-- | common/__init__.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 00000000..5770ec96 --- /dev/null +++ b/common/__init__.py @@ -0,0 +1,27 @@ +import math +from typing import Any, Callable + +def load_data_from_file (path: str, mask_noent = True) -> dict[str, float]: + try: + with open(path) as f: + return load_data(f.readline) + except FileNotFoundError as e: + if not mask_noent: + raise e + + return dict[str, float]() + +def load_data (read_f: Callable) -> dict[str, float]: + ret = dict[str, float]() + + while True: + l = read_f() + if not l: + break + + tpl = l.strip().split() + v = float(tpl[1]) + if math.isfinite(v): + ret[tpl[0]] = v + + return ret |