embody.mappings¶
Mapping interface wrappers for embodied objects.
This module provides wrappers that convert nested structures into uniform Mapping interfaces, supporting:
Lazy evaluation
Attribute access (Box pattern)
Flat path-based access
Immutability (frozen objects)
Functions
|
Convert data to a Mapping with the specified style. |
Classes
|
Mapping that allows attribute access (Box pattern). |
|
Read-only Mapping wrapper for embodied objects with lazy evaluation. |
|
Mapping with flattened nested structure for path-based access. |
|
Immutable Mapping wrapper. |
|
Lazy Mapping that embodies values on access. |
|
Mutable Mapping wrapper for embodied objects. |
|
Mapping supporting multiple path access styles. |
- class embody.mappings.AttributeMapping(data)[source]¶
Bases:
MappingMapping that allows attribute access (Box pattern).
Allows accessing dictionary keys as attributes for more fluid syntax.
Examples
>>> data = {'user': {'name': 'Alice', 'age': 30}} >>> attr_map = AttributeMapping(data) >>> attr_map.user.name 'Alice' >>> attr_map.user.age 30 >>> attr_map['user']['name'] # Still works as dict 'Alice'
- class embody.mappings.EmbodiedMapping(data)[source]¶
Bases:
MappingRead-only Mapping wrapper for embodied objects with lazy evaluation.
This wrapper provides a Mapping interface around an embodied object, with support for lazy embodiment (values materialized on access).
Examples
>>> from embody import embody >>> template = {'name': '${name}', 'age': '${age}'} >>> params = {'name': 'Alice', 'age': 30} >>> # Embody eagerly first >>> data = embody(template, params) >>> mapping = EmbodiedMapping(data) >>> mapping['name'] 'Alice' >>> len(mapping) 2
- class embody.mappings.FlatMapping(data, separator='.')[source]¶
Bases:
MappingMapping with flattened nested structure for path-based access.
Allows accessing nested values using dot notation as single keys.
Examples
>>> nested = {'user': {'name': 'Alice', 'age': 30}} >>> flat = FlatMapping(nested) >>> flat['user.name'] 'Alice' >>> flat['user.age'] 30 >>> list(flat.keys()) ['user.name', 'user.age']
- class embody.mappings.FrozenMapping(data)[source]¶
Bases:
MappingImmutable Mapping wrapper.
Provides a frozen (immutable) view of a dictionary.
Examples
>>> data = {'a': 1, 'b': 2} >>> frozen = FrozenMapping(data) >>> frozen['a'] 1 >>> # Modifications to original don't affect frozen (deep copy) >>> data['c'] = 3 >>> 'c' in frozen False
- class embody.mappings.LazyEmbodiedMapping(template, params, embodier)[source]¶
Bases:
MappingLazy Mapping that embodies values on access.
This is useful for large templates where you only need to access a subset of values. Values are embodied on-demand and cached.
Examples
>>> from embody import Embodier >>> template = {'a': '${x}', 'b': '${y}', 'c': '${z}'} >>> params = {'x': 1, 'y': 2, 'z': 3} >>> embodier = Embodier(template) >>> lazy = LazyEmbodiedMapping(template, params, embodier) >>> lazy['a'] # Only 'a' is embodied 1
- class embody.mappings.MutableEmbodiedMapping(data)[source]¶
Bases:
MutableMappingMutable Mapping wrapper for embodied objects.
Provides full MutableMapping interface for modifying embodied results.
Examples
>>> data = {'a': 1, 'b': 2} >>> mutable = MutableEmbodiedMapping(data) >>> mutable['c'] = 3 >>> mutable['a'] = 10 >>> del mutable['b'] >>> dict(mutable) {'a': 10, 'c': 3}
- class embody.mappings.PathMapping(data, separator='.')[source]¶
Bases:
MappingMapping supporting multiple path access styles.
Supports:
Regular dict access: mapping[‘key’]
Dot notation: mapping[‘a.b.c’]
Tuple paths: mapping[(‘a’, ‘b’, ‘c’)]
JSON Pointer: mapping[‘/a/b/c’]
Examples
>>> data = {'a': {'b': {'c': 42}}} >>> pm = PathMapping(data) >>> pm['a.b.c'] 42 >>> pm[('a', 'b', 'c')] 42 >>> pm['/a/b/c'] # JSON Pointer 42 >>> pm['a'] # Regular access {'b': {'c': 42}}
- embody.mappings.as_mapping(data, style='basic')[source]¶
Convert data to a Mapping with the specified style.
- Parameters:
- Return type:
- Returns:
Mapping instance
Examples
>>> data = {'user': {'name': 'Alice'}} >>> m = as_mapping(data, 'attribute') >>> m.user.name 'Alice'
>>> m = as_mapping(data, 'flat') >>> m['user.name'] 'Alice'