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.

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.2.tar.gz (3.5 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.2-py3.5.egg (6.5 kB view details)

Uploaded Egg

flexsm-0.0.2-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

flexsm-0.0.2-py2.7.egg (6.4 kB view details)

Uploaded Egg

flexsm-0.0.2-py2-none-any.whl (3.7 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for flexsm-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f598d720979bc7ceb71fbc20dd864dc188dc93ed05d10bc3cb7c019745943435
MD5 2a750c3249f6f478c87a86ce4c4792ec
BLAKE2b-256 3740ee9683333ba3ce12c9a9413782308c7cc46e3da5fbeb51b65cf47c6cc41c

See more details on using hashes here.

File details

Details for the file flexsm-0.0.2-py3.5.egg.

File metadata

  • Download URL: flexsm-0.0.2-py3.5.egg
  • Upload date:
  • Size: 6.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for flexsm-0.0.2-py3.5.egg
Algorithm Hash digest
SHA256 5dc430d2cd094f233a51520b6aa8f5139995eb56a9004d97d2f48ca892e1b09a
MD5 990372558d3d6ce445bf2735123bdb14
BLAKE2b-256 57336477b3b3bf08dd44f75de365bcdcc24e1119ea778d009f21e5cd1837fe05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexsm-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d6b8b616749ebf6ba7699f814f62a4d182de8fc408bd8a79f93c65a973251c9f
MD5 c74d31d0f4cfd6d14917ea9f2c0b1a7a
BLAKE2b-256 f1d3e390c6c113f5644c5034a0cff62692bd860a086d21d12d170df9d264c87b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for flexsm-0.0.2-py2.7.egg
Algorithm Hash digest
SHA256 ef02ecf58d47f0aba58130165bff86a44eab98258986873de73fb8e13d667fe8
MD5 4f784041a024e824d55127419a56e139
BLAKE2b-256 c498dde759f806f433b57ab83d03e4942d29eb2bf23589231c46a6a0ac2cc2ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexsm-0.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 959d961651463d7d8ed9d6805ca4bfcb474d231e1d5eaeb4d6b2aeb517a77a17
MD5 b4a53274693a2f9d2ede7e5c4346f299
BLAKE2b-256 4717943b87059c83555332995cf6f62b7adc948a18dd4910d38633445677cbd1

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