26 lines
808 B
Python
26 lines
808 B
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
class DeltaStore:
|
|
def __init__(self) -> None:
|
|
self.last_full_state: Dict[str, Any] = {}
|
|
|
|
def compute_delta(self, current_state: Dict[str, Any], previous_state: Dict[str, Any]) -> Dict[str, Any]:
|
|
delta: Dict[str, Any] = {}
|
|
# naive shallow diff
|
|
keys = set(current_state.keys()) | set(previous_state.keys())
|
|
for k in keys:
|
|
cur = current_state.get(k)
|
|
prev = previous_state.get(k)
|
|
if cur != prev:
|
|
delta[k] = cur
|
|
return delta
|
|
|
|
def apply_delta(self, state: Dict[str, Any], delta: Dict[str, Any]) -> Dict[str, Any]:
|
|
new_state = dict(state)
|
|
for k, v in delta.items():
|
|
new_state[k] = v
|
|
return new_state
|