Skip to content

Instantly share code, notes, and snippets.

@anatoly-scherbakov
Created April 24, 2020 03:03
Show Gist options
  • Save anatoly-scherbakov/7e17ce45e9aa197d184e83810d914d2a to your computer and use it in GitHub Desktop.
Save anatoly-scherbakov/7e17ce45e9aa197d184e83810d914d2a to your computer and use it in GitHub Desktop.
MementoSet
import dataclasses
from datetime import datetime
from typing import Optional, Dict, Iterable
@dataclasses.dataclass(frozen=True)
class Memento:
original_url: str
time: datetime
archived_headers_cid: str
archived_body_cid: str
data: Optional[Dict[str, str]] = None
@dataclasses.dataclass(frozen=True)
class MementoCursor:
first: Optional[Memento]
previous: Optional[Memento]
closest: Optional[Memento]
next: Optional[Memento]
last: Optional[Memento]
@dataclasses.dataclass(frozen=True)
class MementoSet:
original_uri: Optional[str] = None
def all(self) -> Iterable[Memento]:
...
def by_original_uri(self, uri: Optional[str]) -> 'MementoSet':
return dataclasses.replace(
self,
original_uri=uri,
)
def first(self) -> Optional[Memento]:
...
def last(self) -> Optional[Memento]:
...
def closest_to(self, time: datetime) -> Optional[Memento]:
...
def before(self, memento: Memento) -> Optional[Memento]:
...
def after(self, memento: Memento) -> Optional[Memento]:
...
def cursor(self, time: datetime) -> MementoCursor:
closest = self.closest_to(time)
return MementoCursor(
first=self.first(),
previous=self.before(closest) if closest is not None else None,
closest=closest,
next=self.after(closest) if closest is not None else None,
last=self.last()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment