Package: simplesm
simplesmSimple state machine for python 3 that use dictionaries for creating states.Installationpip3installsimplesmDocumentsSimpleSMsubclasses would have:PropertyDescriptionstart_statemachine will start from this state.current_statemachine current stateprevious_statemachine previous statetest_actiontest action shows current value to be tested, it will be set before testing it against machine states, so it will always have the latest value tested even if machine does not accept that action.current_actionmachine current action, this value will be set after action being accepted by state, so it will always have the latest acceptable value tested.previous_actionjust the last acceptable action beforecurrent_action.is_acceptablereturnTrueif machine is in a final state, otherwiseFalse.statesmachine states! is a dict with keys as machine states, each state value is a dict too.Machinestatesdict values could have:PropertyDescriptiontransitionsa dict that keys are acceptable actions (that could be string or a function name) and values are dicts withstatethat defines next state andeventthat defines event name to call.eventthis event will be called before testing action against machine states.elseif none oftransitionsactions be acceptedelsewill be called.finalif exist and equalsTrueshows that this state is a final state (means acceptable) otherwise state is not acceptable.Machine first tries to match action string against current state transitions, if not found one, then tries to call each current state transition and if it returnTruethat transition will be accepted.MethodDescriptionperform(actions, do_events=True)get a string or list of strings asactionsand try to perform that actions on machine, ifdo_eventsbeFalse,events will not be called.reset()setcurrent_state,previous_state,current_action,previous_actionandtest_actiontoNone.accept(self, actions, do_events=False)get a string or list of strings asactionsand try to perform that actions on machine and returnTrueif machine stops in a final state, otherwiseFalse. this method does not change current values of machine and also by default does not callevents.Usage and Features WithExampleImportSimpleSMfromsimplesmand create a class:fromsimplesmimportSimpleSMclassTestMachine(SimpleSM):start_state="S0"states={"S0":{"transitions":{"0":{"state":"S1","event":"print"},"1":{"state":"S2","event":"print"}},"event":"welcome"},"S1":{"final":False,"transitions":{"1":{"state":"S0","event":"print"}},"event":"welcome"},"S2":{"final":True,"transitions":{"0":{"state":"S1","event":"print"},"2":{"state":"S3","event":"print"},"is_a_to_d":{"state":"S0","event":"print"}},"else":"s2_wrong","event":"welcome"},"S3":{"event":"goto_s2"}}defgoto_s2(self):print("we are just going to state S2")self.current_state="S2"defwelcome(self):print("we are in state{0}and get action{1}".format(self.current_state,self.test_action))defprint(self):print("going from{0}to{1}with{2}".format(self.previous_state,self.current_state,self.current_action))defs2_wrong(self):print("unacceptable input{0}".format(self.test_action))self.current_state="Error"defis_a_to_d(self):ifself.test_actionin"abcd":returnTruereturnFalsedefmain():m=TestMachine()m.perform(list("0110112a1d011"))print("current state is",m.current_state,"before calling accept")print("does machine accept 0110112a1d011 ?",m.accept(list("0110112a1d011")))print("current state is",m.current_state,"after calling accept")print("current state is acceptable ?",m.is_acceptable)if__name__=='__main__':main()output:we are in state S0 and get action 0
going from S0 to S1 with 0
we are in state S1 and get action 1
going from S1 to S0 with 1
we are in state S0 and get action 1
going from S0 to S2 with 1
we are in state S2 and get action 0
going from S2 to S1 with 0
we are in state S1 and get action 1
going from S1 to S0 with 1
we are in state S0 and get action 1
going from S0 to S2 with 1
we are in state S2 and get action 2
going from S2 to S3 with 2
we are just going to state S2
going from S2 to S0 with a
we are in state S0 and get action 1
going from S0 to S2 with 1
we are in state S2 and get action d
going from S2 to S0 with d
we are in state S0 and get action 0
going from S0 to S1 with 0
we are in state S1 and get action 1
going from S1 to S0 with 1
we are in state S0 and get action 1
going from S0 to S2 with 1
current state is S2 before calling accept
does machine accept 0110112a1d011 ? False
current state is S2 after calling accept
current state is acceptable ? True