Skip to main content

A flexible state machine for Python

Project description

## Installing

```python
pip install flexsm
```

## Importing, creating states and state machine

Creating states and state machines is pretty straightforward:

```python
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*:

```python
@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:

```python
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:

```python
@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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

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

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for flexsm-0.0.1-py2.7.egg
Algorithm Hash digest
SHA256 28510e3b1e7b117c55356dd76957509cb288681a6477637695d23a62c2036f1c
MD5 0154324db55679ad2bb636b64c0d8b64
BLAKE2b-256 67e6138b3cb926576a3b000c64533be6d422b3e4e50be9d4c0208c12e4cc2072

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