Skip to main content

A flexible state machine for Python

Project description

Installing

pip install flexsm

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 python 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.4.tar.gz (3.8 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.4-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

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

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for flexsm-0.0.4.tar.gz
Algorithm Hash digest
SHA256 637c8ce48c6d551b79e3f81f8e4a187708f1adeb82533b856f2f6526b7a037d2
MD5 6b97abf6039f8f92df3aa6f0a8c3b153
BLAKE2b-256 8be443c99faaf0b65af623396aadf7d5f5fb350d6a20690ad1dfb9d786e777ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexsm-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3bf1a89b24d6c1c8839b389f76e4fb2ddb1d188fff17a4b3acb1ba14758a2c30
MD5 382055ab06159e7775bb8cecb4d7c8e9
BLAKE2b-256 304dd327ef77c71bfc9e86c4f408415b592269053795b868fd282d664e23d5ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexsm-0.0.4-py2-none-any.whl
Algorithm Hash digest
SHA256 4c838dba07edb86e268079170e38d0d037cb48c94619216e687711587a598b2e
MD5 6ae0cfa220827a75e10336b13e1cec67
BLAKE2b-256 2b64d0e1d7aabde47904555874fadad67f2884780e73f536bc93d5b5c1dfaa23

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