diff --git a/src/lalib/config.py b/src/lalib/config.py new file mode 100644 index 0000000..5a20788 --- /dev/null +++ b/src/lalib/config.py @@ -0,0 +1,5 @@ +"""Library-wide default settings.""" + +NDIGITS = 12 + +THRESHOLD = 1 / (10**NDIGITS) diff --git a/src/lalib/elements/galois.py b/src/lalib/elements/galois.py index 03a9ea2..dd2bfad 100644 --- a/src/lalib/elements/galois.py +++ b/src/lalib/elements/galois.py @@ -43,14 +43,14 @@ except ImportError: # pragma: no cover to support Python 3.9 & 3.10 from typing_extensions import Self -THRESHOLD = 1e-12 +from lalib import config def _to_gf2( value: complex, # `mypy` reads `complex | float | int` *, strict: bool = True, - threshold: float = THRESHOLD, + threshold: float = config.THRESHOLD, ) -> int: """Cast a number as a possible Galois field value: `1` or `0`. @@ -132,7 +132,7 @@ class GF2Element(metaclass=GF2Meta): value: object = None, *, strict: bool = True, - threshold: float = THRESHOLD, + threshold: float = config.THRESHOLD, ) -> Self: """See docstring for `.__init__()`.""" if isinstance(value, cls): @@ -162,7 +162,7 @@ class GF2Element(metaclass=GF2Meta): value: object = None, *, strict: bool = True, - threshold: float = THRESHOLD, + threshold: float = config.THRESHOLD, ) -> None: """Obtain one of two objects: `one` or `zero`. @@ -242,7 +242,7 @@ class GF2Element(metaclass=GF2Meta): """Round `self` up to the next `int`: `math.ceil(self)`.""" return int(self) - def __round__(self, ndigits: Optional[int] = 0) -> int: + def __round__(self, _ndigits: int = config.NDIGITS) -> int: """Round `self` to the next `int`: `round(self)`.""" return int(self) @@ -556,3 +556,5 @@ gf2 = GF2Element del GF2Meta del GF2One del GF2Zero + +del config diff --git a/tests/elements/test_galois.py b/tests/elements/test_galois.py index 4920c82..1af29a9 100644 --- a/tests/elements/test_galois.py +++ b/tests/elements/test_galois.py @@ -11,6 +11,7 @@ import sys import pytest +from lalib import config from lalib.elements import galois @@ -26,17 +27,15 @@ GF2Element, GF2One, GF2Zero = ( # not part of the official API type(galois.zero), # are deleted in `lalib.elements.galois` ) -_THRESHOLD = galois.THRESHOLD - del galois CROSS_REFERENCE = not os.environ.get("NO_CROSS_REFERENCE") -default_threshold = _THRESHOLD -within_threshold = _THRESHOLD / 10 -not_within_threshold = _THRESHOLD * 10 +default_threshold = config.THRESHOLD +within_threshold = config.THRESHOLD / 10 +not_within_threshold = config.THRESHOLD * 10 strict_one_like_values = ( 1,