This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Python State Machines for Humans

Project Description

state machine for humans

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 state_machine

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 pypi@comanage.com

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

Release History

Release History

This version
History Node

0.3.1

History Node

0.3.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
statu-0.3.1.tar.gz (5.1 kB) Copy SHA256 Checksum SHA256 Source Aug 21, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting