diff --git a/.vscode/settings.json b/.vscode/settings.json index becb0ac..bd636e5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -87,6 +87,7 @@ "texttt", "TLDR", "uncert", + "uncerts", "usepackage" ] } \ No newline at end of file diff --git a/src/api/console_stringifier.py b/src/api/console_stringifier.py index e3a77db..d5d13ce 100644 --- a/src/api/console_stringifier.py +++ b/src/api/console_stringifier.py @@ -27,7 +27,7 @@ def result_to_str(self, result: Result): Returns the result as human-readable string. """ - return f"{result.name} = {self.create_str(result.value, result.uncertainties, result.unit)}" + return f"{result.name} = {self.create_str(result)}" def _modify_unit(self, unit: str) -> str: """ diff --git a/src/api/printable_result.py b/src/api/printable_result.py index 8718959..e429f77 100644 --- a/src/api/printable_result.py +++ b/src/api/printable_result.py @@ -1,7 +1,9 @@ from api.console_stringifier import ConsoleStringifier import api.config as c from api.latexer import get_latexer +from api.printable_uncertainty import PrintableUncertainty from domain.result import Result +from application import error_messages class PrintableResult: @@ -13,6 +15,46 @@ def print(self): stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) print(stringifier.result_to_str(self._result)) + @property + def string(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str(self._result) + + @property + def string_value(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str_value(self._result) + + @property + def string_without_uncert(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str_without_uncert(self._result) + + @property + def uncerts(self) -> list[PrintableUncertainty]: + return [PrintableUncertainty(u, self._result.unit) for u in self._result.uncertainties] + + @property + def string_uncert_total(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + short_result = self._result.get_short_result() + if short_result is None: + raise RuntimeError(error_messages.SHORT_RESULT_IS_NONE) + return stringifier.create_str_uncert(short_result.uncertainties[0], self._result.unit) + + @property + def string_short(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + short_result = self._result.get_short_result() + if short_result is None: + raise RuntimeError(error_messages.SHORT_RESULT_IS_NONE) + return stringifier.create_str(short_result) + + @property + def string_without_unit(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str_without_unit(self._result) + def to_latex_str(self) -> str: """Converts the result to a string that can be used in LaTeX documents. diff --git a/src/api/printable_uncertainty.py b/src/api/printable_uncertainty.py new file mode 100644 index 0000000..768b3b7 --- /dev/null +++ b/src/api/printable_uncertainty.py @@ -0,0 +1,15 @@ +from domain.uncertainty import Uncertainty +from api.console_stringifier import ConsoleStringifier +import api.config as c + + +class PrintableUncertainty: + def __init__(self, uncert: Uncertainty, unit: str): + self._uncert = uncert + self._unit = unit + + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + + self.name = uncert.name + self.string = stringifier.create_str_uncert(uncert, unit) + self.value = uncert.uncertainty.get() diff --git a/src/application/latex_commandifier.py b/src/application/latex_commandifier.py index 8012f41..299924e 100644 --- a/src/application/latex_commandifier.py +++ b/src/application/latex_commandifier.py @@ -23,12 +23,12 @@ def result_to_latex_cmd(self, result: Result) -> str: latex_str = rf"\newcommand*{{\{cmd_name}}}[1][]{{" + "\n" # Default case (full result) & value - builder.add_branch("", self.result_to_latex_str(result)) - builder.add_branch("value", self.result_to_latex_str_value(result)) + builder.add_branch("", self.s.create_str(result)) + builder.add_branch("value", self.s.create_str_value(result)) # Without uncertainty if len(result.uncertainties) > 0: - builder.add_branch("withoutUncert", self.result_to_latex_str_without_uncert(result)) + builder.add_branch("withoutUncert", self.s.create_str_without_uncert(result)) # Single uncertainties for i, u in enumerate(result.uncertainties): @@ -37,7 +37,7 @@ def result_to_latex_cmd(self, result: Result) -> str: else: uncertainty_name = u.name if u.name != "" else Helpers.number_to_word(i + 1) uncertainty_name = f"uncert{Helpers.capitalize(uncertainty_name)}" - uncertainty_latex_str = self.s.create_str(u.uncertainty, [], result.unit) + uncertainty_latex_str = self.s.create_str_uncert(u, result.unit) builder.add_branch(uncertainty_name, uncertainty_latex_str) # Total uncertainty and short result @@ -45,16 +45,16 @@ def result_to_latex_cmd(self, result: Result) -> str: short_result = result.get_short_result() if short_result is None: raise RuntimeError(error_messages.SHORT_RESULT_IS_NONE) - uncertainty_latex_str = self.s.create_str( - short_result.uncertainties[0].uncertainty, [], result.unit + uncertainty_latex_str = self.s.create_str_uncert( + short_result.uncertainties[0], result.unit ) builder.add_branch("uncertTotal", uncertainty_latex_str) - builder.add_branch("short", self.result_to_latex_str(short_result)) + builder.add_branch("short", self.s.create_str(short_result)) # Unit if result.unit != "": builder.add_branch("unit", rf"\unit{{{result.unit}}}") - builder.add_branch("withoutUnit", self.result_to_latex_str_without_unit(result)) + builder.add_branch("withoutUnit", self.s.create_str_without_unit(result)) # Error message keywords = builder.keywords @@ -74,22 +74,4 @@ def result_to_latex_str(self, result: Result) -> str: """ Returns the result as LaTeX string making use of the siunitx package. """ - return self.s.create_str(result.value, result.uncertainties, result.unit) - - def result_to_latex_str_value(self, result: Result) -> str: - """ - Returns only the value as LaTeX string making use of the siunitx package. - """ - return self.s.create_str(result.value, [], "") - - def result_to_latex_str_without_uncert(self, result: Result) -> str: - """ - Returns the result without uncertainty as LaTeX string making use of the siunitx package. - """ - return self.s.create_str(result.value, [], result.unit) - - def result_to_latex_str_without_unit(self, result: Result) -> str: - """ - Returns the result without unit as LaTeX string making use of the siunitx package. - """ - return self.s.create_str(result.value, result.uncertainties, "") + return self.s.create_str(result) diff --git a/src/application/stringifier.py b/src/application/stringifier.py index 35d7040..6e5afb5 100644 --- a/src/application/stringifier.py +++ b/src/application/stringifier.py @@ -2,6 +2,7 @@ from typing import List, Tuple from typing import Protocol, ClassVar from decimal import Decimal +from domain.result import Result # for why we use a Protocol instead of a ABC class, see # https://github.com/microsoft/pyright/issues/2601#issuecomment-977053380 @@ -51,7 +52,7 @@ class Stringifier(Protocol): def __init__(self, config: StringifierConfig): self.config = config - def create_str(self, value: Value, uncertainties: List[Uncertainty], unit: str) -> str: + def _create_str(self, value: Value, uncertainties: List[Uncertainty], unit: str) -> str: """ Returns the result as LaTeX string making use of the siunitx package. @@ -84,6 +85,26 @@ def create_str(self, value: Value, uncertainties: List[Uncertainty], unit: str) unit, ) + def create_str(self, result: Result) -> str: + """ + Returns the result as LaTeX string making use of the siunitx package. + + This string does not yet contain "\newcommand*{}". + """ + return self._create_str(result.value, result.uncertainties, result.unit) + + def create_str_uncert(self, uncertainty: Uncertainty, unit: str) -> str: + return self._create_str(uncertainty.uncertainty, [], unit) + + def create_str_value(self, result: Result) -> str: + return self._create_str(result.value, [], "") + + def create_str_without_uncert(self, result: Result) -> str: + return self._create_str(result.value, [], result.unit) + + def create_str_without_unit(self, result: Result) -> str: + return self._create_str(result.value, result.uncertainties, "") + # pylint: disable-next=too-many-arguments def _assemble_str_parts( self,