Add Fuzzydict docstring#4846
Conversation
| 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.
Private methods should not be documented
There was a problem hiding this comment.
Okay I have removed the private methods.
| __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.
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.
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.
Extremely sorry, I have re-written the doc-string and added an example case.
| 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.
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. |
| 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.
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. |
| 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.
The first sentence captures the gist here too, so these lines look redundant to me.
| as the original dictionary. It ensures that the copied dictionary retains | ||
| the fuzzy matching behavior. | ||
|
|
||
| Returns: |
There was a problem hiding this comment.
| Returns: | |
| Returns | |
| ------- |
|
|
||
| Parameters | ||
| ---------- | ||
| ------- |
There was a problem hiding this comment.
| ------- | |
| ---------- |
|
|
||
| Parameters | ||
| ---------- | ||
| ------- |
There was a problem hiding this comment.
| ------- | |
| ---------- |
| 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.
| 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.
Should I remove this section?
| 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.
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.
Modified the example.
|
@Akhil-Sharma30 Are you still working on this? |
But I have already made the changes asked for, waiting for the review from your end. |
|
@Akhil-Sharma30 did you forget to push your changes? The comments above are still unresolved. |
Could you please verify reflected as per #4846. |
|
I don't see the new commits :( Please make sure your local branch is synced with the remote branch. |
|
@Akhil-Sharma30 Are you still working on this? |
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-commitnox -s testsnox -s doctests