Skip to content

Commit

Permalink
Add some missing replay code (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x26res authored Jun 6, 2024
1 parent 3bd1147 commit 9973baa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions beavers/replay.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for replaying historical data."""

import abc
import collections.abc
import dataclasses
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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()
8 changes: 8 additions & 0 deletions tests/test_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from beavers.replay import (
DataSource,
IteratorDataSourceAdapter,
NoOpDataSinkProvider,
ReplayContext,
ReplayDriver,
T,
Expand Down Expand Up @@ -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()

0 comments on commit 9973baa

Please sign in to comment.