diff --git a/src/api/config.py b/src/api/config.py index 7951687a..122bb068 100644 --- a/src/api/config.py +++ b/src/api/config.py @@ -7,7 +7,7 @@ @dataclass # pylint: disable-next=too-many-instance-attributes -class _Config: +class Config: """Configuration settings for the application. Args: @@ -90,7 +90,7 @@ def config_init( ) -> None: global configuration # pylint: disable=global-statement - configuration = _Config( + configuration = Config( sigfigs, decimal_places, print_auto, @@ -104,7 +104,7 @@ def config_init( _check_config() -configuration = cast(_Config, None) # pylint: disable=invalid-name +configuration = cast(Config, None) # pylint: disable=invalid-name config_init() diff --git a/src/api/console_stringifier.py b/src/api/console_stringifier.py index 6c91289f..4b8c72eb 100644 --- a/src/api/console_stringifier.py +++ b/src/api/console_stringifier.py @@ -1,4 +1,4 @@ -from domain.result import _Result +from domain.result import Result from application.stringifier import Stringifier @@ -19,7 +19,7 @@ class ConsoleStringifier(Stringifier): unit_prefix = " " unit_suffix = "" - def result_to_str(self, result: _Result): + def result_to_str(self, result: Result): """ Returns the result as human-readable string. """ diff --git a/src/api/parsers.py b/src/api/parsers.py index d18c0203..6cc005c6 100644 --- a/src/api/parsers.py +++ b/src/api/parsers.py @@ -1,8 +1,8 @@ from typing import Union, List, Tuple -from application.helpers import _Helpers -from domain.value import _Value -from domain.uncertainty import _Uncertainty +from application.helpers import Helpers +from domain.value import Value +from domain.uncertainty import Uncertainty def check_if_number_string(value: str) -> None: @@ -58,15 +58,15 @@ def parse_name(name: str) -> str: # Turn digits into word(s): word = "" if num_of_digits <= 3: - word += _Helpers.number_to_word(int(name[:num_of_digits])) + word += Helpers.number_to_word(int(name[:num_of_digits])) else: - word += _Helpers.number_to_word(int(name[0])) + word += Helpers.number_to_word(int(name[0])) for i in range(1, num_of_digits): - word += _Helpers.capitalize((_Helpers.number_to_word(int(name[i])))) + word += Helpers.capitalize((Helpers.number_to_word(int(name[i])))) # Add word to parsed_name: if parsed_name != "": - word = _Helpers.capitalize(word) + word = Helpers.capitalize(word) parsed_name += word next_chat_upper = True @@ -130,7 +130,7 @@ def parse_decimal_places(decimal_places: Union[int, None]) -> Union[int, None]: return decimal_places -def parse_value(value: Union[float, int, str]) -> _Value: +def parse_value(value: Union[float, int, str]) -> Value: """Converts the value to a _Value object.""" if not isinstance(value, (float, int, str)): raise TypeError(f"`value` must be a float, int or string, not {type(value)}") @@ -142,10 +142,10 @@ def parse_value(value: Union[float, int, str]) -> _Value: if isinstance(value, int): value = float(value) - return _Value(value) + return Value(value) -def parse_exact_value(value: str) -> _Value: +def parse_exact_value(value: str) -> Value: # Determine min exponent: exponent_offset = 0 value_str = value @@ -158,7 +158,7 @@ def parse_exact_value(value: str) -> _Value: else: min_exponent = exponent_offset - return _Value(float(value), min_exponent) + return Value(float(value), min_exponent) def parse_uncertainties( @@ -169,7 +169,7 @@ def parse_uncertainties( Tuple[Union[float, int, str], str], List[Union[float, str, Tuple[Union[float, int, str], str]]], ] -) -> List[_Uncertainty]: +) -> List[Uncertainty]: """Converts the uncertainties to a list of _Uncertainty objects.""" uncertainties_res = [] @@ -181,7 +181,7 @@ def parse_uncertainties( for uncert in uncertainties: if isinstance(uncert, (float, int, str)): - uncertainties_res.append(_Uncertainty(_parse_uncertainty_value(uncert))) + uncertainties_res.append(Uncertainty(_parse_uncertainty_value(uncert))) elif isinstance(uncert, Tuple): if not isinstance(uncert[0], (float, int, str)): @@ -190,7 +190,7 @@ def parse_uncertainties( + f" int or a string, not {type(uncert[0])}" ) uncertainties_res.append( - _Uncertainty(_parse_uncertainty_value(uncert[0]), parse_name(uncert[1])) + Uncertainty(_parse_uncertainty_value(uncert[0]), parse_name(uncert[1])) ) else: @@ -201,7 +201,7 @@ def parse_uncertainties( return uncertainties_res -def _parse_uncertainty_value(value: Union[float, int, str]) -> _Value: +def _parse_uncertainty_value(value: Union[float, int, str]) -> Value: """Parses the value of an uncertainty.""" if isinstance(value, str): @@ -210,7 +210,7 @@ def _parse_uncertainty_value(value: Union[float, int, str]) -> _Value: else: if isinstance(value, int): value = float(value) - return_value = _Value(value) + return_value = Value(value) if return_value.get() <= 0: raise ValueError("Uncertainty must be positive.") diff --git a/src/api/printable_result.py b/src/api/printable_result.py index 3d7c46e5..fc44739d 100644 --- a/src/api/printable_result.py +++ b/src/api/printable_result.py @@ -1,11 +1,11 @@ from api.console_stringifier import ConsoleStringifier import api.config as c from application.latex_stringifier import LatexStringifier -from domain.result import _Result +from domain.result import Result class PrintableResult: - def __init__(self, result: _Result): + def __init__(self, result: Result): self._result = result def print(self): diff --git a/src/api/res.py b/src/api/res.py index e0593554..9cf95960 100644 --- a/src/api/res.py +++ b/src/api/res.py @@ -4,11 +4,11 @@ from api.printable_result import PrintableResult from api import parsers import api.config as c -from application.cache import _ResultsCache -from application.rounder import _Rounder -from domain.result import _Result +from application.cache import ResultsCache +from application.rounder import Rounder +from domain.result import Result -_res_cache = _ResultsCache() +_res_cache = ResultsCache() @overload @@ -99,10 +99,10 @@ def res( decimal_places_res = parsers.parse_decimal_places(decimal_places) # Assemble the result - result = _Result( + result = Result( name_res, value_res, unit_res, uncertainties_res, sigfigs_res, decimal_places_res ) - _Rounder.round_result(result, c.configuration.to_rounding_config()) + Rounder.round_result(result, c.configuration.to_rounding_config()) _res_cache.add(name, result) printable_result = PrintableResult(result) diff --git a/src/application/cache.py b/src/application/cache.py index a786a2fd..7d2ef9c0 100644 --- a/src/application/cache.py +++ b/src/application/cache.py @@ -1,7 +1,7 @@ -from domain.result import _Result +from domain.result import Result -class _ResultsCache: +class ResultsCache: """ A cache for all user-defined results. Results are hashed by their name. If the user tries to add a result with a name that already exists in the cache, @@ -10,10 +10,10 @@ class _ResultsCache: """ def __init__(self): - self.cache: dict[str, _Result] = {} + self.cache: dict[str, Result] = {} - def add(self, name, result: _Result): + def add(self, name, result: Result): self.cache[name] = result - def get_all_results(self) -> list[_Result]: + def get_all_results(self) -> list[Result]: return list(self.cache.values()) diff --git a/src/application/helpers.py b/src/application/helpers.py index eb3d9dfb..6c072fb4 100644 --- a/src/application/helpers.py +++ b/src/application/helpers.py @@ -32,7 +32,7 @@ } -class _Helpers: +class Helpers: @classmethod def get_exponent(cls, value: float) -> int: return math.floor(math.log10(abs(value))) diff --git a/src/application/latex_stringifier.py b/src/application/latex_stringifier.py index cf1755fe..5f549af6 100644 --- a/src/application/latex_stringifier.py +++ b/src/application/latex_stringifier.py @@ -1,5 +1,5 @@ -from domain.result import _Result -from application.helpers import _Helpers +from domain.result import Result +from application.helpers import Helpers from application.latex_ifelse import LatexIfElseBuilder from application.stringifier import Stringifier @@ -27,13 +27,13 @@ class LatexStringifier(Stringifier): unit_prefix = r"\, \unit{" unit_suffix = "}" - def result_to_latex_cmd(self, result: _Result) -> str: + def result_to_latex_cmd(self, result: Result) -> str: """ Returns the result as LaTeX command to be used in a .tex file. """ builder = LatexIfElseBuilder() - cmd_name = f"{self.config.identifier}{_Helpers.capitalize(result.name)}" + cmd_name = f"{self.config.identifier}{Helpers.capitalize(result.name)}" latex_str = rf"\newcommand*{{\{cmd_name}}}[1][]{{" + "\n" # Default case (full result) & value @@ -49,8 +49,8 @@ def result_to_latex_cmd(self, result: _Result) -> str: if len(result.uncertainties) == 1: uncertainty_name = "error" else: - uncertainty_name = u.name if u.name != "" else _Helpers.number_to_word(i + 1) - uncertainty_name = f"error{_Helpers.capitalize(uncertainty_name)}" + uncertainty_name = u.name if u.name != "" else Helpers.number_to_word(i + 1) + uncertainty_name = f"error{Helpers.capitalize(uncertainty_name)}" error_latex_str = self.create_str(u.uncertainty, [], result.unit) builder.add_branch(uncertainty_name, error_latex_str) @@ -86,19 +86,19 @@ def result_to_latex_cmd(self, result: _Result) -> str: return latex_str - def result_to_latex_str(self, 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 self.create_str(result.value, result.uncertainties, result.unit) - def result_to_latex_str_value(self, result: _Result) -> str: + 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.create_str(result.value, [], "") - def result_to_latex_str_without_error(self, result: _Result) -> str: + def result_to_latex_str_without_error(self, result: Result) -> str: """ Returns the result without error as LaTeX string making use of the siunitx package. """ diff --git a/src/application/rounder.py b/src/application/rounder.py index 99dd85d1..7c1847ae 100644 --- a/src/application/rounder.py +++ b/src/application/rounder.py @@ -1,9 +1,9 @@ from typing import List from dataclasses import dataclass -from domain.result import _Result -from domain.uncertainty import _Uncertainty -from application.helpers import _Helpers +from domain.result import Result +from domain.uncertainty import Uncertainty +from application.helpers import Helpers @dataclass @@ -14,10 +14,10 @@ class RoundingConfig: decimal_places_fallback: int -class _Rounder: +class Rounder: @classmethod - def round_result(cls, result: _Result, config: RoundingConfig) -> None: + def round_result(cls, result: Result, config: RoundingConfig) -> None: """ In-place rounds all numerical fields of a result to the correct number of significant figures. @@ -69,7 +69,7 @@ def round_result(cls, result: _Result, config: RoundingConfig) -> None: @classmethod # pylint: disable-next=too-many-branches - def _round_result(cls, result: _Result, config: RoundingConfig) -> None: + def _round_result(cls, result: Result, config: RoundingConfig) -> None: """See the docstring of the public `round_result` for details.""" value = result.value @@ -107,7 +107,7 @@ def _round_result(cls, result: _Result, config: RoundingConfig) -> None: continue normalized_value = abs(u.uncertainty.get()) * 10 ** ( - -_Helpers.get_exponent(u.uncertainty.get()) + -Helpers.get_exponent(u.uncertainty.get()) ) if round(normalized_value, 1) >= 3.0: @@ -132,7 +132,7 @@ def _round_result(cls, result: _Result, config: RoundingConfig) -> None: @classmethod def _uncertainties_set_min_exponents( - cls, uncertainties: List[_Uncertainty], min_exponent: int + cls, uncertainties: List[Uncertainty], min_exponent: int ) -> None: for u in uncertainties: if not u.uncertainty.is_exact(): diff --git a/src/application/stringifier.py b/src/application/stringifier.py index b44a2995..d6f7baa7 100644 --- a/src/application/stringifier.py +++ b/src/application/stringifier.py @@ -5,9 +5,9 @@ # 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 -from application.helpers import _Helpers +from domain.value import Value +from domain.uncertainty import Uncertainty +from application.helpers import Helpers @dataclass @@ -45,7 +45,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. @@ -68,7 +68,7 @@ def create_str(self, value: _Value, uncertainties: List[_Uncertainty], unit: str if should_use_parentheses: latex_str += self.left_parenthesis latex_str += sign - latex_str += _Helpers.round_to_n_decimal_places(value_normalized, decimal_places) + latex_str += Helpers.round_to_n_decimal_places(value_normalized, decimal_places) for u in uncertainties: uncertainty_normalized = u.uncertainty.get_abs() * factor @@ -78,7 +78,7 @@ def create_str(self, value: _Value, uncertainties: List[_Uncertainty], unit: str else u.uncertainty.get_decimal_place() ) latex_str += self.plus_minus - latex_str += _Helpers.round_to_n_decimal_places(uncertainty_normalized, decimal_places) + latex_str += Helpers.round_to_n_decimal_places(uncertainty_normalized, decimal_places) if len(uncertainties) > 1: latex_str += self.error_name_prefix + u.name + self.error_name_suffix @@ -94,7 +94,7 @@ def create_str(self, value: _Value, uncertainties: List[_Uncertainty], unit: str return latex_str def _should_use_scientific_notation( - self, value: _Value, uncertainties: List[_Uncertainty] + self, value: Value, uncertainties: List[Uncertainty] ) -> bool: """ Returns whether scientific notation should be used for the given value and uncertainties. diff --git a/src/domain/result.py b/src/domain/result.py index 7f0f1aba..01880ddf 100644 --- a/src/domain/result.py +++ b/src/domain/result.py @@ -2,25 +2,25 @@ from typing import Union from copy import copy -from domain.uncertainty import _Uncertainty -from domain.value import _Value +from domain.uncertainty import Uncertainty +from domain.value import Value @dataclass -class _Result: +class Result: """ A general-purpose result, i.e. a value that was somehow measured or calculated, along with a unit and optional uncertainties (list might be empty). """ name: str - value: _Value + value: Value unit: str - uncertainties: list[_Uncertainty] + uncertainties: list[Uncertainty] sigfigs: Union[int, None] decimal_places: Union[int, None] - total_uncertainty: Union[_Uncertainty, None] = field(init=False) + total_uncertainty: Union[Uncertainty, None] = field(init=False) def __post_init__(self): if len(self.uncertainties) >= 2: @@ -28,17 +28,17 @@ def __post_init__(self): else: self.total_uncertainty = None - def _calculate_total_uncertainty(self) -> _Uncertainty: + def _calculate_total_uncertainty(self) -> Uncertainty: total = 0 for u in self.uncertainties: total += u.uncertainty.get() ** 2 - return _Uncertainty(_Value(total**0.5)) + return Uncertainty(Value(total**0.5)) - def get_short_result(self) -> Union["_Result", None]: + def get_short_result(self) -> Union["Result", None]: if self.total_uncertainty is None: return None - return _Result( + return Result( self.name, copy(self.value), self.unit, diff --git a/src/domain/uncertainty.py b/src/domain/uncertainty.py index 792cc5ce..57bcbf7b 100644 --- a/src/domain/uncertainty.py +++ b/src/domain/uncertainty.py @@ -1,7 +1,7 @@ -from domain.value import _Value +from domain.value import Value -class _Uncertainty: +class Uncertainty: """ A named uncertainty value, e.g. a systematic uncertainty of ±0.1cm when measuring a length. In this case the uncertainty would be 0.1 and the name @@ -13,6 +13,6 @@ class _Uncertainty: interchangeably. """ - def __init__(self, uncertainty: _Value, name: str = ""): + def __init__(self, uncertainty: Value, name: str = ""): self.uncertainty = uncertainty self.name = name diff --git a/src/domain/value.py b/src/domain/value.py index 5dd6504a..79d2ba34 100644 --- a/src/domain/value.py +++ b/src/domain/value.py @@ -1,9 +1,9 @@ from typing import Union -from application.helpers import _Helpers +from application.helpers import Helpers -class _Value: +class Value: """ A floating-point value represented as string that is either treated as exact (does not have any uncertainties) or as inexact (has uncertainties). @@ -32,7 +32,7 @@ def __init__(self, value: float, min_exponent: Union[int, None] = None): else: self._is_exact = False - self._max_exponent = _Helpers.get_exponent(self._value) + self._max_exponent = Helpers.get_exponent(self._value) def set_min_exponent(self, min_exponent: int): self._min_exponent = min_exponent diff --git a/src/resultwizard/__init__.py b/src/resultwizard/__init__.py index baaff564..f00a02ca 100644 --- a/src/resultwizard/__init__.py +++ b/src/resultwizard/__init__.py @@ -1,4 +1,3 @@ from api.config import config_init, config from api.res import res from api.export import export -from application.cache import _ResultsCache diff --git a/tests/rounder_test.py b/tests/rounder_test.py index d07f8253..9658b929 100644 --- a/tests/rounder_test.py +++ b/tests/rounder_test.py @@ -2,10 +2,10 @@ import pytest -from application.rounder import _Rounder, RoundingConfig -from domain.result import _Result -from domain.value import _Value -from domain.uncertainty import _Uncertainty +from application.rounder import Rounder, RoundingConfig +from domain.result import Result +from domain.value import Value +from domain.uncertainty import Uncertainty @pytest.fixture @@ -15,84 +15,82 @@ def config_defaults(): class TestRounder: def test_hierarchy_1(self, config_defaults): - res = _Result("", _Value(1.0000, min_exponent=-4), "", [], 2, 10) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0000, min_exponent=-4), "", [], 2, 10) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -4 - res = _Result( - "", _Value(1.0000, min_exponent=-4), "", [_Uncertainty(_Value(0.1))], None, None - ) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0000, min_exponent=-4), "", [Uncertainty(Value(0.1))], None, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -4 assert res.uncertainties[0].uncertainty.get_min_exponent() == -4 - res = _Result( + res = Result( "", - _Value(1.0000, min_exponent=-4), + Value(1.0000, min_exponent=-4), "", - [_Uncertainty(_Value(0.1, min_exponent=-1))], + [Uncertainty(Value(0.1, min_exponent=-1))], None, None, ) - _Rounder.round_result(res, config_defaults) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -4 assert res.uncertainties[0].uncertainty.get_min_exponent() == -1 def test_hierarchy_3(self, config_defaults): - res = _Result("", _Value(1.0), "", [], 10, 2) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [], 10, 2) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -9 - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.1))], 10, None) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.1))], 10, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -9 assert res.uncertainties[0].uncertainty.get_min_exponent() == -9 - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.1, min_exponent=-1))], 10, None) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.1, min_exponent=-1))], 10, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -9 assert res.uncertainties[0].uncertainty.get_min_exponent() == -1 def test_hierarchy_4(self, config_defaults): - res = _Result("", _Value(1.0), "", [], None, 2) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [], None, 2) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -2 - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.1))], None, 2) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.1))], None, 2) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -2 assert res.uncertainties[0].uncertainty.get_min_exponent() == -2 - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.1e-5, min_exponent=-6))], None, 2) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.1e-5, min_exponent=-6))], None, 2) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -2 assert res.uncertainties[0].uncertainty.get_min_exponent() == -6 def test_hierarchy_5(self, config_defaults): - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.11))], None, None) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.11))], None, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -2 assert res.uncertainties[0].uncertainty.get_min_exponent() == -2 - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.294999))], None, None) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.294999))], None, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -2 assert res.uncertainties[0].uncertainty.get_min_exponent() == -2 - res = _Result("", _Value(1.0), "", [_Uncertainty(_Value(0.295001))], None, None) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [Uncertainty(Value(0.295001))], None, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -1 assert res.uncertainties[0].uncertainty.get_min_exponent() == -1 - res = _Result( - "", _Value(1.0), "", [_Uncertainty(_Value(0.4)), _Uncertainty(_Value(0.04))], None, None + res = Result( + "", Value(1.0), "", [Uncertainty(Value(0.4)), Uncertainty(Value(0.04))], None, None ) - _Rounder.round_result(res, config_defaults) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -2 assert res.uncertainties[0].uncertainty.get_min_exponent() == -1 assert res.uncertainties[1].uncertainty.get_min_exponent() == -2 def test_hierarchy_6(self, config_defaults): - res = _Result("", _Value(1.0), "", [], None, None) - _Rounder.round_result(res, config_defaults) + res = Result("", Value(1.0), "", [], None, None) + Rounder.round_result(res, config_defaults) assert res.value.get_min_exponent() == -1