-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dhuynh95
committed
Oct 2, 2024
1 parent
5113f98
commit 5b194b9
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,27 @@ | ||
import re | ||
|
||
def quote_numeric_yaml_values(yaml_string: str) -> str: | ||
"""Wrap numeric values in quotes in a YAML string. | ||
This is helpful when the YAML might contain numeric values that are not wrapped in quotes which can cause issues when parsing the YAML. | ||
In Navigation Engine, we expect values to be always strings, so this avoids issues when the YAML does not wrap numeric values in quotes, such as outputting "value: 01" instead of "value: '01'". | ||
""" | ||
|
||
def replace_value(match): | ||
full_match, value = match.groups() | ||
try: | ||
# Try to convert to float to catch both integers and decimal numbers | ||
float(value) | ||
# If successful, it's a number, so wrap it in quotes | ||
return f'value: "{value}"' | ||
except ValueError: | ||
# If it's not a number, return the original match | ||
return full_match | ||
|
||
# Regex to match 'value:' followed by a space and then any numeric | ||
pattern = r"(value: ([\d.]+))" | ||
|
||
# Replace values that are numeric | ||
modified_yaml = re.sub(pattern, replace_value, yaml_string) | ||
|
||
return modified_yaml |