From 27b12b82cf4bad15a850c4be2967cc352f74f0ff Mon Sep 17 00:00:00 2001 From: paul019 <39464035+paul019@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:47:48 +0200 Subject: [PATCH 1/4] First commit --- src/api/console_stringifier.py | 2 +- src/api/printable_result.py | 41 +++++++++++++++++++++++++++ src/application/latex_commandifier.py | 36 ++++++----------------- src/application/stringifier.py | 23 ++++++++++++++- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/api/console_stringifier.py b/src/api/console_stringifier.py index e3a77dbc..d5d13ce6 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 87189598..c7ab19af 100644 --- a/src/api/printable_result.py +++ b/src/api/printable_result.py @@ -2,6 +2,7 @@ import api.config as c from api.latexer import get_latexer from domain.result import Result +from application import error_messages class PrintableResult: @@ -13,6 +14,46 @@ def print(self): stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) print(stringifier.result_to_str(self._result)) + def get_str(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str(self._result) + + def get_str_value(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str_value(self._result) + + def get_str_without_uncert(self) -> str: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return stringifier.create_str_without_uncert(self._result) + + def get_uncerts(self) -> list[dict[str, str]]: + stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) + return [ + { + "name": u.name, + "string": stringifier.create_str_uncert(u, self._result.unit), + } + for u in self._result.uncertainties + ] + + def get_str_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) + + def get_str_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) + + def get_str_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/application/latex_commandifier.py b/src/application/latex_commandifier.py index 8012f41a..299924ef 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 35d70406..6e5afb5c 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, From bb864413b12886d4601f200ffd7ab006503e744a Mon Sep 17 00:00:00 2001 From: paul019 <39464035+paul019@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:13:00 +0200 Subject: [PATCH 2/4] Replace getters by properties --- src/api/printable_result.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/api/printable_result.py b/src/api/printable_result.py index c7ab19af..d1ca3dab 100644 --- a/src/api/printable_result.py +++ b/src/api/printable_result.py @@ -14,19 +14,23 @@ def print(self): stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) print(stringifier.result_to_str(self._result)) - def get_str(self) -> str: + @property + def string(self) -> str: stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) return stringifier.create_str(self._result) - def get_str_value(self) -> str: + @property + def string_value(self) -> str: stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) return stringifier.create_str_value(self._result) - def get_str_without_uncert(self) -> str: + @property + def string_without_uncert(self) -> str: stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) return stringifier.create_str_without_uncert(self._result) - def get_uncerts(self) -> list[dict[str, str]]: + @property + def uncerts(self) -> list[dict[str, str]]: stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) return [ { @@ -36,21 +40,24 @@ def get_uncerts(self) -> list[dict[str, str]]: for u in self._result.uncertainties ] - def get_str_uncert_total(self) -> str: + @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) - def get_str_short(self) -> str: + @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) - def get_str_without_unit(self) -> str: + @property + def string_without_unit(self) -> str: stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) return stringifier.create_str_without_unit(self._result) From 304996ecc75f0d9b674eed33335773f68759f97d Mon Sep 17 00:00:00 2001 From: paul019 <39464035+paul019@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:04:21 +0200 Subject: [PATCH 3/4] Add word "uncerts" to workspace settings --- .vscode/settings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index becb0acc..bd636e55 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -87,6 +87,7 @@ "texttt", "TLDR", "uncert", + "uncerts", "usepackage" ] } \ No newline at end of file From 466c349a2026a544bae42336890d17534126beb3 Mon Sep 17 00:00:00 2001 From: paul019 <39464035+paul019@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:13:03 +0200 Subject: [PATCH 4/4] Introduce proper type for printable uncertainties --- src/api/printable_result.py | 12 +++--------- src/api/printable_uncertainty.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 src/api/printable_uncertainty.py diff --git a/src/api/printable_result.py b/src/api/printable_result.py index d1ca3dab..e429f77e 100644 --- a/src/api/printable_result.py +++ b/src/api/printable_result.py @@ -1,6 +1,7 @@ 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 @@ -30,15 +31,8 @@ def string_without_uncert(self) -> str: return stringifier.create_str_without_uncert(self._result) @property - def uncerts(self) -> list[dict[str, str]]: - stringifier = ConsoleStringifier(c.configuration.to_stringifier_config()) - return [ - { - "name": u.name, - "string": stringifier.create_str_uncert(u, self._result.unit), - } - for u in self._result.uncertainties - ] + def uncerts(self) -> list[PrintableUncertainty]: + return [PrintableUncertainty(u, self._result.unit) for u in self._result.uncertainties] @property def string_uncert_total(self) -> str: diff --git a/src/api/printable_uncertainty.py b/src/api/printable_uncertainty.py new file mode 100644 index 00000000..768b3b76 --- /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()