-
Notifications
You must be signed in to change notification settings - Fork 0
Normalize hardware response keys across APIs #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Utility helpers for formatting hardware payloads.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any, Dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def normalize_hardware_keys(hardware: Dict[str, Any]) -> Dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Ensure lowercase hardware keys exist for their uppercase counterparts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Some hardware records may come from sources that use uppercase keys (e.g., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``FLOPS`` instead of ``flops``). This helper preserves the original keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while populating the expected lowercase versions when they are missing so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| that API serializers can rely on a consistent shape. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if hardware is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalized = dict(hardware) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key, value in hardware.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(key, str) and key.isupper(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lower_key = key.lower() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if lower_key not in normalized: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalized[lower_key] = value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return normalized | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+22
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. The current implementation of To make this utility more robust and handle all non-lowercase keys, I suggest modifying the logic to check if This change will ensure that all expected keys are normalized, making the API responses more consistent.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 is significant code duplication between this loop in
list_hardwareand a similar loop infilter_hardware(lines 282-300). To improve maintainability and reduce redundancy, consider extracting this logic into a private helper function.For example, you could create a function
_enhance_hardware_results:Then, both
list_hardwareandfilter_hardwarecan be simplified to: