diff --git a/src/api/config.py b/src/api/config.py index 035443a0..b3d6eb4f 100644 --- a/src/api/config.py +++ b/src/api/config.py @@ -7,8 +7,18 @@ def config_init( sigfigs: int = 2, decimal_places: int = 2, print_always: bool = False, + min_exponent_for_non_scientific_notation: int = -2, + max_exponent_for_non_scientific_notation: int = 3, + identifier: str = "res", ): - c = Config(sigfigs, decimal_places, print_always) + c = Config( + sigfigs, + decimal_places, + print_always, + min_exponent_for_non_scientific_notation, + max_exponent_for_non_scientific_notation, + identifier, + ) print(c.to_json_str()) return c diff --git a/src/api/export.py b/src/api/export.py index 5585ae2f..2a2a8f78 100644 --- a/src/api/export.py +++ b/src/api/export.py @@ -1,4 +1,5 @@ from api.res import _res_cache +from api.config import configuration from application.latexer import _LaTeXer @@ -22,8 +23,10 @@ def export(filepath: str): r"", r"% Define commands to print the results:", ] + + latexer = _LaTeXer(configuration.to_latex_config()) for result in results: - result_str = _LaTeXer.result_to_latex_cmd(result) + result_str = latexer.result_to_latex_cmd(result) cmds.append(result_str) # Write to file diff --git a/src/api/printable_result.py b/src/api/printable_result.py index 663b6826..226539ad 100644 --- a/src/api/printable_result.py +++ b/src/api/printable_result.py @@ -1,6 +1,7 @@ -from domain.result import _Result from api.stringifier import Stringifier +from api.config import configuration from application.latexer import _LaTeXer +from domain.result import _Result class PrintableResult: @@ -13,4 +14,5 @@ def print(self): def get_latex_str(self) -> str: """Returns LaTeX string.""" - return _LaTeXer.result_to_latex_str(self._result) + latexer = _LaTeXer(configuration.to_latex_config()) + return latexer.result_to_latex_str(self._result) diff --git a/src/application/config.py b/src/application/config.py index 88dfc77c..817a5d6b 100644 --- a/src/application/config.py +++ b/src/application/config.py @@ -1,4 +1,5 @@ from dataclasses import dataclass +from application.latexer import LaTeXConfig import json @@ -15,6 +16,16 @@ class Config: sigfigs: int decimal_places: int print_always: bool + min_exponent_for_non_scientific_notation: int + max_exponent_for_non_scientific_notation: int + identifier: str def to_json_str(self): return json.dumps(self.__dict__) + + def to_latex_config(self) -> LaTeXConfig: + return LaTeXConfig( + self.min_exponent_for_non_scientific_notation, + self.max_exponent_for_non_scientific_notation, + self.identifier, + ) diff --git a/src/application/latexer.py b/src/application/latexer.py index 7db0ed1e..bb4e5935 100644 --- a/src/application/latexer.py +++ b/src/application/latexer.py @@ -1,3 +1,4 @@ +from dataclasses import dataclass from typing import List import textwrap @@ -8,25 +9,30 @@ from application.rounder import _Rounder -# Config values: -min_exponent_for_non_scientific_notation = -2 -max_exponent_for_non_scientific_notation = 3 -identifier = "res" +@dataclass +class LaTeXConfig: + min_exponent_for_non_scientific_notation: int + max_exponent_for_non_scientific_notation: int + identifier: str class _LaTeXer: - @classmethod - def result_to_latex_cmd(cls, result: _Result) -> str: + """Provides methods to convert results to LaTeX commands.""" + + def __init__(self, config: LaTeXConfig): + self.config = config + + def result_to_latex_cmd(self, result: _Result) -> str: """ Returns the result as LaTeX command to be used in a .tex file. """ - cmd_name = identifier + result.name[0].upper() + result.name[1:] + cmd_name = self.config.identifier + result.name[0].upper() + result.name[1:] latex_str = rf""" \newcommand*{{\{cmd_name}}}[1][]{{ \ifthenelse{{\equal{{#1}}{{}}}}{{ - {cls.result_to_latex_str(result)} + {self.result_to_latex_str(result)} """ latex_str = textwrap.dedent(latex_str).strip() @@ -38,7 +44,7 @@ def result_to_latex_cmd(cls, result: _Result) -> str: latex_str += "\n" latex_str += r" }{\ifthenelse{\equal{#1}{valueOnly}}{" latex_str += "\n" - latex_str += rf" {cls.result_to_latex_str_value_only(result)}" + latex_str += rf" {self.result_to_latex_str_value_only(result)}" keywords.append("valueOnly") number_of_parentheses_to_close += 1 @@ -53,7 +59,7 @@ def result_to_latex_cmd(cls, result: _Result) -> str: else: uncertainty_name = _Helpers.number_to_word(i + 1) uncertainty_name = "error" + uncertainty_name[0].upper() + uncertainty_name[1:] - error_latex_str = cls._create_latex_str(u.uncertainty, [], result.unit) + error_latex_str = self._create_latex_str(u.uncertainty, [], result.unit) latex_str += "\n" latex_str += rf" }}{{\ifthenelse{{\equal{{#1}}{{{uncertainty_name}}}}}{{" @@ -68,7 +74,7 @@ def result_to_latex_cmd(cls, result: _Result) -> str: short_result = result.get_short_result() _Rounder.round_result(short_result) - error_latex_str = cls._create_latex_str( + error_latex_str = self._create_latex_str( short_result.uncertainties[0].uncertainty, [], result.unit ) @@ -83,7 +89,7 @@ def result_to_latex_cmd(cls, result: _Result) -> str: latex_str += "\n" latex_str += r" }{\ifthenelse{\equal{#1}{short}}{" latex_str += "\n" - latex_str += rf" {cls.result_to_latex_str(short_result)}" + latex_str += rf" {self.result_to_latex_str(short_result)}" keywords.append("short") number_of_parentheses_to_close += 1 @@ -118,22 +124,19 @@ def result_to_latex_cmd(cls, result: _Result) -> str: return latex_str - @classmethod - def result_to_latex_str(cls, result: _Result) -> str: + def result_to_latex_str(self, result: _Result) -> str: """ Returns the result as LaTeX string making use of the siunitx package. """ - return cls._create_latex_str(result.value, result.uncertainties, result.unit) + return self._create_latex_str(result.value, result.uncertainties, result.unit) - @classmethod - def result_to_latex_str_value_only(cls, result: _Result) -> str: + def result_to_latex_str_value_only(self, result: _Result) -> str: """ Returns only the value as LaTeX string making use of the siunitx package. """ - return cls._create_latex_str(result.value, [], result.unit) + return self._create_latex_str(result.value, [], result.unit) - @classmethod - def _create_latex_str(cls, value: _Value, uncertainties: List[_Uncertainty], unit: str) -> str: + def _create_latex_str(self, value: _Value, uncertainties: List[_Uncertainty], unit: str) -> str: """ Returns the result as LaTeX string making use of the siunitx package. @@ -146,8 +149,8 @@ def _create_latex_str(cls, value: _Value, uncertainties: List[_Uncertainty], uni # Determine if scientific notation should be used: if ( - value.get_exponent() < min_exponent_for_non_scientific_notation - or value.get_exponent() > max_exponent_for_non_scientific_notation + value.get_exponent() < self.config.min_exponent_for_non_scientific_notation + or value.get_exponent() > self.config.max_exponent_for_non_scientific_notation ): use_scientific_notation = True