Skip to content

Commit

Permalink
feat: paths_getter can take paths as a dict specifying the output keys
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Jan 19, 2024
1 parent 37983d4 commit 2ffb7da
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions dol/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ def _path_get(
except caught_errors as error:
if callable(on_error):
return on_error(
dict(obj=obj, path=path, result=result, k=k, error=error,)
dict(
obj=obj,
path=path,
result=result,
k=k,
error=error,
)
)
elif isinstance(on_error, str):
# use on_error as a message, raising the same error class
Expand Down Expand Up @@ -246,13 +252,30 @@ def paths_getter(paths, obj=None, *, egress=dict, **kwargs):
>>> path_extractor = paths_getter(paths)
>>> path_extractor(obj)
{'a.c': 2, 'd': 3}
See that the paths are used as the keys of the returned dict.
If you want to specify your own keys, you can simply specify `paths` as a dict
whose keys are the keys you want, and whose values are the paths to get:
>>> path_extractor_2 = paths_getter({'california': 'a.c', 'dreaming': 'd'})
>>> path_extractor_2(obj)
{'california': 2, 'dreaming': 3}
"""
if obj is None:
return partial(paths_getter, paths, **kwargs)

def pairs():
for path in paths:
yield path, path_get(obj, path=path, **kwargs)
if isinstance(paths, Mapping):

def pairs():
for key, path in paths.items():
yield key, path_get(obj, path=path, **kwargs)

else:

def pairs():
for path in paths:
yield path, path_get(obj, path=path, **kwargs)

return egress(pairs())

Expand Down Expand Up @@ -713,7 +736,11 @@ def __init__(self, _prefix=''):

@store_decorator
def mk_relative_path_store(
store_cls=None, *, name=None, with_key_validation=False, prefix_attr='_prefix',
store_cls=None,
*,
name=None,
with_key_validation=False,
prefix_attr='_prefix',
):
"""
Expand Down Expand Up @@ -1587,7 +1614,8 @@ def single_to_str(self, k: Any) -> str:

# @_return_none_if_none_input
def dict_to_namedtuple(
self, params: dict,
self,
params: dict,
):
r"""Generates a namedtuple from the dictionary values based on the template.
Expand Down

0 comments on commit 2ffb7da

Please sign in to comment.