Skip to main content

This is a python library supporting statechart (in scxml format) originated by David Harel. Features like hierarchical states, parallel states, and history are ready for you to command. You won’t find another lib as easy and flexible to use as SCM.

Just take a look and run scm_tutorial.py please. The code is listed here:

from scm import StateMachineManager

client_scxml = """
<scxml>
    <state id='appear'>
        <transition event='born' ontransit='say_hello' target='live'/>
    </state>
    <parallel id='live'>
            <transition event='hp_zero' target='dead'/>
            <state id='eat'>
            </state>
            <state id='move'>
            </state>
    </parallel>
    <final id='dead'/>
    </scxml>
"""

class Life:
    def __init__(self):
        self.mach_ = StateMachineManager.instance().getMach('the life')
        self.mach_.set_do_exit_state_on_destroy(True)
        self.mach_.register_state_slot("appear", self.onentry_appear, self.onexit_appear)
        self.mach_.register_state_slot("live", self.onentry_live, self.onexit_live)
        self.mach_.register_state_slot("eat", self.onentry_eat, self.onexit_eat)
        self.mach_.register_state_slot("move", self.onentry_move, self.onexit_move)
        self.mach_.register_state_slot("dead", self.onentry_dead, self.onexit_dead)
        self.mach_.register_action_slot('say_hello', self.say_hello)
        self.mach_.StartEngine()

    def test(self):
        self.mach_.enqueEvent("born")
        #self.mach_.frame_move(0) # state change to 'live'
        StateMachineManager.instance().pumpMachEvents()
        self.mach_.enqueEvent("hp_zero")
        #self.mach_.frame_move(0) # state change to 'dead'
        StateMachineManager.instance().pumpMachEvents()

    def onentry_appear(self):
        print("come to exist")

    def onexit_appear(self):
        print("we are going to...")

    def onentry_live(self):
        print("start living")

    def onexit_live(self):
        print("no longer live")

    def onentry_eat(self):
        print("start eating")

    def onexit_eat(self):
        print("stop eating")

    def onentry_move(self):
        print("start moving")

    def onexit_move(self):
        print("stop moving")

    def onentry_dead(self):
        print("end")

    def onexit_dead(self):
        assert (0 and "should not exit final state");
        print("no, this won't get called.")

    def say_hello(self):
        print("\n*** Hello, World! ***\n")

if __name__ == '__main__':
    StateMachineManager.instance().set_scxml("the life", client_scxml)
    life = Life()
    life.test()
    StateMachineManager.instance().pumpMachEvents()

The output you should see

come to exist
we are going to...

*** Hello, World! ***

start living
start eating
start moving
stop eating
stop moving
no longer live
end

Simply

  1. you load the scxml from external file or from a string defined in your code.

  2. you connect these onentry_ onexit_, etc. slots

  3. you start the engine, call the framemove in main loop.

Done.

It’s that easy!

Read the tutorials at:

Metadata

Release files for py-scm 1.0.5

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

Source distribution (sdist)

Source distribution for py-scm 1.0.5
File Size Uploaded
py_scm-1.0.5.tar.gz 16.4 kB Details

Release files / py_scm-1.0.5.tar.gz

Download URL py_scm-1.0.5.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bd7c8a8e96b44f3124e02704bcab7b16aed32241bef4a8c15d7fea2708b8f856
BLAKE2b-256 checksum
How to use checksums
1bf929b3707b49e747515bf30d799e440882858a5b69cab5195893305470d4d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

1.0.5 This release

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

2 release files

1.0.0

2 release files

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