harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0316harbor download openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0316Goal Implement a compact memoization utility in a single Python file that the tests can import and exercise. To simplify, provide a minimal yet working version that covers the essential interactions the tests rely on:
Location Place the implementation in /app/memoclass.py (single-file approach).
What you must implement (simplified, test-friendly interface)
Signature:
Behavior (minimal, to satisfy tests):
Signature:
Behavior (simplified, test-focused):
Signature:
Behavior (simplified):
Example pattern implied by tests (skeleton outline; you don’t need to copy this exactly, but behavior should align):
Expected test semantics (high level):
Skeleton / starter hints (optional)
Notes:
import contextlib import functools import weakref
_MEMO_WRAPPERS = weakref.WeakSet()
class MemoClass: def init(self, mutable_attrs=None, **kwargs): self._locked = False self._memo_caches_enabled = True self._mutable_attrs = list(mutable_attrs) if mutable_attrs else [] for k, v in kwargs.items(): setattr(self, k, v)
def __setattr__(self, name, value):
if name != "_locked" and getattr(self, "_locked", False):
if not name.startswith("_"):
raise ValueError("Cannot mutate attributes while locked")
object.__setattr__(self, name, value)
def disable_caches(self):
self._memo_caches_enabled = False
def enable_caches(self):
self._memo_caches_enabled = True
@contextlib.contextmanager
def locked(self):
prev = getattr(self, "_locked", False)
self._locked = True
try:
yield self
finally:
self._locked = False
def memomethod(func): # Per-function cache: instance -> {state_key: result} cache = weakref.WeakKeyDictionary()
@functools.wraps(func)
def bound(self, *args, **kwargs):
if not getattr(self, "_memo_caches_enabled", True):
return func(self, *args, **kwargs)
# Build a state key including instance state
state_key = (args, tuple(sorted(kwargs.items())), tuple(sorted(self.__dict__.items())))
inst_cache = cache.get(self)
if inst_cache is None:
inst_cache = {}
cache[self] = inst_cache
if state_key in inst_cache:
return inst_cache[state_key]
result = func(self, *args, **kwargs)
inst_cache[state_key] = result
return result
def clear_cache():
cache.clear()
bound.clear_cache = clear_cache
_MEMO_WRAPPERS.add(bound)
return bound
def mutates(method): @functools.wraps(method) def wrapper(self, *args, **kwargs): result = method(self, *args, **kwargs) # Invalidate all memoized caches globally for simplicity for m in list(_MEMO_WRAPPERS): try: m.clear_cache() except Exception: pass return result return wrapper
Notes for ease
What to deliver