Skip to content
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

Modified search to take in multiple strings #4650

Merged
merged 27 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
52ff65c
modified search to take in multiple strings
medha-14 Dec 9, 2024
ede7d77
style: pre-commit fixes
pre-commit-ci[bot] Dec 9, 2024
99c77e2
Merge branch 'develop' into multi_string
kratman Dec 9, 2024
29956a5
added tests
medha-14 Dec 10, 2024
ae78b02
Merge branch 'develop' into multi_string
medha-14 Dec 10, 2024
5851c25
added changelog entry
medha-14 Dec 11, 2024
8d0a9c2
modified docstring
medha-14 Dec 11, 2024
273e609
modified search to handle partial matches
medha-14 Dec 12, 2024
d9e5bcd
modified tests
medha-14 Dec 12, 2024
6b953a1
minor refactoring
medha-14 Dec 12, 2024
3f96d73
Merge branch 'develop' into multi_string
medha-14 Dec 12, 2024
92e12b3
Merge branch 'develop' into multi_string
medha-14 Dec 13, 2024
926097c
added tests
medha-14 Dec 13, 2024
6716db7
Merge branch 'develop' into multi_string
medha-14 Dec 14, 2024
5a5f350
modified to handle empty inputs
medha-14 Dec 16, 2024
4763d6e
fixed test coverage
medha-14 Dec 16, 2024
0aae031
fixed original_keys
medha-14 Dec 16, 2024
b0b361f
Merge branch 'develop' into multi_string
agriyakhetarpal Dec 16, 2024
3a1b130
Merge branch 'develop' into multi_string
medha-14 Dec 18, 2024
67ba351
minor changes
medha-14 Dec 18, 2024
99fd204
added annotations
medha-14 Dec 18, 2024
6f36d36
result formatting
medha-14 Dec 19, 2024
3e27a48
Merge branch 'develop' into multi_string
medha-14 Dec 19, 2024
c9f5ae9
error message formatting
medha-14 Dec 20, 2024
2ce438e
Merge branch 'develop' into multi_string
medha-14 Dec 20, 2024
0afd38b
style: pre-commit fixes
pre-commit-ci[bot] Dec 20, 2024
2e59daa
Merge branch 'develop' into multi_string
kratman Dec 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions src/pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,41 +102,45 @@
f"'{key}' not found. Best matches are {best_matches}"
) from error

def search(self, key, print_values=False):
def search(self, keys, print_values=False):
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
"""
Search dictionary for keys containing 'key'. If print_values is True, then
both the keys and values will be printed. Otherwise just the values will
be printed. If no results are found, the best matches are printed.
Search dictionary for keys containing all terms in 'keys'.
If print_values is True, both the keys and values will be printed.
Otherwise, just the keys will be printed. If no results are found,
the best matches are printed.
medha-14 marked this conversation as resolved.
Show resolved Hide resolved
"""
key_in = key
key = key_in.lower()
if isinstance(keys, str):
keys = [keys]
medha-14 marked this conversation as resolved.
Show resolved Hide resolved

# Sort the keys so results are stored in alphabetical order
keys = list(self.keys())
keys.sort()
keys_lower = [key.lower() for key in keys]

dict_keys = list(self.keys())
dict_keys.sort()
medha-14 marked this conversation as resolved.
Show resolved Hide resolved
results = {}

# Check if any of the dict keys contain the key we are searching for
for k in keys:
if key in k.lower():
# Check if all search terms are present in each dictionary key
for k in dict_keys:
k_lower = k.lower()
if all(term in k_lower for term in keys_lower):
medha-14 marked this conversation as resolved.
Show resolved Hide resolved
results[k] = self[k]

if results == {}:
# If no results, return best matches
best_matches = self.get_best_matches(key)
# No results: Suggest best matches using the concatenated string
concatenated_keys = " ".join(keys_lower)
best_matches = self.get_best_matches(concatenated_keys)
medha-14 marked this conversation as resolved.
Show resolved Hide resolved
print(
f"No results for search using '{key_in}'. "
f"No results for search using {keys}. "
f"Best matches are {best_matches}"
)
elif print_values:
# Else print results, including dict items
# Print keys and values of matching results
print("\n".join(f"{k}\t{v}" for k, v in results.items()))
else:
# Just print keys
print("\n".join(f"{k}" for k in results.keys()))
# Print only keys of matching results
print("\n".join(results.keys()))

def copy(self):
return FuzzyDict(super().copy())
def copy(self):
return FuzzyDict(super().copy())

Check warning on line 143 in src/pybamm/util.py

View check run for this annotation

Codecov / codecov/patch

src/pybamm/util.py#L143

Added line #L143 was not covered by tests
medha-14 marked this conversation as resolved.
Show resolved Hide resolved


class Timer:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def test_url_gets_to_stdout(self, mocker):
with mocker.patch("sys.stdout", new=StringIO()) as fake_out:
model.variables.search("Electrolyte cot")
out = (
"No results for search using 'Electrolyte cot'. "
"No results for search using ['Electrolyte cot']. "
"Best matches are ['Electrolyte concentration', "
"'Electrode potential']\n"
)
Expand Down
Loading