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

Add Fuzzydict docstring #4846

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from
49 changes: 47 additions & 2 deletions src/pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ def root_dir():


class FuzzyDict(dict):
"""
A dictionary with fuzzy matching capabilities for key retrieval and search.

Methods
-------
get_best_matches(key)
Returns a list of the best-matching keys for a given input key.

search(keys, print_values=False)
Searches the dictionary for keys containing all specified terms. Prints
the results and, optionally, the corresponding values.

copy()
Returns a copy of the FuzzyDict instance.

Comment on lines +29 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Methods
-------
get_best_matches(key)
Returns a list of the best-matching keys for a given input key.
search(keys, print_values=False)
Searches the dictionary for keys containing all specified terms. Prints
the results and, optionally, the corresponding values.
copy()
Returns a copy of the FuzzyDict instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove this section?

Examples
--------
>>> params = FuzzyDict({
>>> "electrode diffusivity": 1.2,
>>> "particle diffusivity": 0.8,
>>> "Negative electrode SOC": 0.5,
>>> "Open-circuit voltage at 100% SOC [V]": 4.2,
>>> })
>>> print(params["electrode diffusivity"])
1.2
>>> print(params["particle diffusivity"])
0.8
>>> params.search("open circuit voltage")
>>> params_copy = params.copy() # Creates a new FuzzyDict instance

"""

def get_best_matches(self, key):
"""Get best matches from keys"""
return difflib.get_close_matches(key, list(self.keys()), n=3, cutoff=0.5)
Expand Down Expand Up @@ -96,7 +128,7 @@ def _find_matches(self, search_key: str, known_keys: list[str]):
Helper method to find exact and partial matches for a given search key.

Parameters
----------
-------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-------
----------

search_key : str
The term to search for in the keys.
known_keys : list of str
Expand All @@ -115,7 +147,7 @@ def search(self, keys: str | list[str], print_values: bool = False):
the best matches are printed.

Parameters
----------
-------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-------
----------

keys : str or list of str
Search term(s)
print_values : bool, optional
Expand Down Expand Up @@ -183,6 +215,19 @@ def search(self, keys: str | list[str], print_values: bool = False):
print(f"No matches found for '{original_key}'")

def copy(self):
"""
Create and return a copy of the FuzzyDict instance.

This method returns a new FuzzyDict object containing the same key-value pairs
as the original dictionary. It ensures that the copied dictionary retains
the fuzzy matching behavior.
Comment on lines +221 to +223
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an AI generated text 🙁

Anyways, it is redundant. The first sentence makes everything clear.

Suggested change
This method returns a new FuzzyDict object containing the same key-value pairs
as the original dictionary. It ensures that the copied dictionary retains
the fuzzy matching behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely sorry, I have re-written the doc-string and added an example case.


Returns
-------
FuzzyDict
A new FuzzyDict instance with the same data as the original.
"""

return FuzzyDict(super().copy())


Expand Down