-
-
Notifications
You must be signed in to change notification settings - Fork 13
Support tuple variable names in _subset_list #148
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: main
Are you sure you want to change the base?
Changes from all commits
88116f0
cefa874
049066b
e4acbea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import re | ||
| import warnings | ||
| from collections.abc import Sequence | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
@@ -90,8 +91,21 @@ def _subset_list(subset, whole_list, filter_items=None, warn=True, check_if_pres | |
| and ``filter_items``. | ||
| """ | ||
| if subset is not None: | ||
| if isinstance(subset, str): | ||
| if subset in whole_list: | ||
| subset = [subset] | ||
| elif isinstance(subset, str): | ||
| subset = [subset] | ||
| elif isinstance(subset, Sequence) and not isinstance(subset, str | bytes): | ||
| subset = list(subset) | ||
| else: | ||
| subset = [subset] | ||
|
|
||
| def _string_parts(obj): | ||
| if isinstance(obj, str): | ||
| return [obj] | ||
| if isinstance(obj, Sequence) and not isinstance(obj, str | bytes): | ||
| return [x for x in obj if isinstance(x, str)] | ||
| return [] | ||
|
|
||
| whole_list_tilde = [item for item in whole_list if _check_tilde_start(item)] | ||
| if whole_list_tilde and warn: | ||
|
|
@@ -112,11 +126,17 @@ def _subset_list(subset, whole_list, filter_items=None, warn=True, check_if_pres | |
| for pattern in excluded_items[:]: | ||
| excluded_items.remove(pattern) | ||
| if filter_items == "like": | ||
| real_items = [real_item for real_item in whole_list if pattern in real_item] | ||
| real_items = [ | ||
| real_item | ||
| for real_item in whole_list | ||
| if any(pattern in s for s in _string_parts(real_item)) | ||
| ] | ||
|
Comment on lines
129
to
133
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this is what we want. IIUC, with this behaviour, if I use I think for Important note: This is a collaborative project and it is quite probably it will take a while until we all agree on a behaviour around this. I may have ideas, but me saying "I think this or that should happen" doesn't automatically mean this should be the behaviour of the library. It can be frustrating but you'll probably need some extra patience for this PR.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification. I agree it’s better to align with xarray behavior and get consensus before finalizing anything. I’m happy to iterate based on feedback and adjust the implementation/tests as needed. Please let me know what target behavior you’d prefer and I can update the PR accordingly. |
||
| else: | ||
| # i.e filter_items == "regex" | ||
| real_items = [ | ||
| real_item for real_item in whole_list if re.search(pattern, real_item) | ||
| real_item | ||
| for real_item in whole_list | ||
| if any(re.search(pattern, s) for s in _string_parts(real_item)) | ||
| ] | ||
| if not real_items: | ||
| not_found.append(pattern) | ||
|
|
@@ -129,14 +149,23 @@ def _subset_list(subset, whole_list, filter_items=None, warn=True, check_if_pres | |
| subset = [item for item in whole_list if item not in excluded_items] | ||
|
|
||
| elif filter_items == "like": | ||
| subset = [item for item in whole_list for name in subset if name in item] | ||
| subset = [ | ||
| item | ||
| for item in whole_list | ||
| for name in subset | ||
| if isinstance(name, str) and any(name in s for s in _string_parts(item)) | ||
| ] | ||
| elif filter_items == "regex": | ||
| subset = [item for item in whole_list for name in subset if re.search(name, item)] | ||
|
|
||
| existing_items = np.isin(subset, whole_list) | ||
| if check_if_present and not np.all(existing_items): | ||
| raise KeyError(f"{np.array(subset)[~existing_items]} are not present") | ||
|
|
||
| subset = [ | ||
| item | ||
| for item in whole_list | ||
| for name in subset | ||
| if isinstance(name, str) and any(re.search(name, s) for s in _string_parts(item)) | ||
| ] | ||
| existing_items = [item in whole_list for item in subset] | ||
| if check_if_present and not all(existing_items): | ||
| missing = [item for item, ok in zip(subset, existing_items) if not ok] | ||
| raise KeyError(f"{missing} are not present") | ||
| return subset | ||
|
|
||
|
|
||
|
|
||
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.
not sure what this is doing