GithubHelp home page GithubHelp logo

triplekill / pystatemachine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cmaugg/pystatemachine

0.0 2.0 0.0 168 KB

A versatile, yet easy-to-use finite-state machine library written in python.

License: MIT License

Python 100.00%

pystatemachine's Introduction

pystatemachine

pystatemachine is a versatile, yet easy-to-use finite-state machine library written in python. It provides functions to turn any python object into a finite-state automaton which changes from one State to another when initiated by a triggering event.

Usage

A finite-state machine is defined by a list of its states, and the triggering condition for each transition. pystatemachine offers an event decorator for a classes' bound methods, a State class to define the finite-state machine's states, and a acts_as_state_machine decorator for turning any python (new- or old-style) class into a finite-state machine. By default, any event-decorated method may raise errors. Optionally, a transition_failure_handler decorator turns any class method into a failure handler which gets invoked when an event-decorated method raises an error.

Example

Following, a turnstile is modeled.

An example of a very simple mechanism that can be modeled by a state machine is a turnstile. A turnstile is a gate with three rotating arms at waist height, one across the entryway. Initially the arms are locked, barring the entry, preventing customers from passing through. Depositing a coin or token in a slot on the turnstile unlocks the arms, allowing a single customer to push through. After the customer passes through, the arms are locked again until another coin is inserted.

@acts_as_state_machine
class Turnstile(object):
    locked = State('locked', initial=True)
    unlocked = State('unlocked')

    @event(from_states=(locked, unlocked), to_state=unlocked)
    def coin(self):
        assert random.random() > .5, 'failing for demonstration purposes, only ..'
        print('*blingbling* .. unlocked!')

    @event(from_states=(locked, unlocked), to_state=locked)
    def push(self):
        print('*push* .. locked!')

    @transition_failure_handler(calling_sequence=2)
    def turnstile_malfunction(self, method, from_state, to_state, error):
        print('state transition from {0.name} to {1.name} failed. Reason: {2}'.format(from_state, to_state, error))

    @transition_failure_handler(calling_sequence=1)
    def before_turnstile_malfunction(self, method, from_state, to_state, error):
        print('before state transition failure handler ..')


import random

turnstile = Turnstile()
for _ in range(10):
    handler = random.choice([turnstile.coin, turnstile.push])
    handler()

Changelog

1.2

  • exceptions in an event-decorated function are now reraised when no transition failure handler was registered

1.1

  • added a decorator for registering a class' method as exception handler when an 'event'-decorated method fails. multiple methods may be registered as transition failure handler: they are invoked in the order given by the optional 'calling_sequence' keyword

1.0

  • first public release

License

pystatemachine is available under MIT License.

Download

You can download pystatemachine.py.

Alternatively:

git clone [email protected]:cmaugg/pystatemachine

pystatemachine's People

Contributors

cmaugg avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.