Skip to main content

Automata

Table of Contents
  1. Introduction
  2. Getting Started
  3. States, Events, State Changes, Data Events, and Payloads
  4. Advanced
  5. License

Introduction

Automata is a lightweight framework that allows users to define a state machine based websocket server. Each machine has a collection of states, for example, a traffic light state machine has Green, Yellow, and Red states. Each state can have any number of events, these events can be defined by the developer to do whatever they want. They can return data back to the client, transition to a new state, process data, or even do nothing; that is all up the the desired implementation. Overall, the goal of Automata is to provide a unified methodology and doctrine for defining and improving the clarity of websocket communication.

Getting Started

This is a simple example of how you to set up your Automata instance and run your application.

Prerequisites

  • python version >= 3.7

Installation and Setup

  1. Install with pip install automata-ws

  2. Initialize your Automata (we use app.py)

from automata import Automata

my_machine = Automata(
  name="myMachine"
)
  1. Create your states and any events (in any directory you want)
from automata import State, EventStatus, transition, transmit

red_light = State('red_light')

@red_light.event('change_to_green')
async def handler(automata, data):
  #do something here...
  run_some_func()
  await transition(automata, 'green', EventStatus.OK) #will change the state of our machine and will report this to the client
  
green_state = State(
  name="green",
  targets=["yellow"]
)

@green_light.event('change_to_yellow')
async def handler(automata, data):
  await transmit(automata, EventStatus.OK, some_data) #will send back data without changing state
  
#and so on and so on...
  1. Register states in your machine (back in the file you defined the machine)
my_machine.register_state(red_light) #the first state that is registered will be the initial state for new sessions
my_machine.register_state(green_light)
  1. Run the machine
my_machine.run('localhost', 8000)

States, Events, State Changes, Data Events, and Payloads

1. States

  • Every state must be unique: there cannot exist two states with the same name

  • A state may have a list of target states: all of the states in which a transition from the referenced state is possible

    • Trying to transition to a state that is not in the target list for the referenced state will raise an exception
    • An undefined target list means that a transition to any state is allowed

    green_state = State(
      name="green",
      targets=["yellow"] #the target list with all the states that the green_state can transition to
    )
    
  • The initial state is the one that is registered first in the Automata instance

2. Events

  • Events defined for a specific state must be unique
  • All events are identified by their name and defined by their handler
  • Each handler will recieve an Automata instance and a data instance (either as a dict or a str)

@green_state.event('change_to_yellow') #this annotation will create an event for the corresponding state (green_state in this case)
async def handler(automata: Automata, data):
  run_some_code()
  do_whatever_you_want()
  await transition(automata, 'yellow', EventStatus.OK)

@green_state.event('ping')
def handler(automata: Automata, data): #the handler doesn't necessarily have to be async
  print('pong')

3. State Changes

  • Each state transition has to be relayed to the client: they will receive data about their new state and all events they can call
  • Each state transition has to be accompanied with a status code: these are similar to the 100, 200, 300, 400, 500 status codes defined in HTTP and are found in the EventStatus enum
  • A state transition may have data associated with it if needed (data is optional)
  • A state transition is achieved through the transition function

    • await transition(automata, 'logged_in', EventStatus.OK, some_data) #a state transition with some data
      

4. Data Events

  • It is possible to send data back to the client without changing state: this is achieved through the transmit function
  • Each data event must be accompanied with a status code (as mentioned above)

    • await transmit(automata, EventStatus.OK, some_data) #a data event with no state transition
      

5. Payloads

  • All client to server payloads must follow the format below

    • {
        "event" : "event_name",
        "data" : data
      }
      
  • A state change will be relayed to the client in the following format (occurs when transition is called)

    • {
        "state" : "new_state",
        "events" : [
            "event1",
            "event2", //Any number of possible events associated with the new state
        ],
        "status" : 200,
        "data": "some_data" //optional: can be None/Null
      }
      
  • Any generic data event will be returned back in the following format (occurs when transmit is called)

    • {
        "status" : 200,
        "data": "some_data" //optional
      }
      

Advanced

1. Custom HTTP Endpoint(s)

  • As of now all websocket connections are served on /
  • It is possible to implement a custom endpoint using the automata.endpoint('/endpoint') annotation
  • This is useful if a health check (or something generic) is needed

    • @my_machine.endpoint('/health')
      async def some_handler(request_headers):
          return 'OK'
      
  • Note: only get requests can be handled

License

Apache License 2.0

Metadata

Release files for automata-ws 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for automata-ws 0.1.1
File Size Uploaded Provenance
automata-ws-0.1.1.tar.gz 10.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for automata-ws 0.1.1
File Interpreter ABI Platform
automata_ws-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / automata-ws-0.1.1.tar.gz

Download URL automata-ws-0.1.1.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
85614a00f1a482d0a7f54e17b26fc70caf935c4e17f40bd12c5d3d812ee4fc7c
BLAKE2b-256 checksum
How to use checksums
ce6b40ab37e14ecfc82e32595d7ec14be0ab22f833498e14a719be89a49df330
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / automata_ws-0.1.1-py3-none-any.whl

Download URL automata_ws-0.1.1-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6580eb5dbf17182d79be1ea687a93c660a31493869bfece51fbc7106e67077e0
BLAKE2b-256 checksum
How to use checksums
ca70cc833ec09695c1c3ce7f0591e9deac62756b935c9715302c19e8d8d91261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Security for automata-ws 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page