Skip to content

Commit d2c5331

Browse files
committed
docs: add bitmath.sum() reference entry to module.rst
Documents the bitmath.sum() function added in 2.0.0 in the module API reference, positioned after bitmath.listdir(). Includes parameter table, note contrasting it with built-in sum(), and a seealso cross-reference to the Getting Started page. Also adds the simple_examples_summing anchor needed by that cross-reference.
1 parent 580e8cb commit d2c5331

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

docsite/source/module.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,66 @@ bitmath.listdir()
218218
.. versionadded:: 1.0.7
219219

220220

221+
.. _bitmath_sum:
222+
223+
bitmath.sum()
224+
=============
225+
226+
.. function:: sum(iterable[, start=None])
227+
228+
Sum an iterable of bitmath instances into a single bitmath instance.
229+
230+
:param iterable: Any iterable of bitmath objects to sum.
231+
:param start: **Default:** ``None`` (accumulates into
232+
:class:`bitmath.Byte`). Pass a bitmath instance to
233+
set both the starting value and the result type.
234+
:type start: A bitmath instance, or ``None``
235+
:returns: A bitmath instance whose type is determined by ``start``
236+
(or :class:`bitmath.Byte` when ``start`` is ``None``).
237+
238+
.. note::
239+
240+
Python's built-in :py:func:`sum` also works with bitmath objects.
241+
Because ``0 + bm`` returns ``bm`` itself (the ``__radd__`` identity
242+
element), the built-in accumulates into the type of the **first
243+
element** in the iterable. Use :py:func:`bitmath.sum` instead when
244+
you need the result normalised to a **specific unit** regardless of
245+
the input types.
246+
247+
Sum a homogeneous list — result type matches ``start`` (``Byte`` by
248+
default):
249+
250+
.. code-block:: python
251+
252+
>>> import bitmath
253+
>>> bitmath.sum([bitmath.MiB(1), bitmath.GiB(1)])
254+
Byte(1074790400.0)
255+
256+
Pass ``start`` to choose a different accumulator unit:
257+
258+
.. code-block:: python
259+
260+
>>> bitmath.sum([bitmath.KiB(1), bitmath.KiB(2)], start=bitmath.MiB(0))
261+
MiB(0.0029296875)
262+
263+
Contrast with the built-in :py:func:`sum`, whose result type tracks the
264+
first element:
265+
266+
.. code-block:: python
267+
268+
>>> sum([bitmath.KiB(1), bitmath.KiB(2)])
269+
KiB(3.0)
270+
>>> sum([bitmath.Byte(1), bitmath.MiB(1), bitmath.GiB(1)])
271+
Byte(1074790401.0)
272+
273+
.. seealso::
274+
275+
:ref:`Summing an Iterable <simple_examples_summing>` in *Getting Started*
276+
Side-by-side examples of built-in :py:func:`sum` vs
277+
:py:func:`bitmath.sum`.
278+
279+
.. versionadded:: 2.0.0
280+
221281

222282
bitmath.parse_string()
223283
======================

docsite/source/simple_examples.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ automatically:
249249
:py:func:`bitmath.parse_string` — full parameter reference and caveats.
250250

251251

252+
.. _simple_examples_summing:
253+
252254
Summing an Iterable
253255
*******************
254256

@@ -265,18 +267,37 @@ the iterable:
265267
>>> sum([bitmath.Byte(1), bitmath.MiB(1), bitmath.GiB(1)])
266268
Byte(1074790401.0)
267269
270+
Results from mixing plain numbers and numbers with units yields a
271+
result with no units.
272+
273+
.. code-block:: python
274+
275+
>>> sum([bitmath.Byte(1), 0])
276+
1.0
277+
>>> sum([1, bitmath.KiB(2)])
278+
3.0
279+
280+
.. seealso::
281+
282+
:ref:`Appendix: Rules for Math <appendix_math_mixed_types>` — for a
283+
thrilling discussion about the minute details when doing mixed-type
284+
math math. What it all boils down to is this: if we don’t provide a
285+
unit then bitmath won’t give us one back.
286+
287+
268288
Use :py:func:`bitmath.sum` when you need the result **normalised to a
269289
specific unit** regardless of the input types. Without a ``start``
270290
argument it accumulates into :class:`bitmath.Byte`; pass ``start`` to
271-
choose a different accumulator:
291+
choose a different accumulator (resultant unit):
272292

273293
.. code-block:: python
274294
275295
>>> bitmath.sum([bitmath.MiB(1), bitmath.GiB(1)])
276296
Byte(1074790400.0)
277297
>>> bitmath.sum([bitmath.KiB(1), bitmath.KiB(2)], start=bitmath.MiB(0))
278298
MiB(0.0029296875)
279-
299+
>>> bitmath.sum([bitmath.MiB(100), bitmath.KiB(2000)], start=bitmath.GiB(0))
300+
GiB(0.0995635986328125)
280301
281302
Rounding
282303
********

0 commit comments

Comments
 (0)