-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making conditional_data_trans
better, and generally; recursively applying wrappers
#10
Labels
enhancement
New feature or request
Comments
thorwhalen
added a commit
that referenced
this issue
Jun 11, 2022
Related: A simple from collections.abc import MutableMapping
def flatten_dict(d: MutableMapping, parent_key: str = '', sep: str ='.') -> MutableMapping:
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items) Found this in flatten a dict in 4 different ways article. One we should read before tackling this issue. Other stuff to look at as well: |
This was referenced Mar 7, 2023
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This theme comes up regularly.
When we wrap a store, only the first level is wrapped. If the store's nature is nested, one often expects that the wrap be applied to the nested levels, but one would be wrong to expect this, since it would be an undesirable effect in the general case.
Therefore we need to make it easy for the user to control this aspect if they need it.
To abstract? Here's an example with
add_path_access
(code to that function here).The
add_path_access
does this: doesn't carry on to values.But the wrapping doesn't carry on to values, so if you ask for
s['a']
, and then want to get['b', 'c']
from it, it won't work:That's because
add_path_access
applies to the "top level" only.Once you ask for the
'a'
key, it gives you that value, but that value is a "pure dict", not one wrapped withadd_path_access
likes
is. Of course we could do this: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 wrapspecific values with
add_path_access
.For example, if you wanted to wrap all mappings recursively, you could:
conditional_data_trans
This function:
implements the pattern above. We can now do this:
Tasks
wrapper does not work with types
Well, it just doesn't work at the first level:
Generalize condition
Is checking the value general enough?
We could use the
postget
argument ofwrap_kvs
to condition on both value and key.But in this case it's strange: We need to include the condition and the trans both in data_trans then.
We might want a
condition: Callable[[Key, Value], bool]
.We might also want to handle a
condition: Callable[[Path, Key, Value], bool]
for more (full?) control -- say if we wanted to condition on the nested depth.And what about according to the path itself? Perhaps we'd like to
┆Issue is synchronized with this Asana task by Unito
The text was updated successfully, but these errors were encountered: