def recover_state(self): """recovers the state from last persisted and applies any events from the event log""" if isinstance(self._state_connector, ConnectorContract): handler = HandlerFactory.instantiate(self._state_connector) self.__book_state = handler.load_canonical() else: self.__book_state = pd.DataFrame() super()._set_modified(True) if isinstance(self._events_connector, ConnectorContract): handler = HandlerFactory.instantiate(self._events_connector) self.__events_log = handler.load_canonical() _event_times = pd.Series(list( self.__events_log.keys())).sort_values().reset_index(drop=True) for _items in _event_times: _action, _event = self.__events_log.get( _items, ['add', pd.DataFrame()]) if str(_action).lower() == 'add': self.add_event(event=_event) elif str(_action).lower() == 'increment': self.increment_event(event=_event) elif str(_action).lower() == 'decrement': self.decrement_event(event=_event) else: self.__events_log = dict() return
def save_state(self, with_reset: bool = None, fillna: bool = None, **kwargs): """ saves the current state and optionally resets the event book""" if isinstance(self._state_connector, ConnectorContract): _current_state = self.current_state(fillna=fillna) handler = HandlerFactory.instantiate(self._state_connector) handler.persist_canonical(_current_state, **kwargs) if isinstance(with_reset, bool) and with_reset: self.reset_state() return
def backup_state(self, stamp_uri: str = None, fillna: bool = None, **kwargs): """ persists the event book state with an alternative to save off a stamped copy to a provided URI :param stamp_uri: in addition to persisting the event book, save to this uri :param fillna: if the NAN values in the current state should be filled :return: """ if isinstance(self._state_connector, ConnectorContract) and isinstance( stamp_uri, str): _current_state = self.current_state(fillna=fillna) handler = HandlerFactory.instantiate(self._state_connector) handler.backup_canonical(canonical=_current_state, uri=stamp_uri, **kwargs) return
def _persist_events(self): """Saves the pandas.DataFrame to the persisted stater""" if isinstance(self._events_connector, ConnectorContract): handler = HandlerFactory.instantiate(self._events_connector) handler.persist_canonical(self.__events_log) return