-
Notifications
You must be signed in to change notification settings - Fork 36
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
Adds Hook functionality #33
base: master
Are you sure you want to change the base?
Conversation
- `hook` Callable now added to flatten and unflatten - Fixed tests so that they point at local code instead of the installed package (which would be the last stable version) - Update readme for hook functionality - Update gitignore for PyCharm IDE
@ianlini lmk if theres anything you think should be changed on this or would need to be considered...otherwise this should be good to merge :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution. Your idea is cool but the word "hook" is too ambiguous here.
Correct me if I am wrong. We originally support customized key generating method (reducer and splitter), but the values cannot be changed. Therefore, you are proposing this hook for changing the values.
I think this is not related to the flatten or unflatten process, but might be good to have for saving memory usage or computing time. We can do all the things we want in one dict creation process instead of having some intermediate dict and doing some post processing. If this is the purpose, and think there are some other ways that are more flexible and pythonic.
For example, we can make a generator Flatten.items()
:
{key: hook(key, value) for key, value in Flatten(d).items()}
I think this will make the code much more readable. I'm actually planning to make a Flatten class like this, so please tell me your suggestion.
Right exactly. What are your thoughts on adding this to the splitter/reducer functions directly then @ianlini ?
I used
You've hit the nail on the head with saving memory usage. I'd prefer to do value processing as we do the packing and unpacking to save usage there.
I think the approach you've suggested above would add an additional level of processing, right? (since we'd create the items and then have to process them again with the hook) Context on my current use caseMy use case looks like this and is like vlookups (in excel) for api calls
And I have a working class that looks something like this (simplified for clarity):
|
In your code, you still need to create the key and value. Yielding items should be very fast, so I think it won't be the bottleneck in most cases. |
Combining with splitter/reducer will be more complicated. Most of the time, people don't need this function, so they don't want to return the identical value. It's hard to make one of the return value optional. It's also hard to explain how hook can be used because it is not abstract enough. People need to know a lot of implementation details to write the code correctly. For example, if I have a dict d = {"a": {"b": {"c": "value}}} People need to know that hook will be called like this: hook(("c",), "value")
hook(("b", "c"), "value")
hook(("a", "b", "c"), "value") If I want to hook something when the key is Other design issues:
|
hook
Callable now added to flatten and unflatten