Skip to content

Commit

Permalink
Use configuration in LaTeXer
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Mar 14, 2024
1 parent 9a0d277 commit 07146a2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
12 changes: 11 additions & 1 deletion src/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/api/export.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from api.res import _res_cache
from api.config import configuration
from application.latexer import _LaTeXer


Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/api/printable_result.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
11 changes: 11 additions & 0 deletions src/application/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from application.latexer import LaTeXConfig
import json


Expand All @@ -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,
)
47 changes: 25 additions & 22 deletions src/application/latexer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from typing import List

import textwrap
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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}}}}}{{"
Expand All @@ -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
)

Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down

0 comments on commit 07146a2

Please sign in to comment.