-
Notifications
You must be signed in to change notification settings - Fork 9
Normalize version fields to strings in import scripts #39
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
Open
arash77
wants to merge
4
commits into
research-software-ecosystem:main
Choose a base branch
from
arash77:normalize-version-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c1bb215
Normalize version fields to strings in import scripts
arash77 386ca09
Refactor version normalization to target only the main version field
arash77 a1825cd
Enhance version normalization functions with detailed docstrings and …
arash77 14e7448
Remove obsolete subproject content directory
arash77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import logging | ||
|
|
||
|
|
||
| def normalize_version_to_string(value): | ||
| """ | ||
| Recursively convert version values to strings. | ||
|
|
||
| This function processes version data by converting numeric types to strings | ||
| while preserving None and boolean values. It recursively processes nested | ||
| structures (lists and dicts). | ||
|
|
||
| Args: | ||
| value: The value to normalize. Can be any type. | ||
|
|
||
| Returns: | ||
| - None and bool values are returned unchanged | ||
| - int and float values are converted to strings | ||
| - Lists are processed recursively, returning a new list with normalized values | ||
| - Dicts are processed recursively, returning a new dict with normalized values | ||
| - Other types are returned unchanged | ||
|
|
||
| Examples: | ||
| >>> normalize_version_to_string(1) | ||
| '1' | ||
| >>> normalize_version_to_string([1, 2, 3]) | ||
| ['1', '2', '3'] | ||
| >>> normalize_version_to_string({'version': 1.5}) | ||
| {'version': '1.5'} | ||
| """ | ||
| if value is None or isinstance(value, bool): | ||
| return value | ||
| if isinstance(value, (int, float)): | ||
| return str(value) | ||
| if isinstance(value, list): | ||
| return [normalize_version_to_string(v) for v in value] | ||
| if isinstance(value, dict): | ||
| return {k: normalize_version_to_string(v) for k, v in value.items()} | ||
| return value | ||
|
|
||
|
|
||
| def normalize_version_fields(data, field_paths): | ||
arash77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Normalize version fields to strings in a data dictionary. | ||
|
|
||
| This function takes a dictionary and a collection of field paths, then normalizes | ||
| the version values at those paths to strings using normalize_version_to_string. | ||
|
|
||
| Args: | ||
| data (dict): The dictionary to process. | ||
| field_paths (iterable): An iterable of field path strings. Supports: | ||
| - Simple fields: "version" | ||
| - Nested fields: "tool.version" | ||
| - List fields: "versions[]" | ||
| - List item nested fields: "versions[].version" | ||
|
|
||
| Returns: | ||
| dict: The modified data dictionary with normalized version fields. | ||
|
|
||
| Raises: | ||
| TypeError: If data is not a dictionary. | ||
|
|
||
| Examples: | ||
| >>> data = {"version": 1, "versions": [{"version": 2}]} | ||
| >>> normalize_version_fields(data, ["version", "versions[].version"]) | ||
| {'version': '1', 'versions': [{'version': '2'}]} | ||
| """ | ||
| if not isinstance(data, dict): | ||
| raise TypeError(f"Expected dict, got {type(data).__name__}") | ||
|
|
||
| for field_path in field_paths: | ||
| try: | ||
| if "[" in field_path: | ||
| if "[]." not in field_path: | ||
| list_key = field_path[:-2] if field_path.endswith("[]") else field_path | ||
| if list_key in data and isinstance(data[list_key], list): | ||
| data[list_key] = normalize_version_to_string(data[list_key]) | ||
| else: | ||
| list_key, item_path = field_path.split("[].", 1) | ||
| if list_key in data and isinstance(data[list_key], list): | ||
| for item in data[list_key]: | ||
| if isinstance(item, dict) and item_path in item: | ||
| item[item_path] = normalize_version_to_string( | ||
| item[item_path] | ||
| ) | ||
| elif "." in field_path: | ||
| keys = field_path.split(".") | ||
| current = data | ||
| for key in keys[:-1]: | ||
| if not isinstance(current, dict) or key not in current: | ||
| break | ||
| current = current[key] | ||
| else: | ||
| final_key = keys[-1] | ||
| if isinstance(current, dict) and final_key in current: | ||
| current[final_key] = normalize_version_to_string( | ||
| current[final_key] | ||
| ) | ||
| else: | ||
| if field_path in data: | ||
| data[field_path] = normalize_version_to_string(data[field_path]) | ||
| except (KeyError, TypeError, IndexError, AttributeError) as e: | ||
| logging.debug(f"Skipping field path '{field_path}': {e}") | ||
| continue | ||
|
|
||
| return data | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.