Skip to content

Instantly share code, notes, and snippets.

@raven4752
raven4752 / cache_decorator.py
Last active December 18, 2019 11:09
python decorator function to cache function's returned value to disk
def cache_result(cache_file, param_in_suffix=None, root_dir=None):
"""
decorator function to cache function's return value (assume returned value can be pickled)
the cached file with be located to root_dir/[_param-key_param-value,]/cache_file.pkl
usage:
@cache_result(cache_file_name,param_in_suffix=[param_key],)
def function_to_create_cache():
....
:param root_dir: dir to store the cache if the wrapped function has root_dir as a param, the value will be used instead
@raven4752
raven4752 / util.py
Created June 13, 2021 16:08
expose python function to cmd in one line
"""
a lazy wrapper to export python function directly as command line interface with one line of code
"""
import click
from functools import wraps, partial
import inspect
def get_prefix(key: str, prefix_set: set):