From a838c154135b2420d9047a101caf24a2c9f593c2 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sun, 11 Jul 2021 10:16:07 -0700 Subject: [PATCH] Random cleanups and type safety. Created ml subdir. --- acl.py | 6 +++--- config.py | 2 +- conversion_utils.py | 14 +++++++------- datetime_utils.py | 3 --- dict_utils.py | 4 ++-- logging_utils.py | 4 +++- ml_model_trainer.py => ml/model_trainer.py | 0 ml_quick_label.py => ml/quick_label.py | 0 string_utils.py | 8 ++++++-- tests/parallelize_test.py | 2 +- tests/run_all_tests.sh | 2 +- 11 files changed, 24 insertions(+), 21 deletions(-) rename ml_model_trainer.py => ml/model_trainer.py (100%) rename ml_quick_label.py => ml/quick_label.py (100%) 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 similarity index 100% rename from ml_model_trainer.py rename to ml/model_trainer.py diff --git a/ml_quick_label.py b/ml/quick_label.py similarity index 100% rename from ml_quick_label.py rename to 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 -- 2.46.0