harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0018harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0018Medium difficulty task
Goal
Build a compact, self-contained in-memory subset of the awkward v2 API sufficient for a small pytest suite. The implementation lives in a single Python file under /app/ and should be importable as a lightweight module that mimics a minimal portion of ak._v2.
Public API to implement
- A top-level class _v2 with:
- A nested Array class that can be constructed in two ways:
- ak._v2.Array({"x": np.arange(10)}) # base data from a dict of field -> sequence
- ak._v2.Array(rows) # rows is a list of dicts, each dict representing a row
- A nested operations submodule-like object accessible as ak._v2.operations with:
- with_field(base, what, where): returns a new Array with an added field named where
- base: an Array
- what: None, a scalar, or a list/array with per-row values
- where: string field name to add
- fields(array): returns a list of field names in the array, preserving insertion order
- The Array must support:
- Initialization from a dict of fields to sequences (base case)
- Initialization from a list of row dicts (used by with_field)
- __len__ to return the number of rows
- __getitem__(self, key) where key is a string:
- Returns a FieldView object representing that field, exposing:
- .type attribute: a small type-like object whose string representation matches the tests
- For unknown/broadcasted fields, str(field.type) should yield "<length> * ?unknown" (e.g., "10 * ?unknown")
- .tolist() method: returns the per-row values of that field (None if not present)
- __setitem__(self, key, value) where key is a string:
- Broadcasts a new field across all rows by setting field key to value for every row
- If value is None, each row gets field[key] = None
- If value is a scalar, broadcast that scalar to all rows
- If value is a per-row list/array, it must have length equal to the number of rows; otherwise raise ValueError
- After setting, the new field should appear in fields(array)
- A lightweight Type-like object used by FieldView.type that formats to "10 * ?unknown" for unknown fields (the tests only require this exact string form)
- The Type object only needs to support __str__ to satisfy tests
Key behaviors expected by the tests
- test_unknown_type
- Start with array = ak._v2.Array({"x": np.arange(10)})
- Derive arrays via with_field:
- array = ak._v2.operations.with_field(base=array, what=None, where="unknown field1")
- array = ak._v2.operations.with_field(base=array, what=[None], where="unknown field2")
- Access types of the new fields:
- tpe1 = array["unknown field1"].type
- tpe2 = array["unknown field2"].type
- Expect str(tpe1) == "10 * ?unknown" and str(tpe2) == "10 * ?unknown"
- test_in_place_wrapper_broadcasting
- Start with array = ak._v2.Array({"x": np.arange(3)})
- Broadcast a new field in-place:
- array["unknown field"] = None
- Field access with per-row values:
- array["unknown field"].tolist() should yield [None, None, None]
- Field names reflecting the addition:
- ak._v2.operations.fields(array) should yield ["x", "unknown field"]
Implementation details and constraints
- The solution must be a single file at /app/awkward.py (or /app/awkward/__init__.py if you prefer a package layout, but the tests are designed for /app/awkward.py).
- Only numpy is allowed as an optional dependency (used in the tests); you may import numpy as np in your module.
- The implementation should be simple, robust, and focused only on the subset used by the tests. No need to implement the full upstream awkward API.
- Input validation:
- with_field: if what is a per-row sequence, its length must equal the number of rows; otherwise raise ValueError.
- __setitem__: if broadcasting a per-row list/array, its length must equal the number of rows; otherwise raise ValueError.
- __init__: when constructing from a dict of fields to sequences, ensure all sequences have the same length; otherwise raise ValueError.
- The field order returned by fields(array) must be deterministic and reflect insertion order:
- For an initial dict, respect the dict’s insertion order.
- When adding new fields (via with_field or __setitem__), append the new field to the end of the order.
Hints and suggested approach
- Represent the array as a list of per-row dictionaries (self._rows). Maintain a separate self._field_order list that tracks the insertion order of fields.
- __getitem__ for a string key should return a lightweight FieldView object. The FieldView should expose:
- a .type object with a custom __str__ that prints "<length> * ?unknown" using len(self._array)
- a .tolist() method that returns the per-row values for that field
- Implement a simple Type-like class inside _v2 to produce the required string form for unknown fields.
- Keep the code well-structured and thoroughly validate inputs to help catch mistakes early.
Deliverable
- A single Python file at /app/awkward.py implementing:
- class _v2 with nested Array
- a nested operations submodule-like object with with_field and fields
- FieldView objects returned by Array.__getitem__
- The lightweight Type-like object with __str__ matching the required format
- In-place broadcasting behavior via Array.__setitem__
Notes
- The task targets a small, well-defined feature surface; aim for clarity and correctness over feature completeness.
- The tests rely on exact string formatting for the Type representation, so ensure __str__ yields "N * ?unknown" with a space around the asterisk.
- You may add minimal in-line comments to clarify design decisions, but keep the solution compact.