Updated doc strings and added type hints
This commit is contained in:
parent
2615798088
commit
aca9ba0723
@ -1,31 +1,33 @@
|
|||||||
import os
|
import os
|
||||||
import numpy as np
|
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
|
from typing import Optional, Callable
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
from .checksum import checksum
|
from .checksum import checksum
|
||||||
from .logging import logger
|
from .logging import logger
|
||||||
|
|
||||||
autosave_directory = None
|
autosave_directory: Optional[str] = None
|
||||||
load_autosave_data = False
|
load_autosave_data = False
|
||||||
verbose_print = True
|
verbose_print = True
|
||||||
user_autosave_directory = os.path.join(os.environ["HOME"], ".mdevaluate/autosave")
|
user_autosave_directory = os.path.join(os.environ["HOME"], ".mdevaluate/autosave")
|
||||||
|
|
||||||
|
|
||||||
def notify(msg):
|
def notify(msg: str):
|
||||||
if verbose_print:
|
if verbose_print:
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
else:
|
else:
|
||||||
logger.debug(msg)
|
logger.debug(msg)
|
||||||
|
|
||||||
|
|
||||||
def enable(dir, load_data=True, verbose=True):
|
def enable(dir: str, load_data: bool = True, verbose: bool = True):
|
||||||
"""
|
"""
|
||||||
Enable auto saving results of functions decorated with :func:`autosave_data`.
|
Enable auto saving results of functions decorated with: func: `autosave_data`.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
dir: Directory where the data should be saved.
|
dir: Directory where the data should be saved.
|
||||||
load_data (opt., bool): If data should also be loaded.
|
load_data (opt., bool): If data should also be loaded.
|
||||||
|
verbose (opt., bool): If autosave should be verbose.
|
||||||
"""
|
"""
|
||||||
global autosave_directory, load_autosave_data, verbose_print
|
global autosave_directory, load_autosave_data, verbose_print
|
||||||
verbose_print = verbose
|
verbose_print = verbose
|
||||||
@ -79,9 +81,8 @@ def get_directory(reader):
|
|||||||
if not os.access(savedir, os.W_OK):
|
if not os.access(savedir, os.W_OK):
|
||||||
savedir = os.path.join(user_autosave_directory, savedir.lstrip("/"))
|
savedir = os.path.join(user_autosave_directory, savedir.lstrip("/"))
|
||||||
logger.info(
|
logger.info(
|
||||||
"Switched autosave directory to {}, since original location is not writeable.".format(
|
"Switched autosave directory to {}, "
|
||||||
savedir
|
"since original location is not writeable.".format(savedir)
|
||||||
)
|
|
||||||
)
|
)
|
||||||
os.makedirs(savedir, exist_ok=True)
|
os.makedirs(savedir, exist_ok=True)
|
||||||
return savedir
|
return savedir
|
||||||
@ -140,20 +141,24 @@ def load_data(filename):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def autosave_data(nargs, kwargs_keys=None, version=None):
|
def autosave_data(
|
||||||
|
nargs: int, kwargs_keys: Optional[list[str]] = None, version: Optional[int] = None
|
||||||
|
) -> Callable:
|
||||||
"""
|
"""
|
||||||
Enable autosaving of results for a function.
|
Enable autosaving of results for a function.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
nargs: Number of args which are relevant for the calculation.
|
nargs: Number of args which are relevant for the calculation.
|
||||||
kwargs_keys (opt.): List of keyword arguments which are relevant for the calculation.
|
kwargs_keys (opt.):
|
||||||
|
List of keyword arguments which are relevant for the calculation.
|
||||||
version (opt.):
|
version (opt.):
|
||||||
An optional version number of the decorated function, which replaces the checksum of
|
An optional version number of the decorated function, which replaces the
|
||||||
the function code, hence the checksum does not depend on the function code.
|
checksum of the function code, hence the checksum does not depend on the
|
||||||
|
function code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator_function(function):
|
def decorator_function(function):
|
||||||
# make sure too include names of positional arguments in kwargs_keys,
|
# make sure to include names of positional arguments in kwargs_keys,
|
||||||
# sice otherwise they will be ignored if passed via keyword.
|
# sice otherwise they will be ignored if passed via keyword.
|
||||||
# nonlocal kwargs_keys
|
# nonlocal kwargs_keys
|
||||||
posargs_keys = list(inspect.signature(function).parameters)[:nargs]
|
posargs_keys = list(inspect.signature(function).parameters)[:nargs]
|
||||||
|
Loading…
Reference in New Issue
Block a user