Skip to content

Commit

Permalink
Move the parsing of exact values from domain to api
Browse files Browse the repository at this point in the history
  • Loading branch information
paul019 committed Mar 15, 2024
1 parent bc3c7ad commit 9b0f11f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
39 changes: 31 additions & 8 deletions src/api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,27 @@ def parse_value(value: Union[float, int, str]) -> _Value:

if isinstance(value, str):
check_if_number_string(value)
elif isinstance(value, int):
value = float(value)
return parse_exact_value(value)
else:
if isinstance(value, int):
value = float(value)
return _Value(value)

return _Value(value)

def parse_exact_value(value: str) -> _Value:
# Determine min exponent:
exponent_offset = 0
value_str = value
if "e" in value_str:
exponent_offset = int(value_str[value_str.index("e") + 1 :])
value_str = value_str[0 : value_str.index("e")]
if "." in value:
decimal_places = len(value_str) - value_str.index(".") - 1
min_exponent = -decimal_places + exponent_offset
else:
min_exponent = exponent_offset

return _Value(float(value), min_exponent)


def parse_uncertainties(
Expand Down Expand Up @@ -159,12 +176,18 @@ def parse_uncertainties(
return uncertainties_res


def _parse_uncertainty_value(value: Union[float, int, str]) -> Union[float, str]:
def _parse_uncertainty_value(value: Union[float, int, str]) -> _Value:
"""Parses the value of an uncertainty."""

if isinstance(value, str):
check_if_number_string(value)
elif isinstance(value, int):
value = float(value)
if float(value) <= 0:
return_value = parse_exact_value(value)
else:
if isinstance(value, int):
value = float(value)
return_value = _Value(value)

if return_value.get() <= 0:
raise ValueError("Uncertainty must be positive.")
return value

return return_value
2 changes: 1 addition & 1 deletion src/domain/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _calculate_total_uncertainty(self) -> _Uncertainty:
total = 0
for u in self.uncertainties:
total += u.uncertainty.get() ** 2
return _Uncertainty(total**0.5)
return _Uncertainty(_Value(total**0.5))

def get_short_result(self) -> Union["_Result", None]:
if self.total_uncertainty is None:
Expand Down
4 changes: 2 additions & 2 deletions src/domain/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class _Uncertainty:
interchangeably.
"""

def __init__(self, uncertainty: Union[float, str], name: str = ""):
self.uncertainty = _Value(uncertainty)
def __init__(self, uncertainty: _Value, name: str = ""):
self.uncertainty = uncertainty
self.name = name

def value(self) -> _Value:
Expand Down
21 changes: 5 additions & 16 deletions src/domain/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,13 @@ class _Value:
# "3400" -> 3400, 0, 3
# "3.4e3" -> 3400, 2, 3

def __init__(self, value: Union[float, str]):
if isinstance(value, str):
self._value = float(value)
self._is_exact = True
def __init__(self, value: float, min_exponent: Union[int, None] = None):
self._value = value

# Determine min exponent:
value_str = value
exponent_offset = 0
if "e" in value_str:
exponent_offset = int(value_str[value_str.index("e") + 1 :])
value_str = value_str[0 : value_str.index("e")]
if "." in value_str:
decimal_places = len(value_str) - value_str.index(".") - 1
self._min_exponent = -decimal_places + exponent_offset
else:
self._min_exponent = exponent_offset
if min_exponent is not None:
self._min_exponent = min_exponent
self._is_exact = True
else:
self._value = value
self._is_exact = False

self._max_exponent = _Helpers.get_exponent(self._value)
Expand Down

0 comments on commit 9b0f11f

Please sign in to comment.