Skip to content

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 iterable is empty and initial and default are MISSING, a TypeError is raised.
  • If iterable is empty and initial is MISSING, but default is not, then default is 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
def 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 `iterable` is empty and `initial` and `default` are `MISSING`, a
      `TypeError` is raised.
    - If `iterable` is empty and `initial` is `MISSING`, but `default` is not,
      then `default` is returned.
    """
    if initial is not MISSING:
        return reduce(function, iterable, initial)
    else:
        it = iter(iterable)
        try:
            initial = next(it)
        except StopIteration:
            if default is not MISSING:
                return default
            else:
                raise TypeError(
                        'accumulation of empty iterable with no'
                        ' initial or default value'
                )
        return reduce(function, it, initial)

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 iterable is empty and initial and default are MISSING, a TypeError is raised.
  • If iterable is empty and initial is MISSING, but default is not, then default is returned.
  • If initial is MISSING, then there is truly no initial 0+=.
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
def 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 `iterable` is empty and `initial` and `default` are `MISSING`, a
      `TypeError` is raised.
    - If `iterable` is empty and `initial` is `MISSING`, but `default` is not,
      then `default` is returned.
    - If `initial` is `MISSING`, then there is truly no initial `0+=`.
    """
    if initial is not MISSING:
        return sum(iterable, initial)
    else:
        it = iter(iterable)
        try:
            initial = next(it)
        except StopIteration:
            if default is not MISSING:
                return default
            else:
                raise TypeError(
                        'accumulation of empty iterable with no'
                        ' initial or default value'
                )
        return sum(it, start=initial)

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 iterable is empty and initial and default are MISSING, a TypeError is raised.
  • If iterable is empty and initial is MISSING, but default is not, then default is returned.
  • If initial is MISSING, then there is truly no initial 1*=.
Source code in operationcounter\accumulators.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def 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 `iterable` is empty and `initial` and `default` are `MISSING`, a
      `TypeError` is raised.
    - If `iterable` is empty and `initial` is `MISSING`, but `default` is not,
      then `default` is returned.
    - If `initial` is `MISSING`, then there is truly no initial `1*=`.
    """
    #don't use math.prod, as it may reject non-numeric values
    return reduce_default(mul, iterable, initial=initial, default=default)

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 a or b is empty and initial and default are MISSING, a TypeError is raised.
  • If a or b is empty and initial is MISSING, but default is not, then default is returned.
  • If initial is MISSING, then there is truly no initial 0+=.
Source code in operationcounter\accumulators.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def 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 `a` or `b` is empty and `initial` and `default` are `MISSING`, a
      `TypeError` is raised.
    - If `a` or `b` is empty and `initial` is `MISSING`, but `default` is not,
      then `default` is returned.
    - If `initial` is `MISSING`, then there is truly no initial `0+=`.
    """
    return sum_default(map(mul, a, b), initial=initial, default=default)