Skip to main content

Simple state machine for Python 3

Project description

simplesm

Simple state machine for python 3 that use dictionaries for creating states.

Installation

pip3 install simplesm

Documents

SimpleSM subclasses would have:

Property Description
start_state machine will start from this state.
current_state machine current state
previous_state machine previous state
test_action test 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_action machine current action, this value will be set after action being accepted by state, so it will always have the latest acceptable value tested.
previous_action just the last acceptable action before current_action.
is_acceptable return True if machine is in a final state, otherwise False.
states machine states! is a dict with keys as machine states, each state value is a dict too.

Machine states dict values could have:

Property Description
transitions a dict that keys are acceptable actions (that could be string or a function name) and values are dicts with state that defines next state and event that defines event name to call.
event this event will be called before testing action against machine states.
else if none of transitions actions be accepted else will be called.
final if exist and equals True shows 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 return True that transition will be accepted.

Method Description
perform(actions, do_events=True) get a string or list of strings as actions and try to perform that actions on machine, if do_events be False, events will not be called.
reset() set current_state, previous_state, current_action, previous_action and test_action to None.
accept(self, actions, do_events=False) get a string or list of strings as actions and try to perform that actions on machine and return True if machine stops in a final state, otherwise False. this method does not change current values of machine and also by default does not call events.

Usage and Features With Example

Import SimpleSM from simplesm and create a class:

from simplesm import SimpleSM


class TestMachine(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"}
    }

    def goto_s2(self):
        print("we are just going to state S2")
        self.current_state = "S2"

    def welcome(self):
        print("we are in state {0} and get action {1}".format(self.current_state, self.test_action))

    def print(self):
        print("going from {0} to {1} with {2}".format(self.previous_state, self.current_state, self.current_action))

    def s2_wrong(self):
        print("unacceptable input {0}".format(self.test_action))
        self.current_state = "Error"

    def is_a_to_d(self):
        if self.test_action in "abcd":
            return True
        return False


def main():
    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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplesm-0.1.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simplesm-0.1.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file simplesm-0.1.4.tar.gz.

File metadata

  • Download URL: simplesm-0.1.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for simplesm-0.1.4.tar.gz
Algorithm Hash digest
SHA256 05c4e124db07985e87db09eb0f9ac5cd47af60f6d43f3564b3703a6c5c456320
MD5 fed307316054e56f057a7a4ca0c8db12
BLAKE2b-256 0aa9014429cf281eb244d3224371be07aefa169a65106b07375ba6078f420ad5

See more details on using hashes here.

File details

Details for the file simplesm-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: simplesm-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for simplesm-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 29d1256b901fa753c36d46caf02e0511ba8ac8b6e0780ca9824f39a2cdcfa69c
MD5 a3e6b63e1bc4b2f6fd79776bc5734f52
BLAKE2b-256 0d3ac7eebf60ae3dd7a5f8a76d6b0686ab32b623c7a5155e564804f8cf61a2c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page