Code Monkey home page Code Monkey logo

statu's Introduction

statu - a python state machine

state machine for humans

Build Status

There are two types of developers in this world: those who love state machines and those who will eventually.

We fall in the first camp. We think it is really important to have a declarative way to define the states of an object. That’s why we are continuing development of statu.

Install

pip install statu

Basic Usage

@acts_as_state_machine
class Person():
    name = 'Billy'

    sleeping = State(initial=True)
    running = State()
    cleaning = State()

    run = Event(from_states=sleeping, to_state=running)
    cleanup = Event(from_states=running, to_state=cleaning)
    sleep = Event(from_states=(running, cleaning), to_state=sleeping)

    @before('sleep')
    def do_one_thing(self):
        print("{} is sleepy".format(self.name))

    @before('sleep')
    def do_another_thing(self):
        print("{} is REALLY sleepy".format(self.name))

    @after('sleep')
    def snore(self):
        print("Zzzzzzzzzzzz")

    @after('sleep')
    def big_snore(self):
        print("Zzzzzzzzzzzzzzzzzzzzzz")

person = Person()
print(person.current_state == Person.sleeping)      # True
print(person.is_sleeping)                           # True
print(person.is_running)                            # False
person.run()
print(person.is_running)                            # True
person.sleep()

# Billy is sleepy
# Billy is REALLY sleepy
# Zzzzzzzzzzzz
# Zzzzzzzzzzzzzzzzzzzzzz

print(person.is_sleeping)                           # True

Features

Before / After Callback Decorators

You can add callback hooks that get executed before or after an event (see example above).

Important: if the before event causes an exception or returns False, the state will not change (transition is blocked) and the after event will not be executed.

Blocks invalid state transitions

An InvalidStateTransition Exception will be thrown if you try to move into an invalid state.

ORM support

We have basic support for mongoengine, and sqlalchemy.

Mongoengine

Just have your object inherit from mongoengine.Document and state_machine will add a StringField for state.

Note: You must explicitly call #save to persist the document to the datastore.

@acts_as_state_machine
class Person(mongoengine.Document):
    name = mongoengine.StringField(default='Billy')

    sleeping = State(initial=True)
    running = State()
    cleaning = State()

    run = Event(from_states=sleeping, to_state=running)
    cleanup = Event(from_states=running, to_state=cleaning)
    sleep = Event(from_states=(running, cleaning), to_state=sleeping)

    @before('sleep')
    def do_one_thing(self):
        print("{} is sleepy".format(self.name))

    @before('sleep')
    def do_another_thing(self):
        print("{} is REALLY sleepy".format(self.name))

    @after('sleep')
    def snore(self):
        print("Zzzzzzzzzzzz")

    @after('sleep')
    def snore(self):
        print("Zzzzzzzzzzzzzzzzzzzzzz")


person = Person()
person.save()
eq_(person.current_state, Person.sleeping)
assert person.is_sleeping
assert not person.is_running
person.run()
assert person.is_running
person.sleep()
assert person.is_sleeping
person.run()
person.save()

person2 = Person.objects(id=person.id).first()
assert person2.is_running

Sqlalchemy

All you need to do is have sqlalchemy manage your object. For example:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
@acts_as_state_machine
class Puppy(Base):
   ...

Issues / Roadmap:

  • Allow multiple state_machines per object
  • Be able to configure the state field

Questions / Issues

Feel free to open an issue, PR, or shoot us an email at [email protected]

Thank you

to jtushman for getting this library off the ground, and letting us fork and continue the development here

and to aasm and ruby’s state\_machine and all other state machines that I loved before

statu's People

Contributors

artempyanykh avatar benvdhgss avatar daxtens avatar jtushman avatar julianpistorius avatar kageurufu avatar millerdev avatar philskaroulis avatar

Watchers

 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.