From 9973baa73fd781656938578f9f0cefe7a283a389 Mon Sep 17 00:00:00 2001 From: 0x26res Date: Thu, 6 Jun 2024 10:15:07 +0100 Subject: [PATCH] Add some missing replay code (#56) --- beavers/replay.py | 23 +++++++++++++++++++++++ tests/test_replay.py | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/beavers/replay.py b/beavers/replay.py index 075d0b2..22a41b6 100644 --- a/beavers/replay.py +++ b/beavers/replay.py @@ -1,4 +1,5 @@ """Module for replaying historical data.""" + import abc import collections.abc import dataclasses @@ -36,6 +37,11 @@ class ReplayContext: end: pd.Timestamp frequency: pd.Timedelta + def __post_init__(self): + """Check arguments are valid.""" + assert self.start.tzname() == "UTC" + assert self.end.tzname() == "UTC" + class DataSource(Protocol[T]): """Interface for replaying historical data from a file or database.""" @@ -363,3 +369,20 @@ def _next(self) -> Optional[DataSource]: return next(self._sources) except StopIteration: return None + + +class NoOpDataSink(DataSink): + """DataSink that does nothing.""" + + def append(self, timestamp: pd.Timestamp, data: T): + pass + + def close(self): + pass + + +class NoOpDataSinkProvider: + """DataSinkProvider that provides a NoOpDataSink.""" + + def __call__(self, context: ReplayContext) -> DataSink[T]: + return NoOpDataSink() diff --git a/tests/test_replay.py b/tests/test_replay.py index bd108cd..6756ef1 100644 --- a/tests/test_replay.py +++ b/tests/test_replay.py @@ -8,6 +8,7 @@ from beavers.replay import ( DataSource, IteratorDataSourceAdapter, + NoOpDataSinkProvider, ReplayContext, ReplayDriver, T, @@ -316,3 +317,10 @@ def test_replay_run_cycle(): assert metrics.warp_ratio > 0.0 assert driver.current_time == pd.to_datetime("2023-01-02 12:00:00Z") assert driver.is_done() + + +def test_no_op(): + provider = NoOpDataSinkProvider() + data_sink = provider(ReplayContext(UTC_MAX, UTC_MAX, pd.to_timedelta("1s"))) + data_sink.append(UTC_MAX, None) + data_sink.close()