Skip to content

Commit

Permalink
Improve convert func from string_util (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibFrgsGmz committed May 4, 2022
1 parent 526448a commit d53e5aa
Showing 1 changed file with 25 additions and 29 deletions.
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

0 comments on commit d53e5aa

Please sign in to comment.