Giter Club home page Giter Club logo

pystatemachine's Introduction

PyStateMachine

python Python State Machine

CodeFactor Codacy Badge

DeepSource DeepSource

Introduction

PyStateMachine is a Framework that support state machines in Python

Requirements

  • Python3

How to Run

How do we import the framework?

  1. Insure that Python3 is installed:

  2. Install the PyStateMachines framework

    • pip3 install PyStateMachines

Example

After framework is installed, import and create a small example. Create xml file from sample with the State machine states.

myStateMachine.xml

<?xml version="1.0"?>
<x:States xmlns:x="pystatemachine:sm">
    <State>
        <Name>State1</Name>
        <Event>
            <Name>ToState2</Name>
            <ToState>State2</ToState>
        </Event>
    </State>
    <State>
        <Name>State2</Name>
        <Event>
            <Name>ToState3</Name>
            <ToState>State3</ToState>
        </Event>
    </State>
    <State>
        <Name>State3</Name>
        <Event>
            <Name>ToState1</Name>
            <ToState>State1</ToState>
        </Event>
    </State>
    <Initial_State>State1</Initial_State>
</x:States>

Create Python Script to run state machine pyStateMachine.py

# importing necessary packages
from StateMachine import StateMachine

"""Test StateMachine"""
sm = StateMachine("myStateMachine.xml")
sm.LoadStateMachine()

# print initial state
print(sm.get_current_state())  # current_state == State1

sm.InjectEvent("ToState2")
print(sm.get_current_state())  # current_state == State2

sm.InjectEvent("ToState3")
print(sm.get_current_state())  # current_state == State3

sm.InjectEvent("ToState1")
print(sm.get_current_state())  # current_state == State1

run Python script to execute state machine

python pyStateMachine.py

pyStateMachineUnitTest.py

## Test Suite
#  importing necessary packages
from StateMachine import StateMachine
import unittest


class TestBaseStateMachine(unittest.TestCase):
    """Test state machine using various samples"""

# test all valid state transitions
    def test1(self):
        """Test Statemachine"""
        sm = StateMachine("myStateMachine.xml")
        sm.LoadStateMachine()
        self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
        # OK Event
        sm.InjectEvent("ToState2")
        self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
        # OK Event
        sm.InjectEvent("ToState3")
        self.assertEqual(sm.get_current_state(), "State3", "Should be State3")
        # OK Event
        sm.InjectEvent("ToState1")
        self.assertEqual(sm.get_current_state(), "State1", "Should be State1")

# test invalid State1 transitions
    def test2(self):
        """Test Statemachine"""
        sm = StateMachine("myStateMachine.xml")
        sm.LoadStateMachine()
        self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
        # Invalid transition
        sm.InjectEvent("ToState3")
        self.assertEqual(sm.get_current_state(), "State1", "Should be State1")

# test invalid State2 transitions
    def test3(self):
        """Test Statemachine"""
        sm = StateMachine("myStateMachine.xml")
        sm.LoadStateMachine()
        self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
        # OK Event
        sm.InjectEvent("ToState2")
        self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
        # Invalid transition
        sm.InjectEvent("ToState1")
        self.assertEqual(sm.get_current_state(), "State2", "Should be State2")

# test invalid State3 transitions
    def test4(self):
        """Test Statemachine"""
        sm = StateMachine("myStateMachine.xml")
        sm.LoadStateMachine()
        self.assertEqual(sm.get_current_state(), "State1", "Should be State1")
        # OK Event
        sm.InjectEvent("ToState2")
        self.assertEqual(sm.get_current_state(), "State2", "Should be State2")
        # OK Event
        sm.InjectEvent("ToState3")
        self.assertEqual(sm.get_current_state(), "State3", "Should be State3")
        # Invalid transition
        sm.InjectEvent("ToState2")
        self.assertEqual(sm.get_current_state(), "State3", "Should be State3")
        
if __name__ == '__main__':
    unittest.main()

run unit tests

python pyStateMachineUnitTest.py

How to contribute GitHub contributors

Read the CONTRIBUTING GUIDE

Hacktoberfest

We are pleased to inform you that this repository is participating in the #Hacktoberfest!

Happy Coding!

Contact

E-Mail : [email protected]

GitHub Profile Profile views

ZigRazor's github stats

Support

To support me just add Star the project GitHub stars or follow me GitHub followers

To get updated watch the project GitHub watchers

Project Info

Readme Card

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.