Giter Site home page Giter Site logo

pysm's Introduction

pysm

state machine for humans

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

I fall in the first camp. I think it is really important to have a declarative way to define the states of an object. That’s why I developed pysm.

Install

pip install pysm

Basic Usage

Features

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.

@pysm.state_machine
class Person(mongoengine.Document):

    name = mongoengine.StringField(default='Billy')

    class Sleeping(pysm.State):

        initial = True

        def enter_state(self, from_state):
            pass

        def exit_state(self, to_state):
            pass

    class Running(pysm.State):

        def enter_state(self, from_state):
            pass

        def exit_state(self, to_state):
            pass

    class Cleaning(pysm.State):

        def enter_state(self, from_state):
            pass

        def exit_state(self, to_state):
            pass

    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)


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()

Sqlalchemy

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

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

Thank you

to aasm and ruby’s state_machine and jtushman's jtushman/state_machine and all other state machines that I loved before

pysm's People

Contributors

catstyle avatar

Stargazers

 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.