Lenses is a Python library inspired by functional programming concepts, providing a fluent API for immutable data manipulation. It allows for safe and concise modification, retrieval, and transformation of nested data structures (like dictionaries and lists) without mutating the original object. The current version is 1.2.0, with a moderate release cadence.
pip install lensesVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic `get()`, `set()`, and `modify()` operations for navigating and transforming nested immutable data structures.
Always assign the result of a `set()` or `modify()` operation to a new variable or overwrite the original if desired, e.g., `data = lens(data).path.set(new_value)`.
Use `lens(data)['key']` for dictionaries to ensure robust key access. Attribute access `lens(data).key` is syntactic sugar that might rely on custom `__getattr__` implementations or `dict` subclasses.
To handle missing keys gracefully, use methods like `lens(data).path.get_set(default_value)` which provides a fallback value if the path is not found, or `lens(data).path.get_0(default_value)` for an optional value.
Use item access `lens(data)['foo']` instead of `lens(data).foo`. While `lens` itself enables the `.foo` syntax, the error often originates when the underlying data is being accessed through a plain `dict`.
Before `get()`, `set()`, or `modify()`, ensure the path exists, or use methods like `lens(data).path.get_set(default_value)` to provide a fallback value if the path is not found.
Ensure all terminal operations are called with parentheses, even if they take no arguments, e.g., `lens(data).foo.get()` instead of `lens(data).foo.get`.
No dependency data recorded yet.