[Twisted-Python] automat question

Chris Withers chris at withers.org
Tue Mar 16 02:09:09 MDT 2021


On 15/03/2021 09:34, Glyph wrote:
> 
> 
>> Right, but as best I can tell, outputs in automat have to be defined 
>> as part of the state machine class, I need targets only available 
>> after class instantiation to be notified.
> 
> Outputs are just methods, so it sort of depends what you mean by "after 
> class instantiation".  You can call whatever you want /in/ an output.

Right, but then I'd have to maintain my own list of subscribers and 
figure out a way to duplicate in that logic in each of the output 
methods defined on the machine.

>>> Again... have outputs.  I think there's something you're leaving out 
>>> about how you want to have some /generalized/ output, but without 
>>> knowing a bit more it's hard to say how it could help more :).
>>
>> I think it's what I said above. TBH, I'm probably going to end up 
>> writing "yet another state machine class" that does just what I need, 
>> but I think that's okay - not everything has to work for every 
>> situation :-)
> 
> It sounds like what you /might/ want here is soemthing for constructing 
> arbitrary machines at runtime, 

The states and transitions are very rigid.

> and automat is all about enforcing that 
> your machine is static so that (for example) it can be visualized 
> statically.  So yeah in that case, different library.

Take this example:

class LightSwitch(object):
     _machine = MethodicalMachine()

     @_machine.state(serialized="on")
     def on_state(self):
         "the switch is on"

     @_machine.state(serialized="off", initial=True)
     def off_state(self):
         "the switch is off"

     @_machine.input()
     def flip(self):
         "flip the switch"

     on_state.upon(flip, enter=off_state, outputs=[])
     off_state.upon(flip, enter=on_state, outputs=[])


What I'm looking to do is something along the lines of:

aSwitch = LightSwitch()
aSwitch.flip.addListener(myCallable)

myCallable might well be a deferred that something else is waiting on...

Chris



More information about the Twisted-Python mailing list