summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-07-11 10:16:07 -0700
committerScott Gasch <[email protected]>2021-07-11 10:16:07 -0700
commita838c154135b2420d9047a101caf24a2c9f593c2 (patch)
treefbb0f6a0e66b4826352003d67f6e20bbcda0dda4
parent7d7a3ce0abb26766e82c6dfed8a196baa63b736a (diff)
Random cleanups and type safety. Created ml subdir.
-rw-r--r--acl.py6
-rw-r--r--config.py2
-rw-r--r--conversion_utils.py14
-rw-r--r--datetime_utils.py3
-rw-r--r--dict_utils.py4
-rw-r--r--logging_utils.py4
-rw-r--r--ml/model_trainer.py (renamed from ml_model_trainer.py)0
-rw-r--r--ml/quick_label.py (renamed from ml_quick_label.py)0
-rw-r--r--string_utils.py8
-rwxr-xr-xtests/parallelize_test.py2
-rwxr-xr-xtests/run_all_tests.sh2
11 files changed, 24 insertions, 21 deletions
diff --git a/acl.py b/acl.py
index e6bb903..9155090 100644
--- a/acl.py
+++ b/acl.py
@@ -5,7 +5,7 @@ import enum
import fnmatch
import logging
import re
-from typing import Any, Callable, List, Optional, Set
+from typing import Any, Callable, List, Optional, Set, Sequence
# This module is commonly used by others in here and should avoid
# taking any unnecessary dependencies back on them.
@@ -134,8 +134,8 @@ class PredicateListBasedACL(SimpleACL):
"""An ACL that allows or denies by applying predicates."""
def __init__(self,
*,
- allow_predicate_list: List[Callable[[Any], bool]] = None,
- deny_predicate_list: List[Callable[[Any], bool]] = None,
+ allow_predicate_list: Sequence[Callable[[Any], bool]] = None,
+ deny_predicate_list: Sequence[Callable[[Any], bool]] = None,
order_to_check_allow_deny: Order,
default_answer: bool) -> None:
super().__init__(
diff --git a/config.py b/config.py
index e7094f3..672e1ae 100644
--- a/config.py
+++ b/config.py
@@ -161,7 +161,7 @@ def parse() -> Dict[str, Any]:
"""Main program should call this early in main()"""
global config_parse_called
if config_parse_called:
- return
+ return config
config_parse_called = True
global saved_messages
diff --git a/conversion_utils.py b/conversion_utils.py
index 10908c5..b83d62e 100644
--- a/conversion_utils.py
+++ b/conversion_utils.py
@@ -15,15 +15,15 @@ class Converter(object):
unit: str) -> None:
self.name = name
self.category = category
- self.to_canonical = to_canonical
- self.from_canonical = from_canonical
+ self.to_canonical_f = to_canonical
+ self.from_canonical_f = from_canonical
self.unit = unit
def to_canonical(self, n: Number) -> Number:
- return self.to_canonical(n)
+ return self.to_canonical_f(n)
def from_canonical(self, n: Number) -> Number:
- return self.from_canonical(n)
+ return self.from_canonical_f(n)
def unit_suffix(self) -> str:
return self.unit
@@ -75,7 +75,7 @@ conversion_catalog = {
def convert(magnitude: Number,
from_thing: str,
- to_thing: str) -> Number:
+ to_thing: str) -> float:
src = conversion_catalog.get(from_thing, None)
dst = conversion_catalog.get(to_thing, None)
if src is None or dst is None:
@@ -87,10 +87,10 @@ def convert(magnitude: Number,
def _convert(magnitude: Number,
from_unit: Converter,
- to_unit: Converter) -> Number:
+ to_unit: Converter) -> float:
canonical = from_unit.to_canonical(magnitude)
converted = to_unit.from_canonical(canonical)
- return converted
+ return float(converted)
def sec_to_min(s: float) -> float:
diff --git a/datetime_utils.py b/datetime_utils.py
index 795b427..f2cae8b 100644
--- a/datetime_utils.py
+++ b/datetime_utils.py
@@ -80,13 +80,10 @@ class TimeUnit(enum.Enum):
@classmethod
def is_valid(cls, value: Any):
if type(value) is int:
- print("int")
return value in cls._value2member_map_
elif type(value) is TimeUnit:
- print("TimeUnit")
return value.value in cls._value2member_map_
elif type(value) is str:
- print("str")
return value in cls._member_names_
else:
print(type(value))
diff --git a/dict_utils.py b/dict_utils.py
index 0a2df25..292b933 100644
--- a/dict_utils.py
+++ b/dict_utils.py
@@ -39,9 +39,9 @@ def raise_on_duplicated_keys(key, v1, v2):
def coalesce(
inputs: Iterator[Dict[Any, Any]],
*,
- aggregation_function: Callable[[Any, Any, Any], Any] = coalesce_by_creating_list
+ aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
) -> Dict[Any, Any]:
- out = {}
+ out: Dict[Any, Any] = {}
for d in inputs:
for key in d:
if key in out:
diff --git a/logging_utils.py b/logging_utils.py
index 700bfab..9c78f3f 100644
--- a/logging_utils.py
+++ b/logging_utils.py
@@ -102,7 +102,9 @@ class MillisecondAwareFormatter(logging.Formatter):
converter = datetime.datetime.fromtimestamp
def formatTime(self, record, datefmt=None):
- ct = self.converter(record.created, pytz.timezone("US/Pacific"))
+ ct = MillisecondAwareFormatter.converter(
+ record.created, pytz.timezone("US/Pacific")
+ )
if datefmt:
s = ct.strftime(datefmt)
else:
diff --git a/ml_model_trainer.py b/ml/model_trainer.py
index ab3059f..ab3059f 100644
--- a/ml_model_trainer.py
+++ b/ml/model_trainer.py
diff --git a/ml_quick_label.py b/ml/quick_label.py
index 1ed4296..1ed4296 100644
--- a/ml_quick_label.py
+++ b/ml/quick_label.py
diff --git a/string_utils.py b/string_utils.py
index 740a0b9..911008d 100644
--- a/string_utils.py
+++ b/string_utils.py
@@ -225,10 +225,14 @@ def strip_escape_sequences(in_str: str) -> str:
return in_str
-def add_thousands_separator(in_str: str, *, separator_char = ',', places = 3) -> str:
+def add_thousands_separator(
+ in_str: str,
+ *,
+ separator_char = ',',
+ places = 3
+) -> str:
if isinstance(in_str, int):
in_str = f'{in_str}'
-
if is_number(in_str):
return _add_thousands_separator(
in_str,
diff --git a/tests/parallelize_test.py b/tests/parallelize_test.py
index 051ec5d..44f723c 100755
--- a/tests/parallelize_test.py
+++ b/tests/parallelize_test.py
@@ -22,7 +22,7 @@ def list_primes(n):
@decorator_utils.timed
def driver() -> None:
results = {}
- for _ in range(200):
+ for _ in range(50):
n = random.randint(0, 100000)
results[n] = list_primes(n)
tot = 0
diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh
index ecb3648..13aa2fb 100755
--- a/tests/run_all_tests.sh
+++ b/tests/run_all_tests.sh
@@ -1,6 +1,6 @@
#!/bin/bash
for test in $(ls *_test.py); do
- echo "------------------------------ ${test} ------------------------------"
+ echo "------------------------- ${test} -------------------------"
${test}
done