Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some missing replay code #56

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()