Skip to content
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

Improve convert func from string_util #65

Merged
merged 1 commit into from
May 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 25 additions & 29 deletions src/fprime/util/string_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,31 @@ def format_string_template(format_str, given_values):
"""

def convert(match_obj, ignore_int):
if match_obj.group() is not None:
flags, width, precision, length, conversion_type = match_obj.groups()
format_template = ""
if flags:
format_template += f"{flags}"
if width:
format_template += f"{width}"
if precision:
format_template += f"{precision}"

if conversion_type:
if any(
[
str(conversion_type).lower() == "f",
str(conversion_type).lower() == "x",
str(conversion_type).lower() == "o",
str(conversion_type).lower() == "e",
]
):
format_template += f"{conversion_type}"
elif all([not ignore_int, str(conversion_type).lower() == "d"]):
format_template += f"{conversion_type}"

if format_template == "":
template = "{}"
else:
template = "{:" + format_template + "}"
return template
return match_obj
if match_obj.group() is None:
return match_obj
flags, width, precision, length, conversion_type = match_obj.groups()
format_template = ""
if flags:
format_template += f"{flags}"
if width:
format_template += f"{width}"
if precision:
format_template += f"{precision}"

if conversion_type:
if any(
[
str(conversion_type).lower() == "f",
str(conversion_type).lower() == "x",
str(conversion_type).lower() == "o",
str(conversion_type).lower() == "e",
]
):
format_template += f"{conversion_type}"
elif all([not ignore_int, str(conversion_type).lower() == "d"]):
format_template += f"{conversion_type}"

return "{}" if format_template == "" else "{:" + format_template + "}"

def convert_include_all(match_obj):
return convert(match_obj, ignore_int=False)
Expand Down