neuralib.persistence.cli_persistence.PersistenceOptions
- class neuralib.persistence.cli_persistence.PersistenceOptions[source]
Bases:
Generic[T]The Option class that handle one kind of cache class T, including load cache, create cache, and save cache.
- __init__()
Methods
__init__()compute_cache(result)Compute cache according to cache's required fields.
create an empty cache which only initialize required fields.
find_cache(result[, dest, validator])Find the persistence.
load_cache([result, error_when_missing, dest])load persistence from disk according to result's required fields.
persistence_handler(dest)save_cache(result, dest[, force])validate_cache(result_path, result)Validating loaded cache instance.
Attributes
invalid persistence data (default: False)
- GROUP_CACHE = 'Persistence options'
- invalid_cache: bool
invalid persistence data (default: False)
- property persistence_class: type[T]
- persistence_handler(dest)[source]
- Parameters:
dest (Path) – save root directory.
- Returns:
- Return type:
- abstractmethod empty_cache()[source]
create an empty cache which only initialize required fields.
- Returns:
cache instance
- Return type:
T
- find_cache(result, dest=None, validator=False)[source]
Find the persistence.
for all fields
>>> template = self.empty_cache() >>> template.a = 1 # want to find all cache whose `a` equals to 1 >>> template.b = field_missing # want to find all cache and don't matter what `b` is >>> found = self.find_cache(template)
- Parameters:
result (T)
dest (Path | None) – save root directory.
validator
- Returns:
- Return type:
list[T]
- save_cache(result, dest, force=True)[source]
- Parameters:
result (T)
dest (Path) – save root directory.
force
- Returns:
- load_cache(result=None, error_when_missing=False, dest=None, **kwargs)[source]
load persistence from disk according to result’s required fields.
- Parameters:
result (T | None) – persistence instance with necessary fields filled.
error_when_missing – do not try to generate the cache when cache missing.
dest (Path | None) – save root directory.
kwargs – overwrite field value in result.
- Returns:
persistence instance.
- Raises:
FileNotFoundError – error_when_missing and file not found.
- Return type:
T
- validate_cache(result_path, result)[source]
Validating loaded cache instance.
Once validating fail (return False), goto
_compute_cache().- Parameters:
result_path (Path)
result (T)
- Returns:
False if validating fail.
- Raises:
TypeError – if validating fail.
ValueError – if validating fail.
KeyError – if validating fail.
AttributeError – if validating fail.
RuntimeError – if validating fail.
- Return type:
bool