Skip to content

Commit

Permalink
Replace per by / in console
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Sep 11, 2024
1 parent 6d90130 commit 9be9b27
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/api/console_stringifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,24 @@ def _modify_unit(self, unit: str) -> str:
modified_unit += f"({' '.join(denominator_parts)})"

modified_unit = self.strip_whitespaces_around_parentheses(modified_unit)
modified_unit = self.replace_per_by_symbol(modified_unit)

return modified_unit

def strip_whitespaces_around_parentheses(self, string: str) -> str:
return string.replace(" (", "(").replace("( ", "(").replace(" )", ")").replace(") ", ")")

def replace_per_by_symbol(self, string: str) -> str:
"""
Replaces all occurrences of `per` with `/`.
This might be necessary due to limitations of the above parsing method
where `per(` is recognized as a single token. For a proper parser, we
would have to deal with parentheses in a more sophisticated way. As this
is not the scope of this project for now, we just do a simple replacement
of the `per` that slipped through the above logic.
Note that at this point, `\percent` was already replaced by `%`, so
we can safely replace all occurrences of "per" with "/".
"""
return string.replace("per", " / ")

0 comments on commit 9be9b27

Please # to comment.