pickle-debug 1.1__tar.gz → 1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pickle_debug-1.1/src/pickle_debug.egg-info → pickle_debug-1.2}/PKG-INFO +1 -1
- {pickle_debug-1.1 → pickle_debug-1.2}/pyproject.toml +1 -1
- {pickle_debug-1.1 → pickle_debug-1.2}/src/pickle_debug/__init__.py +34 -2
- {pickle_debug-1.1 → pickle_debug-1.2/src/pickle_debug.egg-info}/PKG-INFO +1 -1
- {pickle_debug-1.1 → pickle_debug-1.2}/src/pickle_debug.egg-info/SOURCES.txt +1 -0
- pickle_debug-1.2/src/test/record.py +11 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/LICENSE +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/README.md +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/setup.cfg +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/src/pickle_debug.egg-info/dependency_links.txt +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/src/pickle_debug.egg-info/top_level.txt +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/src/test/patch_test.py +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/src/test/pd_test.py +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/src/test/ttest.py +0 -0
- {pickle_debug-1.1 → pickle_debug-1.2}/src/test/unpicklable_test.py +0 -0
|
@@ -116,15 +116,32 @@ def _save(path: Path, result: Any) -> None:
|
|
|
116
116
|
raise
|
|
117
117
|
|
|
118
118
|
|
|
119
|
+
def _is_method_receiver(obj: Any, name: str, wrapper: Callable) -> bool:
|
|
120
|
+
"""True if obj is the self or cls that wrapper was called through as a method"""
|
|
121
|
+
cls = obj if isinstance(obj, type) else type(obj)
|
|
122
|
+
for klass in cls.__mro__:
|
|
123
|
+
attr = vars(klass).get(name)
|
|
124
|
+
if isinstance(attr, classmethod):
|
|
125
|
+
attr = attr.__func__
|
|
126
|
+
if attr is wrapper:
|
|
127
|
+
return True
|
|
128
|
+
return False
|
|
129
|
+
|
|
130
|
+
|
|
119
131
|
def cached(func: Callable) -> Callable:
|
|
120
|
-
"""Decorator: cache result of func in directory set by enable(), keyed by arguments
|
|
132
|
+
"""Decorator: cache result of func in directory set by enable(), keyed by arguments.
|
|
133
|
+
When used on a method, self (or cls) is keyed by its class name rather than its current value"""
|
|
121
134
|
|
|
122
135
|
@functools.wraps(func)
|
|
123
136
|
def wrapper(*args, **kwargs):
|
|
124
137
|
directory = _CACHE_DIRECTORY
|
|
125
138
|
if directory is None:
|
|
126
139
|
return func(*args, **kwargs)
|
|
127
|
-
|
|
140
|
+
key_args = args
|
|
141
|
+
if args and _is_method_receiver(args[0], func.__name__, wrapper):
|
|
142
|
+
cls = args[0] if isinstance(args[0], type) else type(args[0])
|
|
143
|
+
key_args = (f"{cls.__module__}.{cls.__qualname__}",) + args[1:]
|
|
144
|
+
key = _dumps((func.__module__, func.__qualname__, key_args, sorted(kwargs.items())))
|
|
128
145
|
path = directory / (hashlib.sha256(key).hexdigest() + _SUFFIX)
|
|
129
146
|
try:
|
|
130
147
|
with open(path, 'rb') as f:
|
|
@@ -140,3 +157,18 @@ def cached(func: Callable) -> Callable:
|
|
|
140
157
|
return result
|
|
141
158
|
|
|
142
159
|
return wrapper
|
|
160
|
+
|
|
161
|
+
def record(name:str,fn,*args,**kwargs):
|
|
162
|
+
p = None
|
|
163
|
+
if _CACHE_DIRECTORY is not None:
|
|
164
|
+
p = _CACHE_DIRECTORY / f"{name}.pickle"
|
|
165
|
+
if p.is_file():
|
|
166
|
+
with open(p,'rb') as f:
|
|
167
|
+
result = pickle.load(f)
|
|
168
|
+
return result
|
|
169
|
+
result = fn(*args,**kwargs)
|
|
170
|
+
if p is not None:
|
|
171
|
+
with open(p,'wb') as f:
|
|
172
|
+
pickle.dump(result,f)
|
|
173
|
+
return result
|
|
174
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|