accumulators
graph TD
Z(["<code>sum(iterable, initial=initial, default=default)</code>"]) --> A{"<code>initial</code> <code>MISSING</code>?"};
A --> |no| B["<code>return initial+...</code>"];
A --> |yes| C{"<code>iterable</code> empty?"};
C --> |no| D["<code>return iterable[0]+...</code>"];
C --> |yes| E{"<code>default</code> <code>MISSING</code>?"};
E --> |no| F["<code>return default</code>"];
E --> |yes| G["<code>raise TypeError</code>"];
operationcounter.accumulators
operationcounter.accumulators module.
Accumulators.
MISSING: object = object()
module-attribute
Sentinel to mark empty parameters.
reduce_default(function: Callable[[Any, Any], Any], iterable: Iterable[Any], *, initial: Any = MISSING, default: Any = MISSING) -> Any
Apply function of two arguments cumulatively to the iterable.
Like functools.reduce but with an optional initial element and an
optional default return value.
Difference to functools.reduce:
- If
iterableis empty andinitialanddefaultareMISSING, aTypeErroris raised. - If
iterableis empty andinitialisMISSING, butdefaultis not, thendefaultis returned.
Source code in operationcounter\accumulators.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
sum_default(iterable: Iterable[Any], *, initial: Any = MISSING, default: Any = 0) -> Any
Return the sum of all elements in the iterable.
Like sum but with an optional initial element and an optional default
return value.
- If
iterableis empty andinitialanddefaultareMISSING, aTypeErroris raised. - If
iterableis empty andinitialisMISSING, butdefaultis not, thendefaultis returned. - If
initialisMISSING, then there is truly no initial0+=.
Source code in operationcounter\accumulators.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
prod_default(iterable: Iterable[Any], *, initial: Any = MISSING, default: Any = 1) -> Any
Return the product of all elements in the iterable.
Like math.prod but with an optional initial element and an optional
default return value.
- If
iterableis empty andinitialanddefaultareMISSING, aTypeErroris raised. - If
iterableis empty andinitialisMISSING, butdefaultis not, thendefaultis returned. - If
initialisMISSING, then there is truly no initial1*=.
Source code in operationcounter\accumulators.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
sumprod_default(a: Iterable[Any], b: Iterable[Any], *, initial: Any = MISSING, default: Any = 0) -> Any
Return the sum-product of all elements in the iterables.
Like math.sumprod but with an optional initial element, an optional
default return value and non-strict zipping of both iterables.
- If
aorbis empty andinitialanddefaultareMISSING, aTypeErroris raised. - If
aorbis empty andinitialisMISSING, butdefaultis not, thendefaultis returned. - If
initialisMISSING, then there is truly no initial0+=.
Source code in operationcounter\accumulators.py
89 90 91 92 93 94 95 96 97 98 99 100 101 | |