From: Scott Gasch Date: Wed, 12 Oct 2022 18:16:55 +0000 (-0700) Subject: Towards a more type-clean mypy check. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=bbf12a55d8bab1e3e12a13d10f7588cd7cf17a74;p=pyutils.git Towards a more type-clean mypy check. --- diff --git a/src/pyutils/persistent.py b/src/pyutils/persistent.py index 2b03ea6..88234cc 100644 --- a/src/pyutils/persistent.py +++ b/src/pyutils/persistent.py @@ -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: diff --git a/src/pyutils/remote_worker.py b/src/pyutils/remote_worker.py index cd6e4d6..54f562d 100755 --- a/src/pyutils/remote_worker.py +++ b/src/pyutils/remote_worker.py @@ -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 diff --git a/src/pyutils/unscrambler.py b/src/pyutils/unscrambler.py index cada2a0..847198d 100644 --- a/src/pyutils/unscrambler.py +++ b/src/pyutils/unscrambler.py @@ -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: