Skip to content

Commit

Permalink
Use Protocol for MasterStringifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Mar 15, 2024
1 parent 557d860 commit dca2e75
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/api/printable_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def __init__(self, result: _Result):

def print(self):
"""Prints the result to the console."""
print(Stringifier(c.configuration.to_stringifier_config()).result_to_str(self._result))
stringifier = Stringifier(c.configuration.to_stringifier_config())
print(stringifier.result_to_str(self._result))

def to_latex_str(self) -> str:
"""Converts the result to a string that can be used in LaTeX documents.
Expand Down
16 changes: 16 additions & 0 deletions src/api/stringifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@


class Stringifier(MasterStringifier):
plus_minus = " ± "
negative_sign = "-"
positive_sign = ""

left_parenthesis = "("
right_parenthesis = ")"

error_name_prefix = " ("
error_name_suffix = ")"

scientific_notation_prefix = "e"
scientific_notation_suffix = ""

unit_prefix = " "
unit_suffix = ""

def result_to_str(self, result: _Result):
"""
Returns the result as human-readable string.
Expand Down
27 changes: 12 additions & 15 deletions src/application/latexer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from domain.result import _Result
from application.helpers import _Helpers
from application.latexer_ifelse import LatexIfElseBuilder
from application.master_stringifier import StringifierConfig, MasterStringifier
from application.master_stringifier import MasterStringifier


class _LaTeXer(MasterStringifier):
Expand All @@ -11,24 +11,21 @@ class _LaTeXer(MasterStringifier):
We assume the result to already be correctly rounded at this point.
"""

def __init__(self, config: StringifierConfig):
super().__init__(config)
plus_minus = r" \pm "
negative_sign = "-"
positive_sign = ""

self.plus_minus = r" \pm "
self.negative_sign = "-"
self.positive_sign = ""
left_parenthesis = r"\left("
right_parenthesis = r"\right)"

self.left_parenthesis = r"\left("
self.right_parenthesis = r"\right)"
error_name_prefix = r"_{\text{"
error_name_suffix = r"}}"

self.error_name_prefix = r"_{\text{"
self.error_name_suffix = r"}}"
scientific_notation_prefix = r" \cdot 10^{"
scientific_notation_suffix = "}"

self.scientific_notation_prefix = r" \cdot 10^{"
self.scientific_notation_suffix = "}"

self.unit_prefix = r"\, \unit{"
self.unit_suffix = "}"
unit_prefix = r"\, \unit{"
unit_suffix = "}"

def result_to_latex_cmd(self, result: _Result) -> str:
"""
Expand Down
34 changes: 20 additions & 14 deletions src/application/master_stringifier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from dataclasses import dataclass
from typing import List
from typing import Protocol, ClassVar

# for why we use a Protocol instead of a ABC class, see
# https://github.com/microsoft/pyright/issues/2601#issuecomment-977053380

from domain.value import _Value
from domain.uncertainty import _Uncertainty
Expand All @@ -13,31 +17,33 @@ class StringifierConfig:
identifier: str


class MasterStringifier:
class MasterStringifier(Protocol):
"""
Provides methods to convert results to strings of customizable pattern.
We assume the result to already be correctly rounded at this point.
"""

def __init__(self, config: StringifierConfig):
self.config = config
config: StringifierConfig

self.plus_minus = " ± "
self.negative_sign = "-"
self.positive_sign = ""
plus_minus: ClassVar[str]
negative_sign: ClassVar[str]
positive_sign: ClassVar[str]

self.left_parenthesis = "("
self.right_parenthesis = ")"
left_parenthesis: ClassVar[str]
right_parenthesis: ClassVar[str]

self.error_name_prefix = " ("
self.error_name_suffix = ")"
error_name_prefix: ClassVar[str]
error_name_suffix: ClassVar[str]

self.scientific_notation_prefix = "e"
self.scientific_notation_suffix = ""
scientific_notation_prefix: ClassVar[str]
scientific_notation_suffix: ClassVar[str]

self.unit_prefix = " "
self.unit_suffix = ""
unit_prefix: ClassVar[str]
unit_suffix: ClassVar[str]

def __init__(self, config: StringifierConfig):
self.config = config

def create_str(self, value: _Value, uncertainties: List[_Uncertainty], unit: str) -> str:
"""
Expand Down

0 comments on commit dca2e75

Please sign in to comment.