Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
medha-14 committed Dec 12, 2024
1 parent d9e5bcd commit 6b953a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
28 changes: 16 additions & 12 deletions src/pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,39 @@ def search(self, keys, print_values=False):
known_keys = list(self.keys())
known_keys.sort()

# Check for exact matches where all terms appear in the same key
# Check for exact matches where all search keys appear together in a key
exact_matches = [
key
for key in known_keys
if all(term in key.lower() for term in search_keys)
]

if exact_matches:
print(f"Results for '{' '.join(original_keys)}': {exact_matches}")
if print_values:
for match in exact_matches:
print(f"{match} -> {self[match]}")
return

# If no exact matches, find partial matches for each individual search key
# If no exact matches, iterate over search keys individually
for original_key, search_key in zip(original_keys, search_keys):
partial_matches = [key for key in known_keys if search_key in key.lower()]

if partial_matches:
print(
f"No exact matches found for '{original_key}'. Best matches are: {partial_matches}"
)
# Find exact matches for this specific search key
exact_key_matches = [key for key in known_keys if search_key in key.lower()]

if exact_key_matches:
print(f"Exact matches for '{original_key}': {exact_key_matches}")
if print_values:
for match in exact_key_matches:
print(f"{match} -> {self[match]}")
else:
# If no partial matches, suggest best matches
best_matches = difflib.get_close_matches(
# Find the best partial matches for this specific search key
partial_matches = difflib.get_close_matches(
search_key, known_keys, n=5, cutoff=0.5
)
if best_matches:

if partial_matches:
print(
f"No exact matches found for '{original_key}'. Best matches are: {best_matches}"
f"No exact matches found for '{original_key}'. Best matches are: {partial_matches}"
)
else:
print(f"No matches found for '{original_key}'.")
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ def test_url_gets_to_stdout(self, mocker):

# Test for multiple strings as input (default returns best matches)
with mocker.patch("sys.stdout", new=StringIO()) as fake_out:
model.variables.search(["Electrolyte", "Poten"])
model.variables.search(["Electrolyte", "Potenteel"])
out = (
"No exact matches found for 'Electrolyte'. Best matches are: ['Electrolyte concentration']\n"
"No exact matches found for 'Poten'. Best matches are: ['Electrode potential']\n"
"Exact matches for 'Electrolyte': ['Electrolyte concentration']\n"
"No exact matches found for 'Potenteel'. Best matches are: ['Electrode potential']\n"
)
assert fake_out.getvalue() == out
# Test param search (default returns key, value)
Expand Down

0 comments on commit 6b953a1

Please sign in to comment.