Add lalib.config
for library-wide settings
Also, refactor `lalib.elements.galois` (incl. tests) to use the new settings
This commit is contained in:
parent
febed693b8
commit
cbc1f8fd3a
3 changed files with 16 additions and 10 deletions
5
src/lalib/config.py
Normal file
5
src/lalib/config.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
"""Library-wide default settings."""
|
||||||
|
|
||||||
|
NDIGITS = 12
|
||||||
|
|
||||||
|
THRESHOLD = 1 / (10**NDIGITS)
|
|
@ -43,14 +43,14 @@ except ImportError: # pragma: no cover to support Python 3.9 & 3.10
|
||||||
from typing_extensions import Self
|
from typing_extensions import Self
|
||||||
|
|
||||||
|
|
||||||
THRESHOLD = 1e-12
|
from lalib import config
|
||||||
|
|
||||||
|
|
||||||
def _to_gf2(
|
def _to_gf2(
|
||||||
value: complex, # `mypy` reads `complex | float | int`
|
value: complex, # `mypy` reads `complex | float | int`
|
||||||
*,
|
*,
|
||||||
strict: bool = True,
|
strict: bool = True,
|
||||||
threshold: float = THRESHOLD,
|
threshold: float = config.THRESHOLD,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Cast a number as a possible Galois field value: `1` or `0`.
|
"""Cast a number as a possible Galois field value: `1` or `0`.
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ class GF2Element(metaclass=GF2Meta):
|
||||||
value: object = None,
|
value: object = None,
|
||||||
*,
|
*,
|
||||||
strict: bool = True,
|
strict: bool = True,
|
||||||
threshold: float = THRESHOLD,
|
threshold: float = config.THRESHOLD,
|
||||||
) -> Self:
|
) -> Self:
|
||||||
"""See docstring for `.__init__()`."""
|
"""See docstring for `.__init__()`."""
|
||||||
if isinstance(value, cls):
|
if isinstance(value, cls):
|
||||||
|
@ -162,7 +162,7 @@ class GF2Element(metaclass=GF2Meta):
|
||||||
value: object = None,
|
value: object = None,
|
||||||
*,
|
*,
|
||||||
strict: bool = True,
|
strict: bool = True,
|
||||||
threshold: float = THRESHOLD,
|
threshold: float = config.THRESHOLD,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Obtain one of two objects: `one` or `zero`.
|
"""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)`."""
|
"""Round `self` up to the next `int`: `math.ceil(self)`."""
|
||||||
return int(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)`."""
|
"""Round `self` to the next `int`: `round(self)`."""
|
||||||
return int(self)
|
return int(self)
|
||||||
|
|
||||||
|
@ -556,3 +556,5 @@ gf2 = GF2Element
|
||||||
del GF2Meta
|
del GF2Meta
|
||||||
del GF2One
|
del GF2One
|
||||||
del GF2Zero
|
del GF2Zero
|
||||||
|
|
||||||
|
del config
|
||||||
|
|
|
@ -11,6 +11,7 @@ import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from lalib import config
|
||||||
from lalib.elements import galois
|
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`
|
type(galois.zero), # are deleted in `lalib.elements.galois`
|
||||||
)
|
)
|
||||||
|
|
||||||
_THRESHOLD = galois.THRESHOLD
|
|
||||||
|
|
||||||
del galois
|
del galois
|
||||||
|
|
||||||
|
|
||||||
CROSS_REFERENCE = not os.environ.get("NO_CROSS_REFERENCE")
|
CROSS_REFERENCE = not os.environ.get("NO_CROSS_REFERENCE")
|
||||||
|
|
||||||
|
|
||||||
default_threshold = _THRESHOLD
|
default_threshold = config.THRESHOLD
|
||||||
within_threshold = _THRESHOLD / 10
|
within_threshold = config.THRESHOLD / 10
|
||||||
not_within_threshold = _THRESHOLD * 10
|
not_within_threshold = config.THRESHOLD * 10
|
||||||
|
|
||||||
strict_one_like_values = (
|
strict_one_like_values = (
|
||||||
1,
|
1,
|
||||||
|
|
Loading…
Reference in a new issue