From 6dfab2d5ff870d8fc303ecfb70cdbb7384b2a9db Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 12 Oct 2022 11:45:06 -0700 Subject: [PATCH] Fixup sphinx usage examples to render correctly(?) --- src/pyutils/persistent.py | 108 ++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/src/pyutils/persistent.py b/src/pyutils/persistent.py index 88234cc..13de472 100644 --- a/src/pyutils/persistent.py +++ b/src/pyutils/persistent.py @@ -89,35 +89,37 @@ class FileBasedPersistent(Persistent): class PicklingFileBasedPersistent(FileBasedPersistent): """ - Example usage: + A class that stores its state in a file as pickled python objects. - import persistent + Example usage:: - @persistent.persistent_autoloaded_singleton() - class MyClass(persistent.PicklingFileBasedPersistent): - def __init__(self, data: Whatever): - #initialize youself from data + import persistent - @staticmethod - @overrides - def get_filename() -> str: - return "/path/to/where/you/want/to/save/data.bin" + @persistent.persistent_autoloaded_singleton() + class MyClass(persistent.PicklingFileBasedPersistent): + def __init__(self, data: Whatever): + #initialize youself from data - @staticmethod - @overrides - def should_we_save_data(filename: str) -> bool: - return true_if_we_should_save_the_data_this_time() + @staticmethod + @overrides + def get_filename() -> str: + return "/path/to/where/you/want/to/save/data.bin" - @staticmethod - @overrides - def should_we_load_data(filename: str) -> bool: - return persistent.was_file_written_within_n_seconds(whatever) + @staticmethod + @overrides + def should_we_save_data(filename: str) -> bool: + return true_if_we_should_save_the_data_this_time() - # 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() + @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() """ @@ -158,35 +160,37 @@ 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() + A class that stores its state in a JSON format file. + + 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() """ -- 2.45.2