neuralib.persistence.cli_persistence
Persistence Class for argp supporting
- author:
Ta-Shun Su
- class neuralib.persistence.cli_persistence.PersistenceOptions[source]
The Option class that handle one kind of cache class T, including load cache, create cache, and save cache.
- GROUP_CACHE = 'Persistence options'
- invalid_cache: bool
invalid persistence data
- property persistence_class: type[T]
- persistence_handler(dest)[source]
- Parameters:
dest (Path) – save root directory.
- Returns:
- Return type:
- abstract 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
- neuralib.persistence.cli_persistence.get_options_and_cache(opt_cls, ref, error_when_missing=False, **kwargs)[source]
copy the arguments from PersistenceOpt (class that compute the cache) to Apply*Opt Can be used for analysis apply two different cached files
- Example
>>> @persistence.persistence_class >>> class SortIdxCache: ... # for cache attributes declare ... pass
>>> class SortIdxOptions(PersistenceOptions[SortIdxCache]): ... # for cache computing ... def empty_cache(self) -> SortIdxCache: ... pass ... def _compute_cache(self, cache: SortIdxCache) -> SortIdxCache: ... pass
>>> class ApplySortIdxOptions: ... # for cache applying, suppose to be the one layer before parent class for analysis class ... def apply_sort_idx_cache(self) -> SortIdxCache: ... return get_options_and_cache(SortIdxOptions, self)
>>> class CPBeltSortOptions(ApplyPosBinActOptions, ApplySortIdxOptions): ... pass
- Parameters:
opt_cls (type[PersistenceOptions[T]]) – PersistenceOpt
ref
error_when_missing
- Returns:
- Return type:
T