Skip to content

Commit 10f2a29

Browse files
Rename datetime to timestamp variables (#742)
.There were internal variables in OrderedRingBuffer and MovingWindow that were named with the prefix `datetime` as a way to refer to the timestamps that are stored, which happened to be represented using a datetime object, but timestamps are actually what it is stored. To avoid confusion when reading the code, these variables were renamed from `_datetime` to `_timestamp`. Fixes #691
2 parents 81de64a + 4d96865 commit 10f2a29

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def __init__( # pylint: disable=too-many-arguments
150150
given sampling period.
151151
input_sampling_period: The time interval between consecutive input samples.
152152
resampler_config: The resampler configuration in case resampling is required.
153-
align_to: A datetime object that defines a point in time to which
153+
align_to: A timestamp that defines a point in time to which
154154
the window is aligned to modulo window size. For further
155155
information, consult the class level documentation.
156156
name: The name of this moving window. If `None`, `str(id(self))` will be
@@ -299,10 +299,10 @@ def window(
299299
and returns an array.
300300
301301
Args:
302-
start: The start of the time interval. If `None`, the start of the
303-
window is used.
304-
end: The end of the time interval. If `None`, the end of the window
305-
is used.
302+
start: The start timestamp of the time interval. If `None`, the
303+
start of the window is used.
304+
end: The end timestamp of the time interval. If `None`, the end of
305+
the window is used.
306306
force_copy: If `True`, the returned array is a copy of the underlying
307307
data. Otherwise, if possible, a view of the underlying data is
308308
returned.
@@ -402,7 +402,7 @@ def __getitem__(self, key: SupportsIndex | datetime | slice) -> float | ArrayLik
402402
403403
* If the key is an integer, the float value of that key
404404
at the given position is returned.
405-
* If the key is a datetime object, the float value of that key
405+
* If the key is a timestamp, the float value of that key
406406
that corresponds to the timestamp is returned.
407407
* If the key is a slice of timestamps or integers, an ndarray is returned,
408408
where the bounds correspond to the slice bounds.

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class Gap:
2424
"""A gap defines the range for which we haven't received values yet."""
2525

2626
start: datetime
27-
"""Start of the range, inclusive."""
27+
"""Start timestamp of the range, inclusive."""
2828
end: datetime
29-
"""End of the range, exclusive."""
29+
"""End timestamp of the range, exclusive."""
3030

3131
def contains(self, timestamp: datetime) -> bool:
3232
"""Check if a given timestamp is inside this gap.
@@ -46,11 +46,11 @@ def contains(self, timestamp: datetime) -> bool:
4646
class OrderedRingBuffer(Generic[FloatArray]):
4747
"""Time aware ringbuffer that keeps its entries sorted by time."""
4848

49-
_DATETIME_MIN = datetime.min.replace(tzinfo=timezone.utc)
50-
"""The minimum representable datetime value."""
49+
_TIMESTAMP_MIN = datetime.min.replace(tzinfo=timezone.utc)
50+
"""The minimum representable timestamp."""
5151

52-
_DATETIME_MAX = datetime.max.replace(tzinfo=timezone.utc)
53-
"""The maximum representable datetime value."""
52+
_TIMESTAMP_MAX = datetime.max.replace(tzinfo=timezone.utc)
53+
"""The maximum representable timestamp."""
5454

5555
def __init__(
5656
self,
@@ -82,8 +82,8 @@ def __init__(
8282
self._time_index_alignment: datetime = align_to
8383

8484
self._gaps: list[Gap] = []
85-
self._datetime_newest: datetime = self._DATETIME_MIN
86-
self._datetime_oldest: datetime = self._DATETIME_MAX
85+
self._timestamp_newest: datetime = self._TIMESTAMP_MIN
86+
self._timestamp_oldest: datetime = self._TIMESTAMP_MAX
8787
self._full_time_range: timedelta = len(self._buffer) * self._sampling_period
8888

8989
@property
@@ -148,17 +148,17 @@ def update(self, sample: Sample[QuantityT]) -> None:
148148

149149
# Don't add outdated entries
150150
if (
151-
timestamp < self._datetime_oldest
152-
and self._datetime_oldest != self._DATETIME_MAX
151+
timestamp < self._timestamp_oldest
152+
and self._timestamp_oldest != self._TIMESTAMP_MAX
153153
):
154154
raise IndexError(
155-
f"Timestamp {timestamp} too old (cut-off is at {self._datetime_oldest})."
155+
f"Timestamp {timestamp} too old (cut-off is at {self._timestamp_oldest})."
156156
)
157157

158158
# Update timestamps
159-
prev_newest = self._datetime_newest
160-
self._datetime_newest = max(self._datetime_newest, timestamp)
161-
self._datetime_oldest = self._datetime_newest - (
159+
prev_newest = self._timestamp_newest
160+
self._timestamp_newest = max(self._timestamp_newest, timestamp)
161+
self._timestamp_oldest = self._timestamp_newest - (
162162
self._full_time_range - self._sampling_period
163163
)
164164

@@ -180,7 +180,7 @@ def time_bound_oldest(self) -> datetime:
180180
Returns:
181181
The timestamp of the oldest sample of the ring buffer.
182182
"""
183-
return self._datetime_oldest
183+
return self._timestamp_oldest
184184

185185
@property
186186
def time_bound_newest(self) -> datetime:
@@ -191,7 +191,7 @@ def time_bound_newest(self) -> datetime:
191191
The timestamp of the newest sample of the ring buffer
192192
or None if the buffer is empty.
193193
"""
194-
return self._datetime_newest
194+
return self._timestamp_newest
195195

196196
@property
197197
def oldest_timestamp(self) -> datetime | None:
@@ -242,12 +242,12 @@ def to_internal_index(
242242
timestamp = self.normalize_timestamp(timestamp)
243243

244244
if not allow_outside_range and (
245-
self._datetime_newest + self._sampling_period < timestamp
246-
or timestamp < self._datetime_oldest
245+
self._timestamp_newest + self._sampling_period < timestamp
246+
or timestamp < self._timestamp_oldest
247247
):
248248
raise IndexError(
249249
f"Requested timestamp {timestamp} is "
250-
f"outside the range [{self._datetime_oldest} - {self._datetime_newest}]"
250+
f"outside the range [{self._timestamp_oldest} - {self._timestamp_newest}]"
251251
)
252252

253253
return self.wrap(
@@ -271,7 +271,7 @@ def get_timestamp(self, index: int) -> datetime | None:
271271
index: Index to convert.
272272
273273
Returns:
274-
Datetime index where the value for the given index can be found.
274+
Timestamp where the value for the given index can be found.
275275
Or None if the buffer is empty.
276276
"""
277277
if self.oldest_timestamp is None:
@@ -323,8 +323,8 @@ def window(
323323
missing entries, they can safely do so.
324324
325325
Args:
326-
start: start time of the window.
327-
end: end time of the window.
326+
start: start timestamp of the window.
327+
end: end timestamp of the window.
328328
force_copy: optional, default True. If True, will always create a
329329
copy of the data.
330330
fill_value: If not None, will use this value to fill missing values.
@@ -476,9 +476,9 @@ def _update_gaps(
476476

477477
if not record_as_missing:
478478
# Replace all gaps with one if we went far into then future
479-
if self._datetime_newest - newest >= self._full_time_range:
479+
if self._timestamp_newest - newest >= self._full_time_range:
480480
self._gaps = [
481-
Gap(start=self._datetime_oldest, end=self._datetime_newest)
481+
Gap(start=self._timestamp_oldest, end=self._timestamp_newest)
482482
]
483483
return
484484

@@ -518,11 +518,11 @@ def _cleanup_gaps(self) -> None:
518518
w_2 = None
519519

520520
# Delete out-of-date gaps
521-
if w_1.end <= self._datetime_oldest:
521+
if w_1.end <= self._timestamp_oldest:
522522
del self._gaps[i]
523523
# Update start of gap if it's rolled out of the buffer
524-
elif w_1.start < self._datetime_oldest:
525-
self._gaps[i].start = self._datetime_oldest
524+
elif w_1.start < self._timestamp_oldest:
525+
self._gaps[i].start = self._timestamp_oldest
526526
# If w2 is a subset of w1 we can delete it
527527
elif w_2 and w_1.start <= w_2.start and w_1.end >= w_2.end:
528528
del self._gaps[i + 1]
@@ -682,7 +682,7 @@ def count_valid(self) -> int:
682682
Returns:
683683
The number of valid items in this buffer.
684684
"""
685-
if self._datetime_newest == self._DATETIME_MIN:
685+
if self._timestamp_newest == self._TIMESTAMP_MIN:
686686
return 0
687687

688688
# Sum of all elements in the gap ranges
@@ -692,15 +692,15 @@ def count_valid(self) -> int:
692692
(
693693
gap.end
694694
# Don't look further back than oldest timestamp
695-
- max(gap.start, self._datetime_oldest)
695+
- max(gap.start, self._timestamp_oldest)
696696
)
697697
// self._sampling_period
698698
for gap in self._gaps
699699
),
700700
)
701701

702-
start_pos = self.to_internal_index(self._datetime_oldest)
703-
end_pos = self.to_internal_index(self._datetime_newest)
702+
start_pos = self.to_internal_index(self._timestamp_oldest)
703+
end_pos = self.to_internal_index(self._timestamp_newest)
704704

705705
if end_pos < start_pos:
706706
return len(self._buffer) - start_pos + end_pos + 1 - sum_missing_entries

tests/timeseries/test_moving_window.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ def init_moving_window(
6767

6868

6969
def dt(i: int) -> datetime: # pylint: disable=invalid-name
70-
"""Create datetime objects from indices.
70+
"""Create a timestamp from the given index.
7171
7272
Args:
73-
i: Index to create datetime from.
73+
i: The index to create a timestamp from.
7474
7575
Returns:
76-
Datetime object.
76+
The timestamp created from the index.
7777
"""
7878
return datetime.fromtimestamp(i, tz=timezone.utc)
7979

tests/timeseries/test_ringbuffer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ def test_timestamp_ringbuffer_missing_parameter(
192192

193193

194194
def dt(i: int) -> datetime: # pylint: disable=invalid-name
195-
"""Create datetime objects from indices.
195+
"""Create a timestamp from the given index.
196196
197197
Args:
198-
i: Index to create datetime from.
198+
i: The index to create a timestamp from.
199199
200200
Returns:
201-
Datetime object.
201+
The timestamp created from the index.
202202
"""
203203
return datetime.fromtimestamp(i, tz=timezone.utc)
204204

tests/timeseries/test_ringbuffer_serialization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def load_dump_test(dumped: rb.OrderedRingBuffer[Any], path: str) -> None:
5252
np.testing.assert_equal(dumped[:], loaded[:])
5353

5454
# pylint: disable=protected-access
55-
assert dumped._datetime_oldest == loaded._datetime_oldest
55+
assert dumped._timestamp_oldest == loaded._timestamp_oldest
5656
# pylint: disable=protected-access
57-
assert dumped._datetime_newest == loaded._datetime_newest
57+
assert dumped._timestamp_newest == loaded._timestamp_newest
5858
# pylint: disable=protected-access
5959
assert len(dumped._gaps) == len(loaded._gaps)
6060
# pylint: disable=protected-access

0 commit comments

Comments
 (0)