-
-
Notifications
You must be signed in to change notification settings - Fork 597
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
Conversation
src/pybamm/util.py
Outdated
Retrieves the value associated with a key, handling renamed terms and | ||
suggesting closest matches if the key is not found. | ||
|
||
_find_matches(search_key, known_keys) |
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.
Private methods should not be documented
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.
Okay I have removed the private methods.
src/pybamm/util.py
Outdated
__getitem__(key) | ||
Retrieves the value associated with a key, handling renamed terms and | ||
suggesting closest matches if the key is not found. |
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.
This is probably better documented as an example rather then the method itself
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #4846 +/- ##
========================================
Coverage 98.70% 98.70%
========================================
Files 303 303
Lines 23335 23335
========================================
Hits 23032 23032
Misses 303 303 ☔ View full report in Codecov by Sentry. |
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. |
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.
This looks like an AI generated text 🙁
Anyways, it is redundant. The first sentence makes everything clear.
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. |
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.
Extremely sorry, I have re-written the doc-string and added an example case.
src/pybamm/util.py
Outdated
Features: | ||
- Fuzzy matching using `difflib.get_close_matches` to suggest the closest keys. | ||
- Custom error handling for specific key renamings with informative messages. | ||
- Search functionality to find keys containing specific terms. | ||
- Custom warnings for deprecated key names. |
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.
Does not add anything. I am not against AI, but it is always important to check the results before adding them in.
Features: | |
- Fuzzy matching using `difflib.get_close_matches` to suggest the closest keys. | |
- Custom error handling for specific key renamings with informative messages. | |
- Search functionality to find keys containing specific terms. | |
- Custom warnings for deprecated key names. |
src/pybamm/util.py
Outdated
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. |
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.
Methods and their respective docstrings are automatically rendered in the docs
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. |
src/pybamm/util.py
Outdated
This class extends the built-in `dict` to provide intelligent key lookup, | ||
including approximate string matching and handling of renamed terms. It is | ||
useful when working with parameter dictionaries where keys might have slight | ||
variations, renamings, or formatting differences. |
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.
The first sentence captures the gist here too, so these lines look redundant to me.
src/pybamm/util.py
Outdated
as the original dictionary. It ensures that the copied dictionary retains | ||
the fuzzy matching behavior. | ||
|
||
Returns: |
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.
Returns: | |
Returns | |
------- |
@@ -96,7 +130,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 comment
The reason will be displayed to describe this comment to others. Learn more.
------- | |
---------- |
@@ -115,7 +149,7 @@ def search(self, keys: str | list[str], print_values: bool = False): | |||
the best matches are printed. | |||
|
|||
Parameters | |||
---------- | |||
------- |
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.
------- | |
---------- |
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. | ||
|
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.
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. |
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?
src/pybamm/util.py
Outdated
Example usage | ||
------- | ||
```python | ||
>>> 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 (direct access) | ||
>>> print(params["particle diffusivity"]) # 0.8 (handles renaming) | ||
|
||
>>> params.search("open circuit voltage") | ||
|
||
>>> params_copy = params.copy() # Creates a new FuzzyDict instance | ||
``` | ||
""" |
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.
You will need to fix this examples as the doctests will fail
Example usage | |
------- | |
```python | |
>>> 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 (direct access) | |
>>> print(params["particle diffusivity"]) # 0.8 (handles renaming) | |
>>> params.search("open circuit voltage") | |
>>> params_copy = params.copy() # Creates a new 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 | |
""" |
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.
Modified the example.
Description
In this PR fixed the following bugs in
pybamm.util.py
:Fixes #4845
Type of change
Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)
Important checks:
Please confirm the following before marking the PR as ready for review:
nox -s pre-commit
nox -s tests
nox -s doctests