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_path(path[, format])

Parse a path in various formats.

resolve_path(data, path[, default, format])

Resolve a path against data.

set_path(data, path, value[, format, ...])

Set a value at a path.

Classes

DotPath(path[, separator])

Dot notation path handler (e.g., 'user.address.city').

JSONPointer(pointer)

JSON Pointer implementation (RFC 6901).

TuplePath(parts)

Tuple-based path (unambiguous, e.g., ('user', 'address', 'city')).

Exceptions

InvalidPathError

Raised when a path is malformed or invalid.

class embody.paths.DotPath(path, separator='.')[source]

Bases: object

Dot notation path handler (e.g., ‘user.address.city’).

Examples

>>> data = {'user': {'address': {'city': 'NYC'}}}
>>> path = DotPath('user.address.city')
>>> path.resolve(data)
'NYC'
resolve(data, default=None)[source]

Resolve the path against data.

Parameters:
  • data (Any) – Data to resolve against

  • default (Any) – Default value if resolution fails

Return type:

Any

Returns:

Resolved value or default

to_json_pointer()[source]

Convert to a JSON Pointer.

Return type:

JSONPointer

Returns:

JSONPointer instance

exception embody.paths.InvalidPathError[source]

Bases: Exception

Raised when a path is malformed or invalid.

class embody.paths.JSONPointer(pointer)[source]

Bases: object

JSON 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:

parts (List[str]) – List of path components

Return type:

JSONPointer

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:
  • data (Any) – Data to resolve against

  • default (Any) – Default value if resolution fails

Return type:

Any

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:
  • data (Any) – Data to modify (in place)

  • value (Any) – Value to set

  • create_intermediate (bool) – If True, create intermediate structures

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: object

Tuple-based path (unambiguous, e.g., (‘user’, ‘address’, ‘city’)).

Examples

>>> data = {'user': {'address': {'city': 'NYC'}}}
>>> path = TuplePath(('user', 'address', 'city'))
>>> path.resolve(data)
'NYC'
resolve(data, default=None)[source]

Resolve the path against data.

Return type:

Any

to_json_pointer()[source]

Convert to a JSON Pointer.

Return type:

JSONPointer

embody.paths.parse_path(path, format='auto')[source]

Parse a path in various formats.

Parameters:
  • path (Union[str, Tuple, List]) – Path in any supported format

  • format (str) – Format hint (‘auto’, ‘json_pointer’, ‘dot’, ‘tuple’)

Return type:

Union[JSONPointer, DotPath, TuplePath]

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:
  • data (Any) – Data to resolve against

  • path (Union[str, Tuple, List]) – Path in any supported format

  • default (Any) – Default value if resolution fails

  • format (str) – Format hint

Return type:

Any

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'
embody.paths.set_path(data, path, value, format='auto', create_intermediate=True)[source]

Set a value at a path.

Parameters:
  • data (Any) – Data to modify

  • path (Union[str, Tuple, List]) – Path in any supported format

  • value (Any) – Value to set

  • format (str) – Format hint

  • create_intermediate (bool) – If True, create intermediate structures

Examples

>>> data = {}
>>> set_path(data, '/user/name', 'Alice')
>>> data
{'user': {'name': 'Alice'}}