Add lalib.config for library-wide settings

Also, refactor `lalib.elements.galois`
(incl. tests) to use the new settings
This commit is contained in:
Alexander Hess 2024-10-14 14:50:02 +02:00
parent febed693b8
commit cbc1f8fd3a
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
3 changed files with 16 additions and 10 deletions

5
src/lalib/config.py Normal file
View file

@ -0,0 +1,5 @@
"""Library-wide default settings."""
NDIGITS = 12
THRESHOLD = 1 / (10**NDIGITS)

View file

@ -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

View file

@ -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,