Random cleanups and type safety. Created ml subdir.
authorScott Gasch <[email protected]>
Sun, 11 Jul 2021 17:16:07 +0000 (10:16 -0700)
committerScott Gasch <[email protected]>
Sun, 11 Jul 2021 17:16:07 +0000 (10:16 -0700)
acl.py
config.py
conversion_utils.py
datetime_utils.py
dict_utils.py
logging_utils.py
ml/model_trainer.py [moved from ml_model_trainer.py with 100% similarity]
ml/quick_label.py [moved from ml_quick_label.py with 100% similarity]
string_utils.py
tests/parallelize_test.py
tests/run_all_tests.sh

diff --git a/acl.py b/acl.py
index e6bb9033f89319eb986adab2574a59171dbe899d..91550901e07df0a614027f90a6d15fa803fde877 100644 (file)
--- 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__(
index e7094f3a5ba6d6c4646a579d2e5e1e47cc8e330a..672e1ae0328e6d7e5b99ccbc4d702adfcd516f32 100644 (file)
--- 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
 
index 10908c54637c143ab6d58da1db95c4ec451c5e6e..b83d62e4daf50ec76850e047672069a8e9f47c7b 100644 (file)
@@ -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:
index 795b427c31b4e57a4551aab26badd1f81b68c9a2..f2cae8b9f06b2f69e5132685f611e4aaceeb02d2 100644 (file)
@@ -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))
index 0a2df252b6e97dbe7baa2c636aab6da4f6da3652..292b933886d7b6b5cc80ec98f358ad1f29ae9abf 100644 (file)
@@ -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:
index 700bfabcf9b9bbba8b28ca199253de40296a6ccc..9c78f3f685aa6fa9c09f6a1a64e7186707384e75 100644 (file)
@@ -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:
similarity index 100%
rename from ml_model_trainer.py
rename to ml/model_trainer.py
similarity index 100%
rename from ml_quick_label.py
rename to ml/quick_label.py
index 740a0b960d1da84a4686bf535eb39f006d9d3b77..911008d4c93bc50d6d78bb7d09d9d4aaaffdbcd5 100644 (file)
@@ -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,
index 051ec5d89c3e1d53284405e74ec581e5eb8b49d1..44f723c2673c80376a3b2bc8a0675bdc5e293581 100755 (executable)
@@ -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
index ecb36480d94ad163f9cb67158eaf51b94e0216af..13aa2fbb2ab7dbc5cc6b963e3014919f6130999a 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/bash
 
 for test in $(ls *_test.py); do
-    echo "------------------------------ ${test} ------------------------------"
+    echo "------------------------- ${test} -------------------------"
     ${test}
 done