Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove base-2 support #202

Merged
merged 10 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ This project adheres to `Semantic Versioning <https://semver.org/>`_.
Unreleased
----------

* Any unreleased changes can be viewed in the latest version
documentation
`changelog <https://sciform.readthedocs.io/en/latest/project.html#changelog>`_.
..
* Any unreleased changes can be viewed in the latest version
documentation
`changelog <https://sciform.readthedocs.io/en/latest/project.html#changelog>`_.

Removed
^^^^^^^

* Removed all support for base-2 formatting.
The ``sciform`` package will focus on base-2 scientific formatting,
more relevent for the physical sciences, unless there is demand for
base-2 or base-``n`` formatting.
We encourage users to consider the `prefixed <https://pypi.org/project/prefixed/>`_
package if they have requirements for base-2 formatting.
[`#201 <https://github.com/jagerber48/sciform/issues/201>`_]

----

0.38.2 (2024-11-02)
-------------------

Fixed
-----
^^^^^

* Corrected a typo in a previous release.

Expand All @@ -28,7 +40,7 @@ Fixed
-------------------

Added
-----
^^^^^

* Documented examples demonstrating the interplay between ``sciform``
formatting the ``decimal`` module configuration.
Expand Down
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ Overview

``sciform`` is used to convert python numbers into strings according to
a variety of user-selected scientific formatting options including
decimal, binary, fixed-point, scientific and engineering formats.
fixed-point, scientific and engineering formats.
Where possible, formatting follows documented standards such as those
published by `BIPM <https://www.bipm.org/en/>`_ or
`IEC <https://iec.ch/homepage>`_.
published by `BIPM <https://www.bipm.org/en/>`_.
``sciform`` provides certain options, such as engineering notation,
well-controlled significant figure rounding, and separator customization
which are not provided by the python built-in
Expand Down
40 changes: 1 addition & 39 deletions docs/source/exp replacement.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ Supported Exponent Replacements
:mod:`sciform` offers exponent replacement modes which provide
translations between exponent strings and conventional alphabetic
symbol representations.
For decimal exponents in engineering formats, SI prefix translations are
For exponents in engineering formats, SI prefix translations are
available according to the
`SI prefixes <https://www.nist.gov/pml/owm/metric-si-prefixes>`_.
It is also possible to convert decimal exponents into
`parts-per notation <https://en.wikipedia.org/wiki/Parts-per_notation>`_.
For binary formats, the IEC prefix translations are available matched to
integer multiples of 10 according to the
`IEC prefixes <https://physics.nist.gov/cuu/Units/binary.html>`_.

SI Prefixes
-----------
Expand Down Expand Up @@ -161,38 +158,3 @@ override the standard mappings listed above.
33e-09
>>> print(formatter(33e-12))
33 ppb

IEC Prefixes
------------

.. list-table:: IEC Prefixes
:widths: 15, 15, 15
:header-rows: 1

* - Exponent Value
- Prefix Name
- Prefix
* - 2\ :sup:`+80`
- yobi
- Yi
* - 2\ :sup:`+70`
- zebi
- Zi
* - 2\ :sup:`+60`
- exi
- Ei
* - 2\ :sup:`+50`
- pebi
- Pi
* - 2\ :sup:`+40`
- tebi
- Ti
* - 2\ :sup:`+30`
- gibi
- Gi
* - 2\ :sup:`+20`
- mebi
- Mi
* - 2\ :sup:`+10`
- kibi
- Ki
11 changes: 4 additions & 7 deletions docs/source/fsml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The :mod:`sciform` format specification mini-language is given by::
round_mode ::= "!" | "."
special_round_mode ::= "A" | "P"
ndigits ::= [+-]?digit+
exp_mode ::= "f" | "F" | "%" | "e" | "E" | "r" | "R" | "b" | "B" |
exp_mode ::= "f" | "F" | "%" | "e" | "E" | "r" | "R" |
exp_val ::= [+-]?digit+

Below are two simple FSML usage examples.
Expand Down Expand Up @@ -74,8 +74,7 @@ Further details about the options can be found at
| (``'#'``)
- The alternate mode flag indicates to use
:ref:`engineering_shifted` mode when the exponent mode flag is
``'r'`` or ``'R'`` or to use :ref:`binary_iec` mode when the
exponent mode flag is ``'b'`` or ``'B'``.
``'r'`` or ``'R'``.
* - | left_pad_dec_place
| (``\d+``)
- Sets ``left_pad_dec_place`` to any non-negative integer.
Expand All @@ -93,8 +92,8 @@ Further details about the options can be found at
- Set ``round_mode`` to ``'all'`` or ``'pdg'``.
See :ref:`rounding`.
* - | exp_mode
| (``'f'``, ``'F'``, ``'%'``, ``'e'``, ``'E'``, ``'r'``, ``'R'``,
``'b'``, ``'B'``)
| (``'f'``, ``'F'``, ``'%'``, ``'e'``, ``'E'``, ``'r'``,
``'R'``)
- Sets ``exponent_mode``.
If this flag is capitalized then ``capitalize`` is set to
``True``.
Expand All @@ -106,8 +105,6 @@ Further details about the options can be found at
* ``'r'`` and ``'R'`` set :ref:`engineering` or
:ref:`engineering_shifted` exponent modes depending on if the
alternate mode flag is used..
* ``'b'`` and ``'B'`` set :ref:`binary` or :ref:`binary_iec`
exponent modes depending on if the alternate mode flag is used.
* - | exp_val
| (``x[+-]\d+``)
- Sets ``exp_val`` to an integer.
Expand Down
77 changes: 16 additions & 61 deletions docs/source/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Exponent Mode
To display numbers across a wide range of magnitudes, scientific
formatting presents numbers in the form::

num = mantissa * base**exp
num = mantissa * 10**exp

Where exp is an integer and ``base`` is 10 or 2.
The different exponent modes control how ``mantissa``, ``base`` and
``exp`` are chosen for a given input number ``num``.
Where exp is an integer.
The different exponent modes control how ``mantissa`` and ``exp`` are
chosen for a given input number ``num``.

.. _fixed_point:

Expand Down Expand Up @@ -62,7 +62,7 @@ appended to the end of the formatted string.
Scientific Notation
-------------------

Scientific notation is used to display base-10 decimal numbers.
Scientific notation is used to display numbers.
In scientific notation, the exponent is uniquely chosen so that the
mantissa ``m`` satisfies ``1 <= m < 10``.

Expand Down Expand Up @@ -120,36 +120,6 @@ the exponent is chosen so that the mantissa ``m`` satisfies
>>> print(formatter(123.456, 0.001))
(0.123456 ± 0.000001)e+03

.. _binary:

Binary
------

Binary formatting can be chosen to display a number in scientific
notation in base-2.

>>> formatter = Formatter(exp_mode="binary")
>>> print(formatter(256))
1b+08

Here ``b`` exponent symbol indicates base-2 instead of base-10.
For binary formatting, the mantissa ``m`` satisfies ``1 <= m < 2``.

.. _binary_iec:

Binary IEC
----------

Binary IEC mode is similar to engineering notation, except in base-2.
In this mode number are expressed in base-2 exponent notation, but the
exponent is constrained to be a multiple of 10, consistent with the
IEC binary prefixes.
The mantissa ``m`` satisfies ``1 <= m < 1024``.

>>> formatter = Formatter(exp_mode="binary_iec")
>>> print(formatter(2048))
2b+10

.. _fixed_exp:

Fixed Exponent
Expand All @@ -171,23 +141,20 @@ For fixed point and percent modes an explicit fixed exponent must equal
0.
For engineering and shifted engineering modes an explicit fixed exponent
must be an integer multiple of 3.
For binary IEC mode an explicit fixed exponent must be an integer
multiple of 10.
Because of this constrained behavior, it is recommended to only use a
fixed exponent with the scientific or binary exponent modes.
fixed exponent with the scientific exponent mode.

.. _exp_str_replacement:

