harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0146harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0146# MEDIUM: In-process partd-like zmq API (single-file, /app)
Goal
Implement a lightweight, in-process key-value store with a ZeroMQ–like API surface, suitable for in-process tests. Do this in a single Python file placed under /app/ (a minimal package shape is acceptable). The implementation should expose a small in-memory “server/client” pair that mimics the essential API used by the tests, without requiring real IPC or ZeroMQ sockets.
What to implement (in a single-file style)
- Create a minimal in-process package layout inside /app/ so that the tests can import:
- from partd.zmq import Server, keys_to_flush, File, Client
- from partd import core, Dict
- Do this without creating real socket communications. Use a shared in-process registry so a Client connects to the same in-process Server instance by address.
- Provide:
- partd.Dict: a simple dict-like type (a small subclass of dict is fine)
- partd.core: a minimal module (exported as partd.core) to satisfy imports
- partd.zmq: a module-like namespace containing:
- Server
- __init__(self, path=None, available_memory=None, n_outstanding_writes=None, start=False)
- address attribute (string identifier for the server)
- start(self) -> None
- __enter__(self) -> Server
- __exit__(self, exc_type, exc, tb) -> None
- append(self, payload: dict) -> None
- get(self, keys: Union[str, List[Union[str, tuple]]]) -> Union[bytes, List[bytes]]
- delete(self, keys: Union[str, List[Union[str, tuple]]]) -> None
- iset(self, key, value: bytes) -> None
- drop(self) -> None
- close(self) -> None
- Client
- __init__(self, address: str)
- __enter__(self) -> Client
- __exit__(self, exc_type, exc, tb) -> None
- append(self, payload: dict) -> None
- get(self, keys: Union[str, List[Union[str, tuple]]]) -> Union[bytes, List[bytes]]
- delete(self, keys: Union[str, List[Union[str, tuple]]]) -> None
- iset(self, key, value: bytes) -> None
- drop(self) -> None
- __getstate__ and __setstate__ to support pickle
- File: a placeholder class
- keys_to_flush: a placeholder function
- Behavior/semantics to mirror (in-memory, in-process only)
- In-memory store: key (str or tuple) -> bytes
- append(payload)
- For each key, if it exists, new_value = old_value + payload_value; else set to payload_value
- get(keys)
- If keys is a single str, return the corresponding bytes (or b'' if missing)
- If keys is a list, return a list of bytes in the same order, preserving key types (strings or tuples)
- If keys is a single tuple key, return the bytes for that key (not a list)
- If a key is missing, return b'' for that position
- delete(keys)
- For each key, set its value to b'' (empty) in the store
- iset(key, value)
- Overwrite or set a single key to value
- drop()
- Clear the entire in-memory store
- Server lifecycle
- Server.start() registers the server in a global in-process registry
- Address is a stable identifier (string)
- Server.__enter__ should start and return self
- Server.__exit__ should close and detach from the registry
- Client lifecycle
- Client connects via address to the shared server
- Client methods perform operations on the connected server
- Client must be pickleable:
- Implement __getstate__ and __setstate__ so that pickling a Client preserves the ability to operate on the same server after unpickling
- The typical approach: store the address and look up the server in a global registry on use
- Edge cases and moderate tests (1–2)
- Ensure get(['x', 'y']) returns a list of bytes in the same order, with b'' for missing keys
- Ensure get('x') returns a single bytes value, not a list
- Support tuple keys as first-class keys (do not normalize or sort)
- After delete(['x']), 'x' should yield b'' when retrieved
- After drop(), any get returns b'' for all keys
- Pickle/unpickle a Client, then use it to perform operations against the same in-process server
- Validation and robustness
- Validate payload types in append: keys must be strings or tuples, values must be bytes
- Validate keys in get/delete/iset/del: must be strings, tuples, or lists of those; raise TypeError for invalid types
- If a Client references a non-existent server address, raise a helpful error
- Hints and design notes
- Use a global registry dict like _SERVER_REGISTRY[address] = server_instance
- For Client pickle support, __getstate__ should return a minimal serializable state (e.g., {'address': self._address}); __setstate__ should restore the address
- Implement __enter__/__exit__ to make Server usable in a with block; ensure resources are cleaned up on exit
- Keep the implementation self-contained in /app/ (no external sockets or processes)
Deliverables
- A single Python file under /app/ that provides the required behavior by:
- Defining partd.Dict
- Providing a minimal in-process partd.core module
- Providing a in-memory partd.zmq module (via an in-file module object) containing Server, Client, File, and keys_to_flush
- The package should be importable as:
- from partd.zmq import Server, keys_to_flush, File, Client
- from partd import core, Dict
Notes
- The tests will create and use a Server, create a Client connected to that server, perform append/get/delete/iset/drop, and verify pickle behavior of Client. Ensure consistent behavior across all these scenarios.
- Prioritize correct semantics and clean, well-validated input handling over complex concurrency or real IPC.
Optional tips
- You may implement the in-file submodules by populating sys.modules['partd.zmq'] and sys.modules['partd.core'] inside /app/partd/__init__.py, and export the appropriate names from those module objects.
- Keep the solution self-contained and well-documented to ease understanding and testing.