Skip to content

Commit

Permalink
refac: Using get/set state for pickling
Browse files Browse the repository at this point in the history
  • Loading branch information
fgmacedo committed Nov 30, 2024
1 parent 681fc04 commit 1e534c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
18 changes: 2 additions & 16 deletions statemachine/engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,13 @@

class BaseEngine:
def __init__(self, sm: "StateMachine", rtc: bool = True):
self.sm: StateMachine = proxy(sm)
self._external_queue: Queue = PriorityQueue()
self._sentinel = object()
self._rtc = rtc
self._running = True
self._init(sm)

def _init(self, sm: "StateMachine"):
self.sm: StateMachine = proxy(sm)
self._external_queue: Queue = PriorityQueue()
self._processing = Lock()

def __getstate__(self) -> dict:
state = self.__dict__.copy()
del state["_external_queue"]
del state["_processing"]
del state["sm"]
return state

def __setstate__(self, state: dict) -> None:
for attr, value in state.items():
setattr(self, attr, value)

def empty(self):
return self._external_queue.qsize() == 0

Expand Down
4 changes: 2 additions & 2 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ def assertions(sm, _reference):
if not sm._listeners:
pytest.fail("did not found any observer")

for listener in sm._listeners:
for listener in sm._listeners.values():
listener.let_me_be_visible = False

with pytest.raises(TransitionNotAllowed):
sm.send("publish")

sm.model.let_me_be_visible = True

for listener in sm._listeners:
for listener in sm._listeners.values():
with pytest.raises(TransitionNotAllowed):
sm.send("publish")

Expand Down

0 comments on commit 1e534c8

Please sign in to comment.