Various little changes. Naming. Durations as arguments.
authorScott Gasch <[email protected]>
Thu, 23 Sep 2021 00:37:17 +0000 (17:37 -0700)
committerScott Gasch <[email protected]>
Thu, 23 Sep 2021 00:37:17 +0000 (17:37 -0700)
argparse_utils.py
datetime_utils.py
persistent.py

index 0ee2be9f8017093caeff732962ad9b4d10a993e7..2d2297ccf2f8e7f9df99929925ab1b973ee5c44b 100644 (file)
@@ -232,6 +232,28 @@ def valid_datetime(txt: str) -> datetime.datetime:
     raise argparse.ArgumentTypeError(msg)
 
 
     raise argparse.ArgumentTypeError(msg)
 
 
+def valid_duration(txt: str) -> datetime.timedelta:
+    """If the string is a valid time duration, return a
+    datetime.timedelta representing the period of time.  Otherwise
+    maybe raise an ArgumentTypeError or potentially just treat the
+    time window as zero in length.
+
+    >>> valid_duration('3m')
+    datetime.timedelta(seconds=180)
+
+    >>> valid_duration('your mom')
+    datetime.timedelta(seconds=0)
+
+    """
+    from datetime_utils import parse_duration
+    try:
+        secs = parse_duration(txt)
+    except Exception as e:
+        raise argparse.ArgumentTypeError(e)
+    finally:
+        return datetime.timedelta(seconds=secs)
+
+
 if __name__ == '__main__':
     import doctest
     doctest.ELLIPSIS_MARKER = '-ANYTHING-'
 if __name__ == '__main__':
     import doctest
     doctest.ELLIPSIS_MARKER = '-ANYTHING-'
index db5b2b5e19ba8e74fdf192dc122e518e90ec6bf9..c100057d3a306a0efb482418479a3857dcc07b93 100644 (file)
@@ -652,6 +652,18 @@ def describe_duration(seconds: int, *, include_seconds = False) -> str:
     return descr
 
 
     return descr
 
 
+def describe_timedelta(delta: datetime.timedelta) -> str:
+    """
+    Describe a duration represented by a timedelta object.
+
+    >>> d = datetime.timedelta(1, 600)
+    >>> describe_timedelta(d)
+    '1 day, and 10 minutes'
+
+    """
+    return describe_duration(delta.total_seconds())
+
+
 def describe_duration_briefly(seconds: int, *, include_seconds=False) -> str:
     """
     Describe a duration briefly.
 def describe_duration_briefly(seconds: int, *, include_seconds=False) -> str:
     """
     Describe a duration briefly.
@@ -685,6 +697,18 @@ def describe_duration_briefly(seconds: int, *, include_seconds=False) -> str:
     return descr.strip()
 
 
     return descr.strip()
 
 
+def describe_timedelta_briefly(delta: datetime.timedelta) -> str:
+    """
+    Describe a duration represented by a timedelta object.
+
+    >>> d = datetime.timedelta(1, 600)
+    >>> describe_timedelta_briefly(d)
+    '1d 10m'
+
+    """
+    return describe_duration_briefly(delta.total_seconds())
+
+
 if __name__ == '__main__':
     import doctest
     doctest.testmod()
 if __name__ == '__main__':
     import doctest
     doctest.testmod()
index ecf39c40be7e578f8d5d3b29835058074594416d..0ba931521a8841968068a6c7d97eb78edd86f78c 100644 (file)
@@ -31,7 +31,7 @@ class Persistent(ABC):
 def reuse_if_mtime_is_today() -> Callable[[datetime.datetime], bool]:
     """
     A helper that returns a lambda appropriate for use in the
 def reuse_if_mtime_is_today() -> Callable[[datetime.datetime], bool]:
     """
     A helper that returns a lambda appropriate for use in the
-    persistent_autoload_singleton decorator's may_reuse_persisted
+    persistent_autoloaded_singleton decorator's may_reuse_persisted
     parameter that allows persisted state to be reused as long as it
     was persisted on the same day as the load.
 
     parameter that allows persisted state to be reused as long as it
     was persisted on the same day as the load.
 
@@ -49,7 +49,7 @@ def reuse_if_mtime_less_than_limit_sec(
 ) -> Callable[[datetime.datetime], bool]:
     """
     A helper that returns a lambda appropriate for use in the
 ) -> Callable[[datetime.datetime], bool]:
     """
     A helper that returns a lambda appropriate for use in the
-    persistent_autoload_singleton decorator's may_reuse_persisted
+    persistent_autoloaded_singleton decorator's may_reuse_persisted
     parameter that allows persisted state to be reused as long as it
     was persisted within the past limit_seconds.
 
     parameter that allows persisted state to be reused as long as it
     was persisted within the past limit_seconds.
 
@@ -58,6 +58,11 @@ def reuse_if_mtime_less_than_limit_sec(
     return lambda dt: (now - dt).total_seconds() <= limit_seconds
 
 
     return lambda dt: (now - dt).total_seconds() <= limit_seconds
 
 
+def dont_reuse_persisted_state_force_refresh(
+) -> Callable[[datetime.datetime], bool]:
+    return lambda dt: False
+
+
 class PersistAtShutdown(enum.Enum):
     """
     An enum to describe the conditions under which state is persisted
 class PersistAtShutdown(enum.Enum):
     """
     An enum to describe the conditions under which state is persisted
@@ -69,7 +74,7 @@ class PersistAtShutdown(enum.Enum):
     ALWAYS = 2,
 
 
     ALWAYS = 2,
 
 
-class persistent_autoload_singleton(Persistent):
+class persistent_autoloaded_singleton(Persistent):
     """This class is meant to be used as a decorator around a class that:
 
         1. Is a singleton; one global instance per python program.
     """This class is meant to be used as a decorator around a class that:
 
         1. Is a singleton; one global instance per python program.
@@ -80,7 +85,7 @@ class persistent_autoload_singleton(Persistent):
 
     Here's and example usage pattern:
 
 
     Here's and example usage pattern:
 
-        @persistent_autoload_singleton(
+        @persistent_autoloaded_singleton(
             filename = "my_cache_file.bin",
             may_reuse_persisted = reuse_if_mtime_less_than_limit_sec(60),
             persist_at_shutdown = PersistAtShutdown.IF_NOT_INITIALIZED_FROM_DISK,
             filename = "my_cache_file.bin",
             may_reuse_persisted = reuse_if_mtime_less_than_limit_sec(60),
             persist_at_shutdown = PersistAtShutdown.IF_NOT_INITIALIZED_FROM_DISK,