SciFile-Core 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scifile_core/__init__.py +111 -0
- scifile_core/_content.py +490 -0
- scifile_core/_diagnostic.py +322 -0
- scifile_core/_exception.py +221 -0
- scifile_core/_io.py +705 -0
- scifile_core/_mapping.py +139 -0
- scifile_core/_model.py +1056 -0
- scifile_core/_number.py +41 -0
- scifile_core/_optional.py +40 -0
- scifile_core/py.typed +0 -0
- scifile_core/testing.py +653 -0
- scifile_core/wording.py +454 -0
- scifile_core-0.0.1.dist-info/METADATA +6 -0
- scifile_core-0.0.1.dist-info/RECORD +16 -0
- scifile_core-0.0.1.dist-info/WHEEL +5 -0
- scifile_core-0.0.1.dist-info/top_level.txt +1 -0
scifile_core/_number.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Writing numbers in the text formats."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import decimal
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def positional(value: float, *, point: bool = False) -> str:
|
|
9
|
+
"""Return a number without an exponent, in the fewest digits that read back.
|
|
10
|
+
|
|
11
|
+
The digits are those that `repr` writes of the number as a float, the
|
|
12
|
+
fewest that read back as it. A number that Python writes with an
|
|
13
|
+
exponent, of a size below 1e-4 or from 1e16 on, is written in full
|
|
14
|
+
instead, with zeros, since the readers of some formats read no exponent.
|
|
15
|
+
The text is that of `numpy.format_float_positional` of the float, with
|
|
16
|
+
``unique=True``, and with ``trim="0"`` if ``point`` is true or
|
|
17
|
+
``trim="-"`` if not, without NumPy.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
value
|
|
22
|
+
The number, which is written as a float: a 32-bit float, for
|
|
23
|
+
example, with the digits of its value as a 64-bit float, as
|
|
24
|
+
``0.10000000149011612`` for ``float32(0.1)``.
|
|
25
|
+
point
|
|
26
|
+
Whether a whole number keeps its point and a zero, as in ``40.0``,
|
|
27
|
+
as Python writes it, rather than ``40``.
|
|
28
|
+
|
|
29
|
+
Returns
|
|
30
|
+
-------
|
|
31
|
+
The number, or ``nan``, ``inf`` or ``-inf`` for a number that is not
|
|
32
|
+
finite, as `repr` writes it.
|
|
33
|
+
"""
|
|
34
|
+
text = repr(float(value))
|
|
35
|
+
if "e" in text:
|
|
36
|
+
# The digits shift by the exponent, without rounding.
|
|
37
|
+
text = format(decimal.Decimal(text), "f")
|
|
38
|
+
return f"{text}.0" if point and "." not in text else text
|
|
39
|
+
if not point and text.endswith(".0"):
|
|
40
|
+
return text[:-2]
|
|
41
|
+
return text
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""NumPy, which SciFile-Core neither requires nor imports to check a value."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from types import ModuleType
|
|
10
|
+
|
|
11
|
+
_IMPORTED: list[ModuleType] = []
|
|
12
|
+
"""NumPy, once an import of it has ended, so that it is not looked up again."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def imported_numpy() -> ModuleType | None:
|
|
16
|
+
"""Return NumPy if the process has imported it, or `None`.
|
|
17
|
+
|
|
18
|
+
No value is a NumPy value unless NumPy is imported, so that a check of a
|
|
19
|
+
value needs NumPy only once it is in ``sys.modules``. Python puts NumPy
|
|
20
|
+
there before its import ends, so that another thread may find it half
|
|
21
|
+
built: the import statement waits for such an import to end, and returns
|
|
22
|
+
NumPy whole. An import that fails leaves no NumPy. The waiting statement
|
|
23
|
+
then returns a half-built NumPy that is no longer in ``sys.modules``, as
|
|
24
|
+
Python 3.12 does, or imports NumPy again, as Python 3.14 does, which may
|
|
25
|
+
fail with any error. A module of ``None`` in ``sys.modules`` blocks NumPy,
|
|
26
|
+
as if it were not installed.
|
|
27
|
+
"""
|
|
28
|
+
if _IMPORTED:
|
|
29
|
+
return _IMPORTED[0]
|
|
30
|
+
if sys.modules.get("numpy") is None:
|
|
31
|
+
return None
|
|
32
|
+
try:
|
|
33
|
+
import numpy as np # noqa: PLC0415 - waits for an import in another thread
|
|
34
|
+
except Exception: # noqa: BLE001 - a NumPy that fails to import made no value
|
|
35
|
+
return None
|
|
36
|
+
if sys.modules.get("numpy") is not np:
|
|
37
|
+
return None
|
|
38
|
+
if not _IMPORTED:
|
|
39
|
+
_IMPORTED.append(np)
|
|
40
|
+
return np
|
scifile_core/py.typed
ADDED
|
File without changes
|