Skip to main content

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.

Metadata

Release files for flexsm 0.1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flexsm 0.1.0.0
File Size Uploaded
flexsm-0.1.0.0.tar.gz 4.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for flexsm 0.1.0.0
File Interpreter ABI Platform
flexsm-0.1.0.0-py3-none-any.whl Python 3 none any Details
flexsm-0.1.0.0-py2-none-any.whl Python 2 none any Details

Total release size: 11.9 kB

Release files / flexsm-0.1.0.0.tar.gz

Download URL flexsm-0.1.0.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4a9abefaef33b9b23e635ecdb70287e232d1f80ef9615f9df528f627e313dc5e
BLAKE2b-256 checksum
How to use checksums
0610f9408144cb214eb52a56392766835b3ead4502c2ca10687b7570c654d832
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / flexsm-0.1.0.0-py3-none-any.whl

Download URL flexsm-0.1.0.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e39a146209e77ac4ea3ddbe526c019591e5b75c7383505afadf585f82e1e23d
BLAKE2b-256 checksum
How to use checksums
38eba9545ac162da30452d1a97a966dae5e627b8df9f40d0a95b2baf60b1c68e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / flexsm-0.1.0.0-py2-none-any.whl

Download URL flexsm-0.1.0.0-py2-none-any.whl
Size 4.0 kB
Tags Python 2
SHA-256 checksum
How to use checksums
10567ab9719402d5a4820dc59b2bfd2554a0fb449251377c81ef51b289ea5634
BLAKE2b-256 checksum
How to use checksums
44605ee2496fc1b7bc94adf18ef6fca90cbd2fe58b1a980d250b9421da8e237a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0.0 This release

3 release files

0.0.13

4 release files

0.0.12

3 release files

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

3 release files

0.0.3

3 release files

0.0.2

5 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page