-
-
Notifications
You must be signed in to change notification settings - Fork 598
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
base: develop
Are you sure you want to change the base?
Add Fuzzydict docstring #4846
Changes from all commits
bfa847c
5c896d6
58e937e
2194231
577260a
b8abf29
f262534
b3d8262
a352e8f
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||
|
||||||||
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) | ||||||||
|
@@ -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 | ||||||||
---------- | ||||||||
------- | ||||||||
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.
Suggested change
|
||||||||
search_key : str | ||||||||
The term to search for in the keys. | ||||||||
known_keys : list of str | ||||||||
|
@@ -115,7 +147,7 @@ def search(self, keys: str | list[str], print_values: bool = False): | |||||||
the best matches are printed. | ||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
kratman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
------- | ||||||||
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.
Suggested change
|
||||||||
keys : str or list of str | ||||||||
Search term(s) | ||||||||
print_values : bool, optional | ||||||||
|
@@ -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
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. This looks like an AI generated text 🙁 Anyways, it is redundant. The first sentence makes everything clear.
Suggested change
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. 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()) | ||||||||
|
||||||||
|
||||||||
|
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.
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.
Should I remove this section?