Skip to main content

A library implementing workflows (or state machines) for Python projects.

Project description

XWorkflows is a library to add workflows, or state machines, to Python objects.

It has been fully tested with all versions of Python from 2.6 to 3.3

Example

It allows to easilly define a workflow, attach it to a class, and use its transitions:

class MyWorkflow(xworkflows.Workflow):
    # A list of state names
    states = (
        ('foo', _(u"Foo")),
        ('bar', _(u"Bar")),
        ('baz', _(u"Baz")),
    )
    # A list of transition definitions; items are (name, source states, target).
    transitions = (
        ('foobar', 'foo', 'bar'),
        ('gobaz', ('foo', 'bar'), 'baz'),
        ('bazbar', 'baz', 'bar'),
    )
    initial_state = 'foo'


class MyObject(xworkflows.WorkflowEnabled):
    state = MyWorkflow()

    @transition()
    def foobar(self):
        return 42

    # It is possible to use another method for a given transition.
    @transition('gobaz')
    def blah(self):
        return 13

>>> o = MyObject()
>>> o.state
State('foo')
>>> o.state.is_foo
True

>>> o.foobar()
42
>>> o.state
State('bar')

>>> o.blah()
13
>>> o.state
State('baz')

Hooks

Custom functions can be hooked to transactions, in order to run before/after a transition, when entering a state, when leaving a state, …:

class MyObject(xworkflows.WorkflowEnabled):

    state = MyWorkflow()

    @xworkflows.before_transition('foobar')
    def my_hook(self, *args, **kwargs):
        # *args and **kwargs are those passed to MyObject.foobar(...)
        pass

    @xworkflows.on_enter_state('bar')
    def my_other_hook(self, result, *args, **kwargs):
        # Will be called just after any transition entering 'bar'
        # result is the value returned by that transition
        # *args, **kwargs are the arguments/keyword arguments passed to the
        # transition.
        pass

Project details


Download files

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

Source Distribution

xworkflows-1.0.2.tar.gz (33.8 kB view details)

Uploaded Source

File details

Details for the file xworkflows-1.0.2.tar.gz.

File metadata

  • Download URL: xworkflows-1.0.2.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xworkflows-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fca9880c7b3861a1716c3de93dd7a4c0a552da9c215e23e29d0af1a753376209
MD5 53f182f8e18cd997f86d44744dadc51f
BLAKE2b-256 64fc392e85d3b4df8ab9d94b2f48139693fcd415d029d46b2ad854e833dd20f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page