import datetime import typing import tinydb from . import _radar Period = typing.Tuple[datetime.datetime, datetime.datetime] class Storage: def __init__(self, pathname): self.db = tinydb.TinyDB(pathname, create_dirs=True) def show(self, period: Period=None): if not period: now = datetime.datetime.now() period = (now, now + datetime.timedelta(days=5)) def _is_between(prediction): return period[0] < prediction.time and period[1] > prediction.time return filter(_is_between, map(_radar.Prediction.from_json, self.db.all())) def extend(self, predictions: typing.Iterable[_radar.Prediction]): for prediction in predictions: self.db.insert(prediction.json()) def __iter__(self): return self.show()