embody.paths¶
Path addressing for nested data structures.
This module provides utilities for working with paths in nested structures, supporting:
JSON Pointer (RFC 6901)
Dot notation
Tuple paths
Path parsing and resolution
Functions
|
Parse a path in various formats. |
|
Resolve a path against data. |
|
Set a value at a path. |
Classes
|
Dot notation path handler (e.g., 'user.address.city'). |
|
JSON Pointer implementation (RFC 6901). |
|
Tuple-based path (unambiguous, e.g., ('user', 'address', 'city')). |
Exceptions
Raised when a path is malformed or invalid. |
- class embody.paths.DotPath(path, separator='.')[source]¶
Bases:
objectDot notation path handler (e.g., ‘user.address.city’).
Examples
>>> data = {'user': {'address': {'city': 'NYC'}}} >>> path = DotPath('user.address.city') >>> path.resolve(data) 'NYC'
- exception embody.paths.InvalidPathError[source]¶
Bases:
ExceptionRaised when a path is malformed or invalid.
- class embody.paths.JSONPointer(pointer)[source]¶
Bases:
objectJSON Pointer implementation (RFC 6901).
JSON Pointer defines a string syntax for identifying a specific value within a JSON document. Example: /foo/bar/0 refers to the first element of the ‘bar’ array in the ‘foo’ object.
Examples
>>> data = {'foo': {'bar': [1, 2, 3]}} >>> ptr = JSONPointer('/foo/bar/0') >>> ptr.resolve(data) 1
>>> data = {'user': {'name': 'Alice', 'age': 30}} >>> JSONPointer('/user/name').resolve(data) 'Alice'
- classmethod from_parts(parts)[source]¶
Create a JSON Pointer from path components.
- Parameters:
- Return type:
- Returns:
JSONPointer instance
Examples
>>> ptr = JSONPointer.from_parts(['foo', 'bar', '0']) >>> ptr.pointer '/foo/bar/0'
- resolve(data, default=None)[source]¶
Resolve the pointer against data.
- Parameters:
- Return type:
- Returns:
Resolved value or default
Examples
>>> data = {'a': {'b': [1, 2, 3]}} >>> JSONPointer('/a/b/1').resolve(data) 2 >>> JSONPointer('/a/x').resolve(data, default='not found') 'not found'
- set(data, value, create_intermediate=False)[source]¶
Set a value at the pointer location.
- Parameters:
- Raises:
InvalidPathError – If path cannot be set
Examples
>>> data = {'a': {'b': {}}} >>> JSONPointer('/a/b/c').set(data, 42) >>> data {'a': {'b': {'c': 42}}}
- class embody.paths.TuplePath(parts)[source]¶
Bases:
objectTuple-based path (unambiguous, e.g., (‘user’, ‘address’, ‘city’)).
Examples
>>> data = {'user': {'address': {'city': 'NYC'}}} >>> path = TuplePath(('user', 'address', 'city')) >>> path.resolve(data) 'NYC'
- embody.paths.parse_path(path, format='auto')[source]¶
Parse a path in various formats.
- Parameters:
- Return type:
- Returns:
Path object
Examples
>>> parse_path('/user/name') <embody.paths.JSONPointer object at ...>
>>> parse_path('user.name') <embody.paths.DotPath object at ...>
>>> parse_path(('user', 'name')) <embody.paths.TuplePath object at ...>
- embody.paths.resolve_path(data, path, default=None, format='auto')[source]¶
Resolve a path against data.
- Parameters:
- Return type:
- Returns:
Resolved value or default
Examples
>>> data = {'user': {'name': 'Alice', 'age': 30}} >>> resolve_path(data, '/user/name') 'Alice' >>> resolve_path(data, 'user.age') 30 >>> resolve_path(data, ('user', 'name')) 'Alice'