-
Notifications
You must be signed in to change notification settings - Fork 161
Add suntools.table module #812
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: develop
Are you sure you want to change the base?
Conversation
| try: | ||
| if "." in value: | ||
| value = float(value) | ||
| else: | ||
| value = int(value) | ||
| except ValueError: | ||
| pass # Keep value as string if conversion fails |
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.
Similar parsing is done at
sundials/tools/suntools/csv.py
Lines 22 to 31 in c8dabce
| def num(s): | |
| """Try to convert a string to an int or float""" | |
| try: | |
| return int(s) | |
| except ValueError: | |
| try: | |
| return float(s) | |
| except ValueError: | |
| return s |
Could consider making this into a shared function that both files can use.
| Args: | ||
| data (str): Multiline string containing repeated key-value tables. | ||
| Returns: | ||
| list: A dictionary, where each key is a list of the values. |
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.
| list: A dictionary, where each key is a list of the values. | |
| stats: A dictionary, where each key is a list of the values. |
Calling the output list when it is a dict is a bit confusing
| lines = data.splitlines() | ||
| for line in lines: | ||
| # Extract key-value pairs | ||
| match = re.match(r"^(.*?)\s+=\s+(.*)$", line) |
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.
| lines = data.splitlines() | |
| for line in lines: | |
| # Extract key-value pairs | |
| match = re.match(r"^(.*?)\s+=\s+(.*)$", line) | |
| lines = data.splitlines() | |
| regexp = re.compile(r"^(.*?)\s+=\s+(.*)$") | |
| for line in lines: | |
| # Extract key-value pairs | |
| match = regexp.match(line) |
Untested, but should be a bit more efficient.
Parses stats that were printed in the table format instead of csv.