Skip to main content

Create state machines with a visually pleasing syntax

Project description

Levee

Create state machines with a visually pleasing syntax in Python

Remember, keep your Levee DRY!

Example Usage

For more examples of what you can do, read the tests

from levee import Chart, State, Condition, Effect

class FlowChart(Chart):
    class ALPHA(State): pass
    class BETA(State): pass
    class GAMMA(State): pass

    class Sometimes(Condition):

        def eval(self, sometimes):
            return sometimes
    
    class Maybe(Condition):

        def eval(self, maybe):
            # Return a string to block the transition with a reason
            return maybe if maybe else 'I guess not'

    class PrintWhatever(Effect):

        def exec(self, whatever):
            # Parameter names in the definition of Condition.eval/Effect.exec will become
            # required named arguments for Chart.to/Chart.can/Chart.choices
            print(whatever)
    
    class SayHello(Effect):
        
        def exec(self):
            print('Hello!')

    chart = {
        ALPHA: {
            BETA (Sometimes) [PrintWhatever]: ...,
            GAMMA (Sometimes | Maybe) [SayHello]: ...,
        },
        BETA: {
            ALPHA (Sometimes & Maybe) [PrintWhatever + PrintWhatever]: ...,
        },
        GAMMA: {
            ALPHA (~Maybe) [PrintWhatever + SayHello]: ...,
        }
    }

def main():
    data = { 'state': None }
    chart = FlowChart(data)
    print(chart.state, chart.choices(sometimes=True, maybe=False, whatever='whatever'))
    chart.to(FlowChart.BETA, sometimes=True, whatever='Whatever Once')
    chart.to(FlowChart.ALPHA, sometimes=True, maybe=True, whatever='Whatever Twice')
    if chart.can(FlowChart.GAMMA, sometimes=False, maybe=True):
      chart.to(FlowChart.GAMMA, sometimes=False, maybe=True)
    chart.to(FlowChart.ALPHA, maybe=False, whatever='Goodbye!')

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

levee-1.0.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

levee-1.0.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file levee-1.0.0.tar.gz.

File metadata

  • Download URL: levee-1.0.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.6

File hashes

Hashes for levee-1.0.0.tar.gz
Algorithm Hash digest
SHA256 46f291a402a256fefc02f5c8d4136c8b5f23ed75024a4cf19b51ba21bf6c19d5
MD5 2e50660ab07d30ae5fd4c9eced974cf3
BLAKE2b-256 133081bd75708f82d5ec697a3749ee45f1fe1214c57a9d76f0d3fdbb8e7b3a45

See more details on using hashes here.

File details

Details for the file levee-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: levee-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.6

File hashes

Hashes for levee-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd5ada8c4fa48c8ee7db2af98cb3fd8734d87d6190e3000680072a9fd6490ba5
MD5 fdc8b446a25ed05b2fadcd399dcbf9f8
BLAKE2b-256 a9d70a80dbc6c1a2bbfda314bc97c99be10d87b2394faa4b8df3c3fc6b87bb1f

See more details on using hashes here.

Supported by

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