harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0099harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0099Harder task: Robust and scalable fb_group.parse_args with streaming, Unicode, and design constraints
Goal
Extend the fb_group parsing utility to be robust against very large inputs, handle a wider range of edge cases, and follow a clean design pattern. The public API must remain the same for compatibility with the existing test suite.
What to implement
- Create a Python package at /app/fb_group (i.e., a directory named fb_group with an __init__.py) containing a module utils.py.
- In fb_group.utils, implement the function:
- def parse_args(argv: list[str]) -> dict[str, str]
- Additional internal API (optional but recommended):
- def _token_to_kv(token: str) -> tuple[str, str] | None
- Behavior required by tests (unchanged from original):
- parse_args([]) -> {}
- parse_args(["a=1"]) -> {"a": "1"}
- parse_args(["a=1", "b=2"]) -> {"a": "1", "b": "2"}
- parse_args(["a"]) -> {}
- parse_args(["a", "b"]) -> {}
- parse_args(["a="]) -> {"a": ""}
- parse_args(["a=", "b="]) -> {"a": "", "b": ""}
- Rules and edge cases (extended requirements):
- Only tokens containing an "=" are considered valid key=value pairs.
- Split on the first "=" only (e.g., "k=v=c" yields key="k", value="v=c").
- The key must be non-empty to be included in the result (e.g., "=value" should be ignored).
- Values may be empty strings (e.g., "a=" yields value "").
- If the same key appears multiple times, the last occurrence wins (override previous values).
- Harder constraints and design goals:
- Performance and memory:
- Aim for linear time O(n) in the number of tokens and O(m) extra memory where m is the number of distinct keys produced.
- Implement a streaming-like approach to minimize intermediate allocations; avoid creating unnecessary temporary lists.
- Robustness and edge coverage:
- Support extremely large argv lists (millions of tokens) without assuming a small input size.
- Preserve Unicode and non-ASCII characters exactly as provided in both keys and values.
- Do not trim whitespace; treat keys and values as-is after the first "=".
- Purity and thread-safety:
- Implement as a pure function with no side effects or reliance on global state; safe to call concurrently.
- Design pattern:
- Use a small private helper _token_to_kv(token: str) -> tuple[str, str] | None to encapsulate single-token parsing logic.
- The public parse_args should orchestrate the streaming iteration and build the final dictionary from the results of the helper.
- Documentation and typing:
- Provide a clear, concise docstring for parse_args describing behavior, edge cases, and performance considerations.
- Include type hints (as shown) and ensure compatibility with the existing test suite.
- Import/export:
- Ensure tests can import parse_args via fb_group.utils (i.e., the module must be importable as a package named fb_group).
- File layout (actionable):
- /app/fb_group/__init__.py
- /app/fb_group/utils.py (contains parse_args and the optional _token_to_kv helper)
Notes
- The tests will continue to import from fb_group.utils, so the implementation must be accessible as a package named fb_group.
- The specification should remain in Python, maintaining the same general topic of a simple command-line argument parser, but with the added requirements for performance, edge-case handling, and design discipline.