Skip to main content

Efficient Tiny State Machine using object callbacks.

Project description

MIT licensed PyPI version

python etsm

Tiny state machine for python, see etsm

Description

Implement a bare bones state machine in many languages. This library aim to be simple as possible and support only basic features:

  • states on object (owner)
  • optional enter/exit methods
  • virtual state user methods
  • is in
  • unrestricted transitions
  • no runtime allocation

Install

include this file into your project etsm.py
or
import it

import etsm

Example

Simple

import etsm

class Foo:
    def __init__(self):
        self.__sm = etsm.StateMachine(self)
        self.__a = etsm.State(Foo.EnterA, Foo.ExitA)
        self.__b = etsm.State(Foo.EnterB, None)

    def EnterA(self):
        print(' ->A ', end='')

    def ExitA(self):
        print(' A-> ', end='')

    def EnterB(self):
        print(' ->B ', end='')

    # no exit for B
    # def ExitB(self):
    #     print(' B-> ', end='')

    def Run(self):
        self.__sm.Transition(self.__a);
        self.__sm.Transition(self.__b);
        self.__sm.Transition(None);

foo = Foo()
foo.Run()

Output : " ->A A-> ->B " Sample: simple.py

Virtual State Methods

import etsm

class FooState(etsm.State):
    def __init__(self, enter, exit, tick):
        super().__init__(enter, exit)
        self.Tick = tick

class Foo:
    def __init__(self):
        self.__sm = etsm.StateMachine(self)
        self.__a = FooState(None, None, Foo.TickA)
        self.__b = FooState(None, None, Foo.TickB)

    def TickA(self):
        print(' A ', end='')

    def TickB(self):
        print(' B ', end='')

    # call tick on the current state, aka virtual call
    def Tick(self):
        if ( self.__sm.Current != None ):
            self.__sm.Current.Tick(self)

    def Run(self):
        self.Tick();
        self.__sm.Transition(self.__a);
        self.Tick();
        self.__sm.Transition(self.__b);
        self.Tick();
        self.__sm.Transition(None);
        self.Tick();

foo = Foo()
foo.Run()

Output: " A B " Sample: virtual_call.py

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

etsm-0.2.2.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

etsm-0.2.2-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file etsm-0.2.2.tar.gz.

File metadata

  • Download URL: etsm-0.2.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for etsm-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ed1415935587a1094942f7dd5edf04414513c4b154f359c4042b46ee791d2982
MD5 6aa18db0acf6907afe8e1cd50be22bf0
BLAKE2b-256 95510db8159c22c46feac28cb9d04a55e6ea1e4224608f836a0d6e297585d6b4

See more details on using hashes here.

File details

Details for the file etsm-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: etsm-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for etsm-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 34465646b51debd073ea72b1f29362620861e04f18cd8a436e2ff0af5ba8ee1b
MD5 acae8d7a5eb4ea867a84cdf6582f505a
BLAKE2b-256 f8b14aa280348e52ac603217892fbb871273a8257aa90ad6334b0c1e142c29c0

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