Skip to main content

A flexible state machine for Python

Project description

Installing

pip install flexsm

You can easily install flexsm with pip:

Importing, creating states and state machine

Creating states and state machines is pretty straightforward:

from flexsm import *

root = State("root")
state1 = State("state 1")
state2 = State("state 2")

sm = StateMachine(root)

Transitions and input variables

Transitions are responsible for getting from one state to another. They usually start in one state and end in another. If the next state is only known at runtime, e.g. if it depends on an input variable “x”, then you can override the transition method getNextState:

@addTransition(state=root)
class WaitForSomeValueToBecomeSmall(Transition):
    def getNextState(self, x):
        if x>15:
            return state2
        else:
            return state1

This piece of code allows us to transition from state root to state 1 if x is smaller than 15, for example by calling:

sm.update("x", 10)

or to state 2 if x is 15 or bigger.

There is also an input variable time_in_state, which contains the amount of time we’ve been in the current state in seconds. The minimal guaranteed resolution for time_in_state is 0.1 seconds, which can be changed in the StateMachine construction:

sm = StateMachine(root, time_resolution=0.01)

By overriding the check method, we can transition if we are in the state 5 seconds or longer:

@addTransition(state=state1, next=state2)
class WaitAMoment(Transition):
    def check(self, time_in_state, x):
        return time_in_state > 5

    def onTrigger(self, time_in_state, x):
        print("""We are in this boring
            state since {:.2f} seconds,
            with x being {}""".format(time_in_state, x))

We also override onTrigger, which is called when the transition is triggered. Note how the parameters for onTrigger and check are equal. The parameters for all transition methods are name sensitive. So you can’t simply use the parameter y instead of x and expect y to be 100 if you run sm.update(“x”, 100). For the same transition, the parameters for getNextState, check and onTrigger even have to be equal!

Transition.check will only be called if the value of one of its parameters changed. Thus, if your code in the check method takes a lot of time, try to avoid frequently changing parameters like time_in_state.

Parent states

Consider the following example:

airbourne = State("Airbourne")
doing360spin = State("Spin 360", parent=airbourne)

sm = StateMachine(doing360spin)

In this case, we are not only in the state doing360spin, but also in the state airbourne. Thus, any transitions defined on airbourne will be considered as well. For example, an airplane could go into an emergency state if its fuel is getting low. Such emergency transitions would be interesting for all states in the air.

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

flexsm-0.0.13.tar.gz (3.9 kB view details)

Uploaded Source

Built Distributions

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

flexsm-0.0.13-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

flexsm-0.0.13-py2.7.egg (6.7 kB view details)

Uploaded Egg

flexsm-0.0.13-py2-none-any.whl (3.9 kB view details)

Uploaded Python 2

File details

Details for the file flexsm-0.0.13.tar.gz.

File metadata

  • Download URL: flexsm-0.0.13.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flexsm-0.0.13.tar.gz
Algorithm Hash digest
SHA256 c597bf6e00ccaf323f7fb3fd0610d60ac2bdfeda5db33b311e6700eb5ded85d2
MD5 67ea58c274fe1e40d6cede3f6567790a
BLAKE2b-256 69d5a3d302fce2af113adebc905d68b55a11617163768b30f31b958b95fa1f41

See more details on using hashes here.

File details

Details for the file flexsm-0.0.13-py3-none-any.whl.

File metadata

File hashes

Hashes for flexsm-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 c76fe3d41cee2c894856617716a8cd2b5c36a4ba5d6882434cc632ad0622294f
MD5 1e0c71246ac19e62304c54f89740a505
BLAKE2b-256 c31ec574117e2240ac1ff2f1372dce7496cadcb3d0bedac2ffb4792c89eb1b70

See more details on using hashes here.

File details

Details for the file flexsm-0.0.13-py2.7.egg.

File metadata

  • Download URL: flexsm-0.0.13-py2.7.egg
  • Upload date:
  • Size: 6.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flexsm-0.0.13-py2.7.egg
Algorithm Hash digest
SHA256 3e9a96135ed1c0a7672b3a0a407894486637f0737ac7c2fc902ed049b7a6dc9d
MD5 3b681fd8c9b791d80304b59b537e7932
BLAKE2b-256 cc01b483f04994eca759756a887f3f9bd52b824478e462e2b968b33e97dea3cf

See more details on using hashes here.

File details

Details for the file flexsm-0.0.13-py2-none-any.whl.

File metadata

File hashes

Hashes for flexsm-0.0.13-py2-none-any.whl
Algorithm Hash digest
SHA256 c8a862ca63a21b184d8bf3dd947e9c3d8c2db16b5f5b432be7bd2416b15ed81e
MD5 0751b9a882512737c644b37ff4275768
BLAKE2b-256 7713de95d2034780b27419c21dab06bde9f09b668c8933948a07f7cc07be3184

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