Exponent String Replacement
===========================

:mod:`sciform` provides a number of formatting options for replacing
decimal and binary exponent strings such as ``'e-03'`` or ``'b+10'``
with conventional strings such as ``'m'`` or ``'Ki'`` to succinctly
communicate the order of magnitude.
Decimal exponent strings can be replaced with either SI prefixes or
parts-per identifiers and binary exponent strings can be replaced with
IEC prefixes.
exponent strings such as ``'e-03'`` with conventional strings such as
``'m'`` (the SI prefix for milli) to succinctly communicate the order
of magnitude.
Exponent strings can be replaced with either SI prefixes or parts-per
identifiers.
See :ref:`exp_replacements` for all default supported
replacements.
Furthermore, it is possible to customize :class:`Formatter`
Expand All @@ -197,14 +164,6 @@ in addition to those provided by default.
>>> formatter = Formatter(exp_mode="engineering", exp_format="prefix")
>>> print(formatter(4242.13))
4.24213 k
>>> formatter = Formatter(
... exp_mode="binary_iec",
... round_mode="sig_fig",
... ndigits=4,
... exp_format="prefix",
... )
>>> print(formatter(1300))
1.270 Ki
>>> formatter = Formatter(exp_mode="engineering", exp_format="parts_per")
>>> print(formatter(12.3e-6))
12.3 ppm
Expand All @@ -217,11 +176,10 @@ Extra Exponent Replacements
In addition to the default
:ref:`exponent replacements <exp_replacements>`, The user can modify the
available exponent replacements using a number of options.
The SI prefix, IEC prefix, and parts-per replacements can be modified
using the ``extra_si_prefixes``, ``extra_iec_prefixes`` and
``extra_parts_per_forms`` options, respectively, and passing in
dictionaries with keys corresponding to integer exponents and values
corresponding to translated strings.
The SI prefix and parts-per replacements can be modified
using the ``extra_si_prefixes`` and ``extra_parts_per_forms`` options,
respectively, and passing in dictionaries with keys corresponding to
integer exponents and values corresponding to translated strings.
The entries in these dictionaries overwrite any default translation
mappings.

Expand Down Expand Up @@ -652,9 +610,6 @@ The capitalization of the exponent character can be controlled
>>> formatter = Formatter(exp_mode="scientific", capitalize=True)
>>> print(formatter(42))
4.2E+01
>>> formatter = Formatter(exp_mode="binary", capitalize=True)
>>> print(formatter(1024))
1B+10

The ``capitalize`` flag also controls the capitalization of ``nan`` and
``inf`` formatting:
Expand Down Expand Up @@ -705,7 +660,7 @@ Python supports ``'nan'``, ``'inf'``, and
and ``'-inf'`` or ``'NAN'``, ``'INF'``, and ``'-INF'``, respectively,
depending on ``capitalize``.
However, if ``nan_inf_exp=True`` (default ``False``), then, for
scientific, percent, engineering, and binary exponent modes, these will
scientific, percent, and engineering exponent modes, these will
instead be formatted as, e.g. ``'(nan)e+00'``.

>>> formatter = Formatter(
Expand Down
8 changes: 1 addition & 7 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ exponents such as ``"n" -> "e-09"``.
These translations are performed by first checking the default SI
prefixes along with any global ``extra_si_prefixes``, then checking
the default parts-per forms along with any global
``extra_parts_per_forms``, then checking the default IEC prefixes along
with any global ``extra_iec_prefixes``.
If an IEC prefix is detected then the exponent base is chosen to be 2.
``extra_parts_per_forms``.
if no valid translations are discovered or more than one valid
translation is discovered an exception is raised.

Expand Down Expand Up @@ -474,7 +472,6 @@ PopulatedOptions(
'left_pad_dec_place': 0,
'exp_format': 'standard',
'extra_si_prefixes': {},
'extra_iec_prefixes': {},
'extra_parts_per_forms': {},
'capitalize': False,
'superscript': False,
Expand Down Expand Up @@ -516,7 +513,6 @@ PopulatedOptions(
'left_pad_dec_place': 0,
'exp_format': 'standard',
'extra_si_prefixes': {},
'extra_iec_prefixes': {},
'extra_parts_per_forms': {},
'capitalize': False,
'superscript': False,
Expand Down Expand Up @@ -615,7 +611,6 @@ PopulatedOptions(
'left_pad_dec_place': 0,
'exp_format': 'standard',
'extra_si_prefixes': {},
'extra_iec_prefixes': {},
'extra_parts_per_forms': {},
'capitalize': False,
'superscript': True,
Expand Down Expand Up @@ -660,7 +655,6 @@ PopulatedOptions(
'left_pad_dec_place': 0,
'exp_format': 'standard',
'extra_si_prefixes': {},
'extra_iec_prefixes': {},
'extra_parts_per_forms': {},
'capitalize': False,
'superscript': True,
Expand Down
16 changes: 3 additions & 13 deletions src/sciform/api/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class Formatter:
'left_pad_dec_place': 0,
'exp_format': 'standard',
'extra_si_prefixes': {},
'extra_iec_prefixes': {},
'extra_parts_per_forms': {},
'capitalize': False,
'superscript': True,
Expand Down Expand Up @@ -153,7 +152,6 @@ def __init__( # noqa: PLR0913
left_pad_dec_place: int | None = None,
exp_format: option_types.ExpFormat | None = None,
extra_si_prefixes: dict[int, str] | None = None,
extra_iec_prefixes: dict[int, str] | None = None,
extra_parts_per_forms: dict[int, str] | None = None,
capitalize: bool | None = None,
superscript: bool | None = None,
Expand All @@ -180,7 +178,6 @@ def __init__( # noqa: PLR0913
* ``exp_val`` must be 0 for fixed point and percent modes
* ``exp_val`` must be a multiple of 3 for engineering and
shifted engineering modes
* ``exp_val`` must be a multiple of 10 for binary iec mode

* ``upper_separator`` may be any of ``['', ',', '.', ' ', '_']``
but must be different from ``decimal_separator``
Expand All @@ -189,13 +186,12 @@ def __init__( # noqa: PLR0913

:param exp_mode: Specify the exponent formatting mode.
:type exp_mode: ``Literal['fixed_point', 'percent',
'scientific', 'engineering', 'engineering_shifted', 'binary',
'binary_iec'] | None``
'scientific', 'engineering', 'engineering_shifted'] | None``
:param exp_val: Indicates how the exponent value should be
chosen. If an integer is specified, the value must be 0 for
fixed point and percent modes, an integer multiple of 3 for
engineering and engineering shifted modes, and an integer
multiple of 10 for binary IEC mode. Can be set to ``"auto"``.
engineering and engineering shifted modes. Can be set to
``"auto"``.
:type exp_val: ``int | Literal['auto'] | None``
:param round_mode: Indicate how to round numbers during
formatting.
Expand Down Expand Up @@ -231,11 +227,6 @@ def __init__( # noqa: PLR0913
values to si prefixes. Entries overwrite default values. A
value of ``None`` means that exponent will not be converted.
:type extra_si_prefixes: ``dict[int, Union[str, None]] | None``
:param extra_iec_prefixes: Dictionary mapping additional
exponent values to iec prefixes. Entries overwrite default
values. A value of ``None`` means that exponent will not be
converted.
:type extra_iec_prefixes: ``dict[int, Union[str, None]] | None``
:param extra_parts_per_forms: Dictionary mapping additional
exponent values to "parts-per" forms. Entries overwrite
default values. A value of ``None`` means that exponent will
Expand Down Expand Up @@ -297,7 +288,6 @@ def __init__( # noqa: PLR0913
left_pad_dec_place=left_pad_dec_place,
exp_format=exp_format,
extra_si_prefixes=extra_si_prefixes,
extra_iec_prefixes=extra_iec_prefixes,
extra_parts_per_forms=extra_parts_per_forms,
capitalize=capitalize,
superscript=superscript,
Expand Down
Loading