Skip to content

Commit

Permalink
update to precision; new default is None so no round is performed.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanwal committed Mar 2, 2025
1 parent 5100096 commit d2162c1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = unitpy
version = 0.0.18
version = 0.0.19
description = Working with scientific units in python.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
1 change: 1 addition & 0 deletions src/unitpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pkg_resources

from unitpy.config import CONFIG
import unitpy.errors as errors
from unitpy.definitions.ledger import ledger
from unitpy.core import Unit, Quantity
Expand Down
6 changes: 4 additions & 2 deletions src/unitpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class StringFormat(enum.Enum):
# latex = 2
# html = 3

def __init__(self):
def __init__(self, precision: int | None = None):
""""""
self.precision = precision
self.format_symbols = Config.StringFormat.symbols
self.integer_format_numerator_parenthesis = False
self.integer_format_denominator_parenthesis = True
Expand All @@ -76,4 +78,4 @@ def get_config_from_env(self):
setattr(self, k, v)


config = Config()
CONFIG = Config()
12 changes: 6 additions & 6 deletions src/unitpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
from datetime import timedelta

from unitpy.errors import UnitDimensionError
from unitpy.config import config
from unitpy.config import CONFIG
from unitpy.definitions.dimensions import Dimension
from unitpy.definitions.unit_base import BaseSet
from unitpy.definitions.entry import Entry
from unitpy.definitions.ledger import ledger
from unitpy.utils.equation_formating import equation_formater

_precision = 10


def get_base_unit(unit: dict[Entry, int | float]) -> BaseSet:
base = BaseSet()
Expand Down Expand Up @@ -89,7 +87,7 @@ def __init__(self, unit: str | dict[Entry, int | float] | BaseSet = None):
self._offset: int | float | None = None

def __str__(self):
if config.abbr:
if CONFIG.abbr:
return self.abbr
return self.label

Expand Down Expand Up @@ -552,7 +550,8 @@ def v(self) -> int | float:
def value(self) -> int | float:
value = self._value
if isinstance(value, float):
value = round(value, _precision)
if CONFIG.precision is not None:
value = round(value, CONFIG.precision)
if value.is_integer():
value = int(value)

Expand All @@ -570,7 +569,8 @@ def unit(self) -> Unit:
def base_value(self) -> int | float:
value = self._base_value
if isinstance(value, float):
value = round(value, _precision)
if CONFIG.precision is not None:
value = round(value, CONFIG.precision)
if value.is_integer():
value = int(value)

Expand Down
22 changes: 11 additions & 11 deletions src/unitpy/utils/equation_formating.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from __future__ import annotations

from unitpy.config import config
from unitpy.config import CONFIG


def equation_formater(dict_: dict[str, int | float]) -> str:
if config.format_symbols == config.StringFormat.symbols:
if CONFIG.format_symbols == CONFIG.StringFormat.symbols:
string = format_with_symbols(dict_)
elif config.format_symbols == config.StringFormat.power:
elif CONFIG.format_symbols == CONFIG.StringFormat.power:
string = format_with_power(dict_)
else:
raise ValueError("Invalid value for 'config.format_symbols'.")
raise ValueError("Invalid value for 'CONFIG.format_symbols'.")

return string


def format_with_power(dict_: dict[str, int | float]) -> str:
return config.multiplication_seperator.join([f"{k}**{v}" for k, v in dict_.items() if v != 0])
return CONFIG.multiplication_seperator.join([f"{k}**{v}" for k, v in dict_.items() if v != 0])


def format_with_symbols(dict_: dict) -> str:
Expand All @@ -33,15 +33,15 @@ def format_with_symbols(dict_: dict) -> str:
return format_with_power(dict_)

if denominator:
string += config.division_seperator + format_integer_denominator(denominator)
string += CONFIG.division_seperator + format_integer_denominator(denominator)

return string


def format_integer_numerator(numerator) -> str:
string = config.multiplication_seperator.join(format_term(k, power) for k, power in numerator if power != 0)
string = CONFIG.multiplication_seperator.join(format_term(k, power) for k, power in numerator if power != 0)

if config.integer_format_numerator_parenthesis and len(numerator) > 1:
if CONFIG.integer_format_numerator_parenthesis and len(numerator) > 1:
string = "(" + string + ")"

if string == "":
Expand All @@ -51,10 +51,10 @@ def format_integer_numerator(numerator) -> str:


def format_integer_denominator(denominator) -> str:
string = config.multiplication_seperator.join(format_term(k, -1 * power) for k, power in denominator if
power != 0)
string = CONFIG.multiplication_seperator.join(format_term(k, -1 * power) for k, power in denominator if
power != 0)

if config.integer_format_denominator_parenthesis and len(denominator) > 1:
if CONFIG.integer_format_denominator_parenthesis and len(denominator) > 1:
string = "(" + string + ")"

return string
Expand Down
18 changes: 17 additions & 1 deletion tests/test_define_quantity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import pytest

from unitpy import U, Q
from unitpy import U, Q, CONFIG


def test_quantity_unit():
Expand Down Expand Up @@ -60,3 +60,19 @@ def test_format_str4():
q = Q("100 km/h")
string = f"{q:4}"
assert string == " 100 kilometer/hour"


def test_precision():
wl0 = Q(630, "nm")
dwl = Q(20, "pm")
wl1 = wl0 + dwl
assert wl1 != wl0


def test_precision2():
CONFIG.precision = 9
wl0 = Q(630, "nm")
dwl = Q(20, "pm")
wl1 = wl0 + dwl
assert wl1 == wl0
CONFIG.precision = None
2 changes: 1 addition & 1 deletion tests/test_quanitiy_math_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_compare_e():


def test_compare_e2():
assert (Q("1 ft") == Q("12 in")) is True
assert Q("1 ft").is_close(Q("12 in")) is True


def test_compare_error_gt():
Expand Down

0 comments on commit d2162c1

Please sign in to comment.