-
Notifications
You must be signed in to change notification settings - Fork 35
Add option to skip enumeration by element types #53
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
base: master
Are you sure you want to change the base?
Conversation
| and not ( | ||
| keep_element_types | ||
| and isinstance(value, enumerate_types) | ||
| and any([isinstance(e, keep_element_types) for e in value]) |
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.
| and any([isinstance(e, keep_element_types) for e in value]) | |
| and any(isinstance(e, keep_element_types) for e in value) |
|
Thanks for your contribution. I'm thinking about a more general way to fulfill your requirement because
|
|
An example for the usage: >>> flatten({'a': ['b', 'c']}, enumerate_types=(list,), keep_element_types=(str,))is equivalent to def is_list_of_nonstr(parent, obj):
return isinstance(obj, list) and not any(isinstance(val, str) for val in obj)
flatten({'a': ['b', 'c']}, enumerate_types=(is_list_of_nonstr,)) |
|
You can do more interesting things like this: from some_type_checking_tool import check
def is_list_of_str(parent, obj):
return check(obj, list[str])
def is_the_specific_key(parent, obj):
return parent == ("the", "specific", "key") |
|
Sure, sounds good to me. Could you please implement it? |
|
Will do it when I have time. Issue created: #54. |
|
Thank you! I look forward to it. |
flatten-dict enumerates all the lists if
enumerate_types=(list,)regardless of the element types.The modification by this Pull Request adds flexibility to enumerate based on the element types while keeping the backward compatibility.
For example, users can now keep list of specific data types (e.g.
str) set bykeep_element_typesargument while still enumerating list of other types.