Skip to content

Commit

Permalink
Add first draft for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Mar 14, 2024
1 parent a364a42 commit b83316f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/api/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Union

from application.config import Config


def config_init(
sigfigs: int = Config.sigfigs_default,
decimal_places: int = Config.decimal_places_default,
print_always: bool = Config.print_always_default,
):
c = Config(sigfigs, decimal_places, print_always)
print(c.to_json_str())
return c


configuration = config_init()


def config(
sigfigs: Union[int, None] = None,
decimal_places: Union[int, None] = None,
print_always: Union[bool, None] = None,
):
if sigfigs is not None:
configuration.sigfigs = sigfigs
if decimal_places is not None:
configuration.decimal_places = decimal_places
if print_always is not None:
configuration.print_always = print_always

print(configuration.to_json_str())
22 changes: 22 additions & 0 deletions src/application/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json


class Config:
"""Configuration settings for the application."""

sigfigs_default = 2
decimal_places_default = 2
print_always_default = False

def __init__(
self,
sigfigs=sigfigs_default,
decimal_places=decimal_places_default,
print_always=print_always_default,
):
self.sigfigs = sigfigs
self.decimal_places = decimal_places
self.print_always = print_always

def to_json_str(self):
return json.dumps(self.__dict__)
1 change: 1 addition & 0 deletions src/resultwizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from api.config import config_init, config
from api.res import res
from api.export import export
from application.cache import _ResultsCache
13 changes: 9 additions & 4 deletions tests/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
print()


# wiz.config(
# standard_sigfigs = 2,
# ...
# )
wiz.config_init(
sigfigs=3,
decimal_places=3,
print_always=True,
)


#############################
Expand All @@ -30,6 +31,8 @@
wiz.res("a1", 1.0, r"\mm").print()
# a: 1.0 \mm

wiz.config(sigfigs=2)

wiz.res("1 b", 1.0, 0.01, r"\per\mm\cubed").print()
# b: (1.0 ± 0.01) \mm

Expand All @@ -46,6 +49,8 @@
wiz.res("e", "1.0", r"\mm").print()
# e: 1.0 \mm

wiz.config(decimal_places=4)

# wiz.standard_sigfigs(3)

wiz.res("f", "1.0e1", 25e-1).print()
Expand Down

0 comments on commit b83316f

Please sign in to comment.