-
from transitions import Machine
class TempMachine(Machine):
def __init__(self):
self.temp = 0
states = ['centigrade', 'fahrenheit', 'kelvin']
Machine.__init__(self, states=states, send_event=True, initial=states[0],
ignore_invalid_triggers=True, auto_transitions=False)
self.add_transition(trigger='to_centigrade', source='*', dest='centigrade', before='set_temp',
after='calc_temp')
def set_temp(self, event):
self.temp = event.kwargs.get('temp', 0)
def calc_temp(self, event):
temp_map = {}
if event.transition.dest == 'centigrade':
temp_map = {
'centigrade': '{} ℃'.format(self.temp),
'fahrenheit': '{} ℉'.format(self.temp * 1.8 + 32),
'kelvin': '{} K'.format(self.temp + 273.15)
}
print(temp_map)
return temp_map
machine = TempMachine()
machine.to_centigrade(temp=25) It's it possible to get the |
Beta Was this translation helpful? Give feedback.
Answered by
aleneum
Mar 21, 2023
Replies: 1 comment
-
Hello @vba34520, callback return values are ignored (unless they are conditions, in this case they must return a 'truesy' or 'falsy' value). from transitions import Machine
class TempMachine(Machine):
def __init__(self):
self.temp = 0
states = ['centigrade', 'fahrenheit', 'kelvin']
Machine.__init__(self, states=states, send_event=True, initial=states[0],
ignore_invalid_triggers=True, auto_transitions=False)
self.add_transition(trigger='to_centigrade', source='*', dest='centigrade', before='set_temp',
after='calc_temp')
def set_temp(self, event):
self.temp = event.kwargs.get('temp', 0)
def calc_temp(self, event):
referenced_map = event.kwargs.get("temp_map")
if event.transition.dest == 'centigrade':
referenced_map.update({
'centigrade': '{} ℃'.format(self.temp),
'fahrenheit': '{} ℉'.format(self.temp * 1.8 + 32),
'kelvin': '{} K'.format(self.temp + 273.15)
})
temp_map = {}
machine = TempMachine()
machine.to_centigrade(temp_map=temp_map, temp=25)
assert temp_map["fahrenheit"] == "77.0 ℉" |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
vba34520
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @vba34520,
callback return values are ignored (unless they are conditions, in this case they must return a 'truesy' or 'falsy' value).
You cannot directly return values from callbacks.
You can either assign variables to your stateful object (the model; in your case also the machine; e.g.
self.temp_map
) or pass a referenced object as a parameter: