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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pickle_debug
3
- Version: 1.1
3
+ Version: 1.2
4
4
  Summary: Store expensive objects from debugging
5
5
  Author: Gerard
6
6
  Author-email: gweatherby@uchc.edu
@@ -2,7 +2,7 @@
2
2
  name = "pickle_debug"
3
3
  description = "Store expensive objects from debugging"
4
4
  license = {text = "MIT" }
5
- version = "1.1"
5
+ version = "1.2"
6
6
  dependencies = []
7
7
  requires-python= ">= 3.12"
8
8
  readme = "README.md"
@@ -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
- key = _dumps((func.__module__, func.__qualname__, args, sorted(kwargs.items())))
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
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pickle_debug
3
- Version: 1.1
3
+ Version: 1.2
4
4
  Summary: Store expensive objects from debugging
5
5
  Author: Gerard
6
6
  Author-email: gweatherby@uchc.edu
@@ -8,5 +8,6 @@ src/pickle_debug.egg-info/dependency_links.txt
8
8
  src/pickle_debug.egg-info/top_level.txt
9
9
  src/test/patch_test.py
10
10
  src/test/pd_test.py
11
+ src/test/record.py
11
12
  src/test/ttest.py
12
13
  src/test/unpicklable_test.py
@@ -0,0 +1,11 @@
1
+ import pickle_debug
2
+
3
+ def sum(x,y):
4
+ print(f"{x} + {y}")
5
+ return x + y
6
+
7
+ pickle_debug.enable("/tmp/sum")
8
+ ans = pickle_debug.record("sum",sum,3,4)
9
+ print(ans)
10
+ ans = pickle_debug.record("sum",sum,3,4)
11
+ print(ans)
File without changes
File without changes
File without changes
File without changes