X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=persistent.py;h=b42a5c0e3036a35d76dc0c8f32374d029e94fd53;hb=c08841600a2f61049cdb1a152407a1fb8ca129a5;hp=c902313eb4a28cb4635779f584f8775a2b646d35;hpb=e8fbbb7306430478dec55d2c963eed116d8330cc;p=python_utils.git diff --git a/persistent.py b/persistent.py index c902313..b42a5c0 100644 --- a/persistent.py +++ b/persistent.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +# © Copyright 2021-2022, Scott Gasch + """A Persistent is just a class with a load and save method. This module defines the Persistent base and a decorator that can be used to create a persistent singleton that autoloads and autosaves.""" @@ -62,7 +64,23 @@ class Persistent(ABC): def was_file_written_today(filename: str) -> bool: - """Returns True if filename was written today.""" + """Returns True if filename was written today. + + >>> import os + >>> filename = f'/tmp/testing_persistent_py_{os.getpid()}' + >>> os.system(f'touch {filename}') + 0 + >>> was_file_written_today(filename) + True + >>> os.system(f'touch -d 1974-04-15T01:02:03.99 {filename}') + 0 + >>> was_file_written_today(filename) + False + >>> os.system(f'/bin/rm -f {filename}') + 0 + >>> was_file_written_today(filename) + False + """ if not file_utils.does_file_exist(filename): return False @@ -80,7 +98,22 @@ def was_file_written_within_n_seconds( """Returns True if filename was written within the pas limit_seconds seconds. + >>> import os + >>> filename = f'/tmp/testing_persistent_py_{os.getpid()}' + >>> os.system(f'touch {filename}') + 0 + >>> was_file_written_within_n_seconds(filename, 60) + True + >>> import time + >>> time.sleep(2.0) + >>> was_file_written_within_n_seconds(filename, 2) + False + >>> os.system(f'/bin/rm -f {filename}') + 0 + >>> was_file_written_within_n_seconds(filename, 60) + False """ + if not file_utils.does_file_exist(filename): return False @@ -167,3 +200,9 @@ class persistent_autoloaded_singleton(object): return self.instance return _load + + +if __name__ == '__main__': + import doctest + + doctest.testmod()