Пример #1
0
def test_fix_order_size_dict_negative_max():
    d = FixSizeOrderedDict(max_size=-1)
    d["test"] = 1
    d["test2"] = 2
    d["test3"] = 3

    del d["test2"]

    assert d == {"test": 1, "test3": 3}
Пример #2
0
def test_fix_order_size_dict_max():
    d = FixSizeOrderedDict(max_size=1)
    d["test"] = 1
    d["test2"] = 2
    d["test3"] = 3

    with pytest.raises(KeyError):
        del d["test2"]

    assert d == {"test3": 3}
Пример #3
0
class ProtectEventStateMachine:
    """A simple state machine for cameras."""

    _events: FixSizeOrderedDict[str, Dict[str, Any]]

    def __init__(self) -> None:
        """Init the state machine."""
        self._events = FixSizeOrderedDict(
            max_size=MAX_EVENT_HISTORY_IN_STATE_MACHINE)

    def add(self, event_id: str, event_json: Dict[str, Any]) -> None:
        """Add an event to the state machine."""
        self._events[event_id] = event_json

    def update(self, event_id: str,
               new_event_json: Dict[str, Any]) -> Optional[Dict[str, Any]]:
        """Update an event in the state machine and return the merged event."""
        event_json = self._events.get(event_id)
        if event_json is None:
            return None
        event_json.update(new_event_json)
        return event_json
Пример #4
0
 def __init__(self) -> None:
     """Init the state machine."""
     self._events = FixSizeOrderedDict(
         max_size=MAX_EVENT_HISTORY_IN_STATE_MACHINE)