Skip to content

Commit

Permalink
docs: doctest to show how one can carry on the add_path_access recurs…
Browse files Browse the repository at this point in the history
…ively.
  • Loading branch information
thorwhalen committed Jun 11, 2022
1 parent 7459632 commit 008d7b4
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions dol/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,9 @@ def __getitem__(self, k):
return store_cls

# TODO: Should we keep add_path_get, or add "read_only" flag to add_path_access?
# TODO: store['a','b','c'] works, but store['a']['b','c'] doesn't because the "path
# accessibility" doesn't carry on to values by default. See doctest on how to make it
# happen an integrate here with one argument only: value condition.
@store_decorator
def add_path_access(store=None, *, name=None, path_type: type = tuple):
"""Make nested stores (read/write) accessible through key paths (iterable of keys).
Expand Down Expand Up @@ -2291,6 +2294,44 @@ def add_path_access(store=None, *, name=None, path_type: type = tuple):
>>> del s['a.b.c']
>>> s
{'a': {'b': {}}}
Note: The add_path_access doesn't carry on to values.
>>> s = add_path_access({'a': {'b': {'c': 42}}})
>>> s['a', 'b', 'c']
42
>>> # but
>>> s['a']['b', 'c']
Traceback (most recent call last):
...
KeyError: ('b', 'c')
That said,
>>> add_path_access(s['a'])['b', 'c']
42
The reason why we don't do this automatically is that it may not always be desirable.
If one wanted to though, one could use ``wrap_kvs(obj_of_data=...)`` to wrap
specific values with ``add_path_access``.
For example, if you wanted to wrap all mappings recursively, you could:
>>> from typing import Mapping
>>> from dol.trans import wrap_kvs
>>> def add_path_access_if_mapping(v):
... if isinstance(v, Mapping):
... return wrap_kvs(
... add_path_access(v),
... obj_of_data=add_path_access_if_mapping
... )
... return v
>>> s = add_path_access_if_mapping({'a': {'b': {'c': 42}}})
>>> s['a', 'b', 'c']
42
>>> # But now this works:
>>> s['a']['b', 'c']
42
"""
store_cls = kv_wrap_persister_cls(store, name=name)
store_cls = add_path_get(store_cls, name=name, path_type=path_type)
Expand Down

0 comments on commit 008d7b4

Please sign in to comment.