Towards a more type-clean mypy check.
authorScott Gasch <[email protected]>
Wed, 12 Oct 2022 18:16:55 +0000 (11:16 -0700)
committerScott Gasch <[email protected]>
Wed, 12 Oct 2022 18:16:55 +0000 (11:16 -0700)
src/pyutils/persistent.py
src/pyutils/remote_worker.py
src/pyutils/unscrambler.py

index 2b03ea6f4e29e496f6dfd9a75d92bd33047cd6c3..88234ccb8d33d8bfadf989162965049216693743 100644 (file)
@@ -88,6 +88,39 @@ class FileBasedPersistent(Persistent):
 
 
 class PicklingFileBasedPersistent(FileBasedPersistent):
+    """
+    Example usage:
+
+    import persistent
+
+    @persistent.persistent_autoloaded_singleton()
+    class MyClass(persistent.PicklingFileBasedPersistent):
+        def __init__(self, data: Whatever):
+            #initialize youself from data
+
+        @staticmethod
+        @overrides
+        def get_filename() -> str:
+            return "/path/to/where/you/want/to/save/data.bin"
+
+        @staticmethod
+        @overrides
+        def should_we_save_data(filename: str) -> bool:
+            return true_if_we_should_save_the_data_this_time()
+
+        @staticmethod
+        @overrides
+        def should_we_load_data(filename: str) -> bool:
+            return persistent.was_file_written_within_n_seconds(whatever)
+
+    # Persistent will handle the plumbing to instantiate your class from its
+    # persisted state iff the should_we_load_data says it's ok to.  It will
+    # also persist the current in memory state to disk at program exit iff
+    # the should_we_save_data methods says to.
+    c = MyClass()
+
+    """
+
     @classmethod
     @overrides
     def load(cls) -> Optional[Any]:
@@ -124,6 +157,39 @@ class PicklingFileBasedPersistent(FileBasedPersistent):
 
 
 class JsonFileBasedPersistent(FileBasedPersistent):
+    """
+    Example usage:
+
+    import persistent
+
+    @persistent.persistent_autoloaded_singleton()
+    class MyClass(persistent.JsonFileBasedPersistent):
+        def __init__(self, data: Whatever):
+            #initialize youself from data
+
+        @staticmethod
+        @overrides
+        def get_filename() -> str:
+            return "/path/to/where/you/want/to/save/data.json"
+
+        @staticmethod
+        @overrides
+        def should_we_save_data(filename: str) -> bool:
+            return true_if_we_should_save_the_data_this_time()
+
+        @staticmethod
+        @overrides
+        def should_we_load_data(filename: str) -> bool:
+            return persistent.was_file_written_within_n_seconds(whatever)
+
+    # Persistent will handle the plumbing to instantiate your class from its
+    # persisted state iff the should_we_load_data says it's ok to.  It will
+    # also persist the current in memory state to disk at program exit iff
+    # the should_we_save_data methods says to.
+    c = MyClass()
+
+    """
+
     @classmethod
     @overrides
     def load(cls) -> Any:
index cd6e4d6bdac48a47028ff26bbc1639a753276506..54f562da181d8d21b9891c8b99cfcc8e2dcd43fe 100755 (executable)
@@ -92,7 +92,9 @@ def cleanup_and_exit(
 @bootstrap.initialize
 def main() -> None:
     in_file = config.config['code_file']
+    assert in_file and type(in_file) == str
     out_file = config.config['result_file']
+    assert out_file and type(out_file) == str
 
     thread = None
     stop_thread = None
index cada2a03c5c5e967be02f1c4703b9d0108f5797f..847198d9212d78b89814c9431af6a1ce62187e0d 100644 (file)
@@ -130,6 +130,7 @@ class Unscrambler(object):
         if indexfile is None:
             if 'unscrambler_default_indexfile' in config.config:
                 indexfile = config.config['unscrambler_default_indexfile']
+                assert type(indexfile) == str
             else:
                 indexfile = "/usr/share/dict/sparse_index"
         else: