本文整理汇总了Python中transitions.Machine.set_state方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.set_state方法的具体用法?Python Machine.set_state怎么用?Python Machine.set_state使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transitions.Machine
的用法示例。
在下文中一共展示了Machine.set_state方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _ClientConnection
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import set_state [as 别名]
class _ClientConnection(object):
states = [
'init',
'auth_request',
'auth_response',
'end'
]
def __init__(self):
self._buffer = b""
self.machine = Machine(
model=self, states=self.states, initial='init')
def initiate_connection(self):
self.machine.set_state("auth_request")
def recv(self, data):
if self.state != "auth_response":
raise ProtocolError("ClientConnection.recv: Incorrect state {}".format(self.state))
self._buffer += data
current_event = read_auth_response(self._buffer)
if current_event == 'NeedMoreData':
return current_event
else:
self._buffer = b""
if self.state == 'auth_response':
self.machine.set_state('end')
return current_event
def send(self, event):
if self.state != "auth_request":
raise ProtocolError("ClientConnection.send: Incorrect state {}".format(self.state))
if self.state == "auth_request" and event != "AuthRequest":
raise ProtocolError("ClientConnection.send: Incorrect event {0} in state: {1}".format(event, self.state))
if self.state == "auth_request":
self.machine.set_state("auth_response")
return write_auth_request(event)
示例2: _ServerConnection
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import set_state [as 别名]
class _ServerConnection(object):
states = [
'init',
'greeting_request',
'greeting_response',
'auth_inprogress',
'request',
'response',
'end'
]
def __init__(self):
self._buffer = b""
self.machine = Machine(
model=self, states=self.states, initial='init')
self._version = 0xff
self._auth_methods = [AUTH_TYPE["NO_SUPPORT_AUTH_METHOD"]]
self._addr_type = 0xff
self._addr = 0
self._port = 0
def initiate_connection(self):
self.machine.set_state("greeting_request")
def auth_end(self):
if self.state != "auth_inprogress":
raise ProtocolError("ServerConnection.auth_end: Incorrect state {}".format(self.state))
self.machine.set_state("request")
def recv(self, data):
if self.state not in ("greeting_request", "request"):
raise ProtocolError("ServerConnection.recv: Incorrect state {}".format(self.state))
self._buffer += data
_reader = getattr(reader, "read_" + self.state)
current_event = _reader(self._buffer)
if current_event == "NeedMoreData":
return current_event
else:
self._buffer = b""
if self.state == 'greeting_request':
if current_event == "GreetingRequest":
self._version = 5
self._auth_methods.extend(current_event.methods)
elif current_event == "Socks4Request":
self._version = 4
self._port = current_event.port
self.machine.set_state('greeting_response')
elif self.state == 'request':
if current_event == "Request":
self._addr_type = current_event.atyp
self._addr = current_event.addr
self._port = current_event.port
self.machine.set_state('response')
self._buffer = b""
return current_event
def send(self, event):
if self.state not in ("greeting_response", "response"):
raise ProtocolError("ServerConnection.recv: Incorrect state {}".format(self.state))
if self.state == "greeting_response" and (event != "GreetingResponse" and event != "Socks4Response"):
raise ProtocolError("ServerConnection.send: Incorrect event {0} in state: {1}".format(event, self.state))
if self.state == "response" and event != "Response":
raise ProtocolError("ServerConnection.send: Incorrect event {0} in state: {1}".format(event, self.state))
_writer = getattr(writer, "write_" + self.state)
if self.state == "greeting_response":
if event == "GreetingResponse":
if (self._version != 5 or
event.auth_type not in self._auth_methods):
raise ProtocolError("ServerConnection.send: incorrect event from user.")
if event.auth_type == AUTH_TYPE["NO_AUTH"]:
self.machine.set_state("request")
elif event.auth_type == AUTH_TYPE["NO_SUPPORT_AUTH_METHOD"]:
self.machine.set_state("end")
else:
self.machine.set_state("auth_inprogress")
elif event == "Socks4Response":
if self._version != 4 or self._port != event.port:
raise ProtocolError("ServerConnection.send: incorrect event from user")
self.machine.set_state("end")
if self.state == "response":
if (self._version != 5 or
self._addr_type != event.atyp or
self._addr != event.addr or
self._port != event.port):
raise ProtocolError("ServerConnection.send: receive incorrect data from server")
#.........这里部分代码省略.........
示例3: Matter
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import set_state [as 别名]
from transitions import Machine, State
# Our old Matter class, now with a couple of new methods we
# can trigger when entering or exit states.
class Matter(object):
def say_hello(self):
print("hello, new state!")
def say_goodbye(self):
print("goodbye, old state!")
lump = Matter()
# Same states as above, but now we give StateA an exit callback
states = [
State(name='solid', on_exit=['say_goodbye']),
'liquid',
#State(name='gas', on_enter=['say_hello']),
{ 'name': 'gas' }
]
machine = Machine(lump, states=states)
machine.add_transition('sublimate', 'solid', 'gas')
# Callbacks can also be added after initialization using
# the dynamically added on_enter_ and on_exit_ methods.
# Note that the initial call to add the callback is made
# on the Machine and not on the model.
machine.on_enter_gas('say_hello')
# Test out the callbacks...
machine.set_state('solid')
lump.sublimate()
示例4: Matter
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import set_state [as 别名]
from transitions import Machine
class Matter(object):
def __init__(self): self.set_environment()
def set_environment(self, temp=0, pressure=101.325):
self.temp = temp
self.pressure = pressure
def print_temperature(self): print("Current temperature is %d degrees celsius." % self.temp)
def print_pressure(self): print("Current pressure is %.2f kPa." % self.pressure)
lump = Matter()
machine = Machine(lump, ['solid', 'liquid'], initial='solid')
machine.add_transition('melt', 'solid', 'liquid', before='set_environment')
lump.melt(45) # positional arg;
# equivalent to lump.trigger('melt', 45)
lump.print_temperature()
machine.set_state('solid') # reset state so we can melt again
lump.melt(pressure=300.23) # keyword args also work
lump.print_pressure()