harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0250harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0250Task: Hardened, self-contained ogindia micro-data calculator (no external deps)
Overview
- You must implement a small Python package under /app/ogindia with these modules:
- __init__.py (minimal; ensures package import works)
- utils.py
- get_micro_data.py
- The package must be self-contained (no Tax-Calc or external data dependencies) and deterministic.
What to implement
1) ogindia.utils
- Define a constant CPS_START_YEAR: int (default to 2016). The exact value is not important for tests beyond being a passed-through constant.
2) ogindia.get_micro_data
- Implement a function with the signature:
- def get_calculator(baseline, start_year, reform=None, records_start_year=None, data=None)
- Return a small, deterministic Calculator object that supports array extraction via an array interface used by tests.
- Implement an internal tiny data model to simulate three scenarios required by tests:
- data == "cps":
- Dataset:
- blind_head: [1, 0, 0] (sum > 0)
- e00700: [0, 0, 0] (sum == 0)
- data is None:
- Dataset (puf-like default when no explicit path is provided):
- blind_head: [0, 0, 0] (sum == 0)
- e00700: [5, 0, 0] (sum > 0)
- data is a string path:
- If the basename ends with "notapath.csv" (i.e., data == "notapath.csv"):
- Raise an IOError or ValueError whose message contains the substring "notapath.csv".
- If the basename is "puf.csv":
- Treat this as the puf dataset (same structure as the data=None case).
- For any other path:
- Raise a generic error mentioning the provided path.
- The Calculator interface must support:
- An array(name) method that returns an object with a sum() method.
- Example usage (as tests expect):
- calc = get_calculator(...); arr = calc.array("blind_head"); total = arr.sum()
- Unknown variable name: calc.array(name) should raise KeyError.
- Tiny data wrapper:
- class SimpleArray with:
- __init__(self, values)
- def sum(self): return sum(self.values)
- The Calculator object should be thread-safe (design for concurrency) and deterministic.
3) Design and architecture requirements (harder specs)
- Performance constraints:
- Access to arrays must be O(1) and independent of data size in typical usage; precompute any needed mappings at Calculator creation time.
- Avoid any heavy or repeated computations on array(name) calls; each call should return a lightweight SimpleArray wrapper around pre-stored data.
- Thread safety:
- Calculator.array(...) must be safe to call from multiple threads simultaneously. Use a simple locking mechanism where appropriate, without harming performance for typical single-threaded tests.
- Edge cases and robustness:
- Support empty datasets gracefully (sum() should be 0).
- Support non-ASCII or unusual path components in data (unicode basenames, etc.).
- If data is an unexpected type, raise a clear, deterministic error mentioning the provided type.
- Multi-file design patterns:
- While the implementation remains simple, structure the internal code to hint at separation of concerns:
- A minimal internal DataModel selection based on the data parameter.
- A tiny, explicit Calculator interface with an internal registry of allowed variable names (e.g., "blind_head", "e00700").
- Keep all data and logic encapsulated within get_micro_data.py, but document the responsibilities clearly.
- Packaging constraints:
- Do not depend on external libraries.
- The solution must be fully contained within /app/ (specifically /app/ogindia/).
- Documentation and errors:
- Error messages for the not-a-path scenario must contain "notapath.csv".
- When using a puf.csv path, the behavior should mirror the data=None puf-like case.
- When given an unknown path that is not covered by the two special cases, raise a generic error that includes the provided path.
Tests expectations (summary)
- test_cps:
- get_calculator(..., data="cps") yields a calculator where:
- calc.array("blind_head").sum() > 0
- calc.array("e00700").sum() == 0
- test_set_path:
- get_calculator(..., data="notapath.csv") raises an error with a message containing "notapath.csv"
- test_puf_path:
- If there is a file named puf.csv next to tests and you call get_calculator(..., data="path/to/puf.csv"), the calculator should behave like the puf dataset (blind_head sum == 0, e00700 sum > 0).
- If puf.csv does not exist and you call get_calculator(..., data=None), the function should raise an error containing "puf.csv".
Files to create (summary)
- /app/ogindia/__init__.py
- /app/ogindia/utils.py
- CPS_START_YEAR = 2016 (or another reasonable int)
- /app/ogindia/get_micro_data.py
- def get_calculator(baseline, start_year, reform=None, records_start_year=None, data=None)
- class Calculator
- class SimpleArray
Notes
- Keep the tests in mind: the code must preserve the exact behaviors described above, including error message substrings and the array interface contract.
- The task remains self-contained and avoids external dependencies.