Skip to content

Commit a8cde98

Browse files
committed
Replace average with statistics.fmean
The `average` function is just a mean function and Python already offers a function to calculate a mean, so we use that instead. We also wrap `fmean` to match the `ResamplingFunction` signature inline so it is clear in the documentation what the default resampling function does. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 26fa527 commit a8cde98

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

src/frequenz/sdk/timeseries/_resampling/_config.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
import logging
9+
import statistics
910
from collections.abc import Sequence
1011
from dataclasses import dataclass
1112
from datetime import datetime, timedelta
@@ -79,27 +80,6 @@ def __call__(
7980
... # pylint: disable=unnecessary-ellipsis
8081

8182

82-
def average(
83-
samples: Sequence[tuple[datetime, float]],
84-
resampler_config: ResamplerConfig, # pylint: disable=unused-argument
85-
source_properties: SourceProperties, # pylint: disable=unused-argument
86-
) -> float:
87-
"""Calculate average of all the provided values.
88-
89-
Args:
90-
samples: The samples to apply the average to. It must be non-empty.
91-
resampler_config: The configuration of the resampler calling this
92-
function.
93-
source_properties: The properties of the source being resampled.
94-
95-
Returns:
96-
The average of all `samples` values.
97-
"""
98-
assert len(samples) > 0, "Average cannot be given an empty list of samples"
99-
values = list(sample[1] for sample in samples)
100-
return sum(values) / len(values)
101-
102-
10383
@dataclass(frozen=True)
10484
class ResamplerConfig:
10585
"""Resampler configuration."""
@@ -134,7 +114,9 @@ class ResamplerConfig:
134114
passed to the resampling function.
135115
"""
136116

137-
resampling_function: ResamplingFunction = average
117+
resampling_function: ResamplingFunction = lambda samples, _, __: statistics.fmean(
118+
s[1] for s in samples
119+
)
138120
"""The resampling function.
139121
140122
This function will be applied to the sequence of relevant samples at

0 commit comments

Comments
 (0)