AFL.automation.shared.PersistentConfig#
Classes
|
A MutableMapping is a generic container for associating key/value pairs. |
|
A dictionary-like class that serializes changes to disk |
- class AFL.automation.shared.PersistentConfig.PersistentConfig(path, defaults=None, overrides=None, lock=False, write=True, max_history=10000, datetime_key_format='%y/%d/%m %H:%M:%S.%f')[source]#
A dictionary-like class that serializes changes to disk
This class provides dictionary-like setters and getters (e.g., [] and .update()) but, by default, all modifications are written into a file in json format with the root keys as timestamps. Modifications can be blocked by locking the config, and writing to disk can be disabled by setting the appropriate member attributes (see constructor). On instantiation, if provided with a previously saved configuration file, PersistentConfig will load the file’s contents into memory and use the more recent configuration.
- __init__(path, defaults=None, overrides=None, lock=False, write=True, max_history=10000, datetime_key_format='%y/%d/%m %H:%M:%S.%f')[source]#
Constructor
- Parameters:
path (str or pathlib.Path) – File path to file in which this config is or will be stored. This file will be created if it does not exist
defaults (dict) – Default values to use if no saved config is available or if parameters are missing from a saved config.
overrides (dict) – Values to use that override parameters in a saved config. These parameters can be changed after the PersistentConfig object is instantiated.
lock (bool) – If True, an AttributeError will be raised if the user attempts to modify the config
write (bool) – If False, all writing to the config file will be disabled and a warning will be emitted each time the config is modified.
datetime_key_format (str) – String defining the root level keys of the json-serialized file. See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior.
- __setitem__(key, value)[source]#
Dictionary-like setter via config[“param”]=value
Changes will be written to PersistentConfig.path if PersistentConfig.write is True (default).
- update(update_dict)[source]#
Update several values in config at once
Changes will be written to PersistentConfig.path if PersistentConfig.write is True (default).
- revert(nth=None, datetime_key=None)[source]#
Revert config to a historical config
- Parameters:
nth (int, optional*) – Integer index of historical value to revert to. Can be negative to count from end of history. Note that -1 will correspond to the current config.
datetime_key (str, optional) – datetime formatted string as defined by datetime_key_format