Skip to content

Commit

Permalink
Implement export_auto_to config option
Browse files Browse the repository at this point in the history
If not empty, each `res()` call will automatically
export all results to the file the user specifies with this keyword.

They can still use the `export()` method to export
results to other files as well.

This option might be particularly useful in Jupyter notebooks.
  • Loading branch information
Splines committed Mar 18, 2024
1 parent 843abea commit c61d5af
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Config:
sigfigs (int): The number of significant figures to round to.
decimal_places (int): The number of decimal places to round to.
print_auto (bool): Whether to print each result directly to the console.
export_auto_to (str): If not empty, each `res()` call will automatically
export all results to the file you specify with this keyword. You can
still use the `export()` method to export results to other files as well.
This option might be particularly useful when working in a Jupyter
notebook. This way, you don't have to call `export()` manually each time
you want to export results.
min_exponent_for_non_scientific_notation (int): The minimum exponent
for non-scientific notation.
max_exponent_for_non_scientific_notation (int): The maximum exponent
Expand All @@ -32,6 +38,7 @@ class Config:
sigfigs: int
decimal_places: int
print_auto: bool
export_auto_to: str
min_exponent_for_non_scientific_notation: int
max_exponent_for_non_scientific_notation: int
identifier: str
Expand Down Expand Up @@ -86,6 +93,7 @@ def config_init(
sigfigs: int = -1, # -1: "per default use rounding rules instead"
decimal_places: int = -1, # -1: "per default use rounding rules instead"
print_auto: bool = False,
export_auto_to: str = "",
min_exponent_for_non_scientific_notation: int = -2,
max_exponent_for_non_scientific_notation: int = 3,
identifier: str = "result",
Expand All @@ -99,6 +107,7 @@ def config_init(
sigfigs,
decimal_places,
print_auto,
export_auto_to,
min_exponent_for_non_scientific_notation,
max_exponent_for_non_scientific_notation,
identifier,
Expand Down
5 changes: 5 additions & 0 deletions src/api/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def export(filepath: str):
Rounds all results according to the significant figures and writes them
to a .tex file at the given filepath.
"""
return _export(filepath, print_completed=True)


def _export(filepath: str, print_completed: bool):
results = _res_cache.get_all_results()
print(f"Processing {len(results)} result(s)")

Expand Down Expand Up @@ -48,6 +52,7 @@ def export(filepath: str):
# Write to file
with open(filepath, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
if print_completed:
print(f'Exported to "{filepath}"')


Expand Down
9 changes: 9 additions & 0 deletions src/api/res.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

_res_cache = ResultsCache()

# "Wrong" import position to avoid circular imports (export needs the _res_cache)
from api.export import _export # pylint: disable=wrong-import-position,ungrouped-imports


@overload
def res(
Expand Down Expand Up @@ -113,10 +116,16 @@ def res(
Rounder.round_result(result, c.configuration.to_rounding_config())
_res_cache.add(name_res, result)

# Print automatically
printable_result = PrintableResult(result)
if c.configuration.print_auto:
printable_result.print()

# Export automatically
immediate_export_path = c.configuration.export_auto_to
if immediate_export_path != "":
_export(immediate_export_path, print_completed=False)

return printable_result


Expand Down
8 changes: 6 additions & 2 deletions tests/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
print("#############################")
print()


wiz.config_init(print_auto=True, decimal_places=3, siunitx_fallback=False)
wiz.config_init(
print_auto=True,
export_auto_to="results-immediate.tex",
decimal_places=3,
siunitx_fallback=False,
)
# wiz.config(sigfigs=2)
# wiz.config(decimal_places=2)

Expand Down

0 comments on commit c61d5af

Please sign in to comment.