Skip to main content

dsm

Damn simple finite state machine

Build Status

About

DSM is a observable simple finite state machine implementation for Python. Transitions may be programmed declaratively or imperatively. Inputs and state changes are emitting observable events.

Requirements

  • Python 2.7, 3.5, 3.6
  • observable
  • six for compatibility between Python 2 and Python 3

Installation

pip install dsm

Usage

Django integration

It is possible to integrate dsm with Django models by declaring a StateMachineField.

from django.db import models
from dsm.fields import StateMachineField


class Order(models.Model):
    status = StateMachineField(
        transitions=(
            ('new', ['confirmed'], 'processing'),
            ('processing', ['cancel'], 'cancelled'),
            ('processing', ['send'], 'sending'),
            ('sending', ['deliver'], 'finished'),
        ),
        max_length=16,
        choices=(
            ('new', _('New')),
            ('processing', _('Processing')),
            ('sending', _('Sending')),
            ('finished', _('Finished')),
            ('canceled', _('Cancelled')),
        ),
        db_index=True,
        default='new'
    )

Now you can create an Order and check it's status:

>>> order = Order.objects.create()
>>> order.status
new
>>> type(order.status)
dsm.fields.MachineState

The string representation of status field is same as state name provided in transitions declaration, but internally there is always dsm.fields.MachineState instance.

Declarative

FSM declaration:

import string
import dsm

class SumatorMachine(dsm.StateMachine):
    class Meta:
        initial = 'init'
        transitions = (
            ('init', list(string.digits), 'digit_enter'),
            ('digit_enter', list(string.digits), 'digit_enter'),
            ('digit_enter', '=', 'summarize'),
        )

Usage:

Initialization:

fsm = SumatorMachine()

Processing one value:

fsm.process(value)

Processing multiple values:

fsm.process_many(iterable)

Gathering the current state:

>>> fsm.state
'summarize'

Resetting to the intial state:

fsm.reset()

Listening on events:

fsm.when('state', func)

Events example:

>>> the_sum = 0

>>> def add_digit(x): global the_sum; the_sum += int(x)
>>> def reset(x): global the_sum; the_sum = 0

>>> fsm = SumatorMachine()
>>> fsm.when('digit_enter', add_digit)
>>> fsm.when('init', reset)

>>> fsm.process_many('666=')
'summarize'

>>> the_sum
18

Events example (class based):

>>> class Sumator(object):
...     def __init__(self):
...         self.total = 0
...         self.fsm = SumatorMachine()
...         self.fsm.when('digit_enter', self.add)
...         self.fsm.when('init', self.reset)
...
...     def add(self, x):
...         self.total += int(x)
...
...     def reset(self, x):
...         self.total = 0
...
...     def summarize(self, values):
...         self.fsm.reset()
...         self.fsm.process_many(values+'=')
...         return self.total

>>> s = Sumator()
>>> s.summarize('666')
18

Imperative

import string
import dsm

fsm = dsm.StateMachine(
        initial='init',
        transitions=dsm.Transitions((
                ('init', list(string.digits), 'digit_enter'),
                ('digit_enter', list(string.digits), 'digit_enter'),
                ('digit_enter', '=', 'summarize'),
            ))
        )

License

BSD

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dsm-0.5.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

dsm-0.5.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file dsm-0.5.3.tar.gz.

File metadata

  • Download URL: dsm-0.5.3.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.1

File hashes

Hashes for dsm-0.5.3.tar.gz
Algorithm Hash digest
SHA256 817292dd9ce55f552ccffa6e9dfb63892978e4118bafd9b748c182ab13e45342
MD5 0def5536122adf92eb5c3c23b477c656
BLAKE2b-256 8bbf258211755bf809441635bacd7068a603fb9edfe581cefab36e1e87f1dd28

See more details on using hashes here.

File details

Details for the file dsm-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: dsm-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.1

File hashes

Hashes for dsm-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 72a7d82b8b9f9aec8e97e3da7d86749e4a56df7e5626164ce26752420e4939a4
MD5 2f938f88290c4965ff3462050c23a7d6
BLAKE2b-256 855957e1609b3b697183d5659c7f46564d0fae65d3d11d723c8dedb516f5bd68

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5.3 This release

2 files

0.5.2

2 files

0.5.1

2 files

0.5

2 files

0.4

3 files

0.3

3 files

0.2

1 file

0.1

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page