Organize top-level imports for lalib

- make `GF2`, `one`, and `zero`, defined in the `lalib.elements.gf2`
  module, available as top-level imports for the `lalib` package
  via `from lalib import *`
- provide some code snippets in the package's docstring
- test the star import
This commit is contained in:
Alexander Hess 2024-09-18 18:37:54 +02:00
parent 3cfc0db136
commit 62b25f66d9
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
2 changed files with 45 additions and 0 deletions

View file

@ -4,10 +4,26 @@ First, verify that your installation of `lalib` works:
>>> import lalib >>> import lalib
>>> lalib.__version__ != '0.0.0' >>> lalib.__version__ != '0.0.0'
True True
`lalib` exposes its very own "words" (i.e., its public API) at the root
of the package. They can be imported all at once with:
>>> from lalib import *
In addition to Python's built-in numbers, `lalib` comes with a couple of
specific numeric data types, for example, `one` and `zero` representing
the two elements of the Galois field `GF2`:
>>> one + one
zero
>>> one + zero
one
""" """
from importlib import metadata from importlib import metadata
from lalib.elements import gf2
try: try:
pkg_info = metadata.metadata(__name__) pkg_info = metadata.metadata(__name__)
@ -24,4 +40,15 @@ else:
del pkg_info del pkg_info
GF2, one, zero = gf2.GF2, gf2.one, gf2.zero
del gf2
del metadata del metadata
__all__ = (
"GF2",
"one",
"zero",
)

View file

@ -0,0 +1,18 @@
"""Test top-level imports for `lalib`."""
import lalib as top_level
def test_top_level_imports():
"""Verify `from lalib import *` works."""
environment = {}
exec("...", environment, environment) # noqa: S102
defined_vars_before = set(environment)
exec("from lalib import *", environment, environment) # noqa: S102
defined_vars_after = set(environment)
new_vars = defined_vars_after - defined_vars_before
assert new_vars == set(top_level.__all__)