harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0256harbor download openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0256# Easier task: Minimal Fiona-like API (in /app/)
Goal
Create a tiny, self-contained Fiona-like API in Python that is easy to implement and testable. It should provide a very small subset of Fiona’s behavior focused on reading and writing a simple feature collection stored in JSON. The API must be importable as `import fiona` and be usable via `fiona.open(...)` returning a context-managed collection object.
What to implement (simplified)
- A Python module at /app/fiona.py (or a tiny package at /app/fiona/ with __init__.py) exposing:
- def open(path, mode='r', encoding=None, layer=None, **kwargs)
- A class Collection that is returned by open(...)
- Context manager: supports __enter__ and __exit__
- Core behavior:
- __len__(self) -> int
- __iter__(self) -> iterator over feature dictionaries
- Writing support (only when mode == 'w'):
- writerecords(self, records) -> None
- records is an iterable of feature dicts shaped like:
{
'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [x, y]},
'properties': { ... }
}
- Writing uses the provided encoding (default 'utf-8' if encoding is None)
- A simple, JSON-based storage format is acceptable for this task (not a real shapefile)
- If mode is 'w', the writer should overwrite the output file with all provided features
- For reading after a write, subsequent open(..., mode='r') should read back the written features
- Path and layer handling (simplified):
- path is a unicode string
- If layer is provided, it is accepted but not required to affect behavior (ignore for simplicity)
- Unicode and encoding:
- When opening for reading, use the provided encoding (default to 'utf-8')
- When writing, open with the provided encoding (default 'utf-8')
- Ensure non-ASCII characters in feature properties survive a round trip (e.g., utf-8 and gb18030)
- No external dependencies required beyond Python standard library
- No side effects on import (no real I/O beyond the intended dataset file)
Minimal dataset and file layout (to support the tests in this simplified task)
- Provide a small dataset file at /app/dataset.json containing a list of features, e.g.:
[
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [1.0, 2.0]},
"properties": {"label": "A", "num": 1}
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [3.0, 4.0]},
"properties": {"label": "Français", "num": 2}
}
]
- The tests may instantiate open('/app/dataset.json') and expect len(...) to reflect the number of features (e.g., 2 in this minimal dataset).
- The tests may also write to a new file (e.g., '/app/output.json') using mode='w' and then read back to verify Unicode preservation.
What to deliver (implementation details)
- A single /app/fiona.py module (or a small /app/fiona/ package with __init__.py) that provides:
- def open(path, mode='r', encoding=None, layer=None, **kwargs)
- class Collection with:
- __init__(self, path, mode='r', encoding=None, layer=None, **kwargs)
- __enter__(self) -> Collection
- __exit__(self, exc_type, exc, tb) -> None
- __len__(self) -> int
- __iter__(self) -> iterator yielding feature dicts
- writerecords(self, records) -> None (only meaningful in mode 'w')
- Clear docstrings/comments describing behavior, limitations, and how the simplified API maps to Fiona-like concepts.
Hints and approach (to make it easy)
- Use the Python standard library only:
- json module for reading/writing the feature collection
- built-in open for file I/O
- Internal state:
- When opening in 'r' mode, load the JSON feature list into memory (self._features)
- When opening in 'w' mode, prepare to overwrite the target file; track mode so writerecords can replace contents
- Iteration and length:
- __len__ returns len(self._features)
- __iter__ yields from iter(self._features)
- Writing flow:
- writerecords(records) should collect all provided features into a list, replace self._features, and write to disk using json.dump with the chosen encoding
- After writing, __iter__ and __len__ reflect the newly written data
- Encoding considerations:
- When opening or writing, pass encoding to open(..., encoding=...) to ensure proper Unicode handling
- For tests involving utf-8 and gb18030, ensure that non-ASCII strings survive a read/write cycle by using the encoding parameter consistently
- Unicode paths:
- Use path strings as provided; Python 3 handles Unicode paths well. Do not attempt to decode/encode the path manually
Acceptance criteria
- The module can be imported as import fiona without side effects
- The open function and Collection class provide the described surface:
- __enter__/__exit__ for context management
- __len__ and __iter__ for reading
- writerecords for writing when mode='w'
- Encoding scenarios (utf-8, gb18030) round-trip correctly for the minimal dataset
- Path handling supports Unicode file paths in /app/
- Layer parameter exists in signature and is accepted, but behavior can be simplified (ignored for now)
Notes
- This is an easier, self-contained variant of the Fiona-like API. It is not a full Fiona implementation and does not support actual shapefile drivers or layers. The intention is to provide a compact, test-friendly API that exercises the ideas of open/Collection, iteration, length, and encoding-aware writing using a simple JSON-backed dataset.
- Documentation within the module should clearly indicate that this is a simplified, educational subset intended to satisfy the tests for this task.
Place to implement
- Put the code in /app/fiona.py (or as /app/fiona/__init__.py for a tiny package, but a single file is fine and simpler for testing).