Add option to skip enumeration by element types#53
Add option to skip enumeration by element types#53Minyus wants to merge 1 commit intoianlini:masterfrom
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.
| 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.