python 2.7 - Not able to get running the timeout mechanism shown in https://github.com/tyarkoni/transitions/issues/198. -
need help.
trying put in place timeout functionality state machine mentioned here transitions library. on init of machine keep receiving:
typeerror: init() got unexpected keyword argument 'parent'
although cunstructor parameters propagated.
my code(python 2.7.6/ipython 5.1.0):
from transitions.extensions import hierarchicalmachine machine import time threading import thread transitions import state class timeout(thread): def __init__(self, func, timeout): super(timeout, self).__init__() self.func = func self.timeout = timeout self.canceled = false print 'starting countdown' self.start() def run(self): time.sleep(self.timeout) print 'timeout occurred' if not self.canceled: self.func() class timeoutstate(state): def __init__(self, name, timeout=none, *args, **kwargs): self.timeout = timeout super(timeoutstate, self).__init__(name=name, *args, **kwargs) class timeoutmachine(machine): def __init__(self, model, states, transitions, initial, title): super(machine, self).__init__( model=model, states=states, transitions=transitions, initial=initial, name=title, queued=true, auto_transitions=true, send_event=true) def _create_state(self, *args, **kwargs): return timeoutstate(*args, **kwargs) class model(): def __init__(self): self.timer = none def set_timeout(self, event_data): timeout=event_data.kwargs.pop('timeout',3) self.timer = timeout(self.dotimeout, timeout) model = model() transitions = [{'dotimeout', 'b', 'c'}] b = timeoutstate(name='b',timeout=5, on_enter='set_timeout') machine = timeoutmachine(model=model, states=['a', b, 'c'], transitions=transitions, initial='a', title='testmachinewithtimeouts')
error stack:
traceback (most recent call last): file "/usr/local/lib/python2.7/dist-packages/ipython/core/interactiveshell.py", line 2881, in run_code exec(code_obj, self.user_global_ns, self.user_ns) file "<ipython-input-40-747d39e2dc66>", line 1, in <module> t.timeoutmachine(model=model, states=['a', b, 'c'], transitions=transitions, initial='a', title='testmachinewithtimeouts') file "/home/sge/workspace_neon/ocpp16j_dev/_test_.py", line 61, in __init__ send_event=true) file "build/bdist.linux-i686/egg/transitions/core.py", line 364, in __init__ self.add_states(states) file "build/bdist.linux-i686/egg/transitions/extensions/nesting.py", line 264, in add_states new_states = self.traverse(states, *args, **kwargs) file "build/bdist.linux-i686/egg/transitions/extensions/nesting.py", line 183, in traverse ignore_invalid_triggers=ignore)) file "/home/sge/workspace_neon/ocpp16j_dev/_test_.py", line 78, in _create_state return timeoutstate(*args, **kwargs) file "/home/sge/workspace_neon/ocpp16j_dev/_test_.py", line 46, in __init__ super(timeoutstate, self).__init__(name=name, *args, **kwargs) typeerror: __init__() got unexpected keyword argument 'parent'
what's wrong on it? clue?
thank you!
there (at least) 3 issues example:
problem:
hierarchicalmachine
requires state of typetransitions.extensions.nesting.nestedstate
- solution: subclass correct state. easiest fix replace
from transitions import state
from transitions.extensions.nesting import nestedstate state
- solution: subclass correct state. easiest fix replace
problem: in
timeoutmachine.__init__
callsuper
base class instead of subclass. call__init__
method of base class ofmachine
if there any.- solution: change
super(machine, self).__init__
super(timeoutmachine, self).__init__
- solution: change
problem: transition defined set
transitions
supports arrays or dictionaries purpose.- solution: change
transitions = [{'dotimeout', 'b', 'c'}]
eithertransitions=[['dotimeout', 'b', 'c']]
ortransitions=[{'trigger':'dotimeout', 'source':'b', 'dest':'c'}]
- solution: change
and 1 remark: since sublclass timeoutmachine
, use timeoutstate
default state, not have manually create states. define states instead: states = ['a', 'c', {'name': 'b', 'timeout': 5, 'on_enter': 'set_timeout'}]
working code:
from transitions.extensions import hierarchicalmachine machine import time threading import thread transitions.extensions.nesting import nestedstate state class timeout(thread): def __init__(self, func, timeout): super(timeout, self).__init__() self.func = func self.timeout = timeout self.canceled = false print 'starting countdown' self.start() def run(self): time.sleep(self.timeout) print 'timeout occurred' if not self.canceled: self.func() class timeoutstate(state): def __init__(self, name, timeout=none, *args, **kwargs): self.timeout = timeout super(timeoutstate, self).__init__(name=name, *args, **kwargs) class timeoutmachine(machine): def __init__(self, model, states, transitions, initial, title): super(timeoutmachine, self).__init__(model=model, states=states, transitions=transitions, initial=initial, name=title, queued=true, auto_transitions=true, send_event=true) def _create_state(self, *args, **kwargs): return timeoutstate(*args, **kwargs) class model: def __init__(self): self.timer = none def set_timeout(self, event_data): timeout = event_data.kwargs.pop('timeout', 3) self.timer = timeout(self.dotimeout, timeout) model = model() transitions = [{'trigger': 'dotimeout', 'source':'b', 'dest':'c'}] states = ['a', 'c', {'name': 'b', 'timeout': 5, 'on_enter': 'set_timeout'}] machine = timeoutmachine(model=model, states=states, transitions=transitions, initial='a', title='testmachinewithtimeouts') model.to_b()
Comments
Post a Comment