From 0667d797fa8038120426aedfddfe1fbb8fbf340e Mon Sep 17 00:00:00 2001 From: afro-coder Date: Tue, 5 Oct 2021 23:20:00 +0530 Subject: [PATCH 1/2] Added Docstrings to Functions Signed-off-by: afro-coder --- src/Action.py | 4 ++++ src/Actions.py | 4 ++++ src/Condition.py | 4 ++++ src/Conditions.py | 4 ++++ src/Event.py | 4 ++++ src/ReadStateMachine.py | 2 ++ src/State.py | 4 ++++ src/StateMachine.py | 14 +++++++------- tests/testBaseStateMachine.py | 21 ++++++++++++++++++++- 9 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Action.py b/src/Action.py index e571da5..69cb96d 100644 --- a/src/Action.py +++ b/src/Action.py @@ -2,9 +2,13 @@ class Action: """Define Action Class""" def __init__(self, expression: str): + """Initialize Action""" + self.expression = expression @staticmethod def to_string(): + """To string""" + result_s = "" return result_s diff --git a/src/Actions.py b/src/Actions.py index 9664672..6cbbf1f 100644 --- a/src/Actions.py +++ b/src/Actions.py @@ -2,11 +2,15 @@ class Actions: """Define and check actions""" def __init__(self, actions=None): + """Initialize Actions class""" + if actions is None: actions = [] self.actions_list = actions @staticmethod def to_string(): + """To string""" + result_s = "" return result_s diff --git a/src/Condition.py b/src/Condition.py index 38283c3..fb43ce5 100644 --- a/src/Condition.py +++ b/src/Condition.py @@ -2,10 +2,14 @@ class Condition: """Define a Condition Class""" def __init__(self, expression: str, result: str): + """Initialize Condition class""" + self.expression = expression self.result = result @staticmethod def to_string(): + """To string""" + result_s = "" return result_s diff --git a/src/Conditions.py b/src/Conditions.py index bd7a5df..d6b2d2c 100644 --- a/src/Conditions.py +++ b/src/Conditions.py @@ -2,11 +2,15 @@ class Conditions: """Define Conditions""" def __init__(self, conditions=None): + """Initialize Conditions""" + if conditions is None: conditions = [] self.conditions_list = conditions @staticmethod def to_string(): + """To string""" + result_s = "" return result_s diff --git a/src/Event.py b/src/Event.py index 5e42a50..6df35ac 100644 --- a/src/Event.py +++ b/src/Event.py @@ -6,6 +6,8 @@ class Event: """Construct an Event class""" def __init__(self, name: str, to_state: str, pre_conditions: Conditions, post_conditions: Conditions, pre_actions: Actions, post_actions: Actions): + """Initialize Event Class""" + self.name = name self.to_state = to_state self.pre_conditions = pre_conditions @@ -14,6 +16,8 @@ def __init__(self, name: str, to_state: str, pre_conditions: Conditions, post_co self.post_actions = post_actions def to_string(self): + """To string""" + result_s = "Event: \n" result_s += "\tName: " + self.name + "\n" result_s += "\tToState: " + self.to_state + "\n" diff --git a/src/ReadStateMachine.py b/src/ReadStateMachine.py index bb75fba..947a38c 100644 --- a/src/ReadStateMachine.py +++ b/src/ReadStateMachine.py @@ -8,6 +8,8 @@ def ReadStateMachineFile(xml_file: str): + """Read the xml file and parse it""" + states = {} initial_state = "" tree = ET.parse(xml_file) diff --git a/src/State.py b/src/State.py index e4325c8..8249ea7 100644 --- a/src/State.py +++ b/src/State.py @@ -2,12 +2,16 @@ class State: """Define the state class""" def __init__(self, name: str, events=None): + """Initialize State""" + if events is None: events = {} self.name = name self.events = events def to_string(self): + """To string""" + result_s = "State:\n" result_s += " Name: " + self.name + "\n" result_s += " Events: \n" diff --git a/src/StateMachine.py b/src/StateMachine.py index dd29742..37ed5a1 100644 --- a/src/StateMachine.py +++ b/src/StateMachine.py @@ -5,7 +5,7 @@ class StateMachine: """ This is a class defines state machine operations. - + Attributes: xml_file (str): The name of the xml that defines the state machine. states (list): list of possible states of the machine. @@ -16,7 +16,7 @@ class StateMachine: def __init__(self, xml_file: str): """ The constructor for StateMachine class. - + Parameters: xml_file (str): The name of the xml that defines the state machine. """ @@ -31,7 +31,7 @@ def __init__(self, xml_file: str): def __CheckConditions(self, conditions): """ This Function checks the conditions passed as argument - + Parameters: conditions (list): List of condition to check. Returns: @@ -69,7 +69,7 @@ def __CheckConditions(self, conditions): def __ExecActions(self, actions): """ This Function executes the actions passed as argument - + Parameters: actions (list): List of actions to execute. Returns: @@ -115,7 +115,7 @@ def __RestoreState(self): def __PrepareExpression(expression): """ This Function split expression in module and expression - + Parameters: expression (str): complete expression. Returns: @@ -148,7 +148,7 @@ def LoadStateMachine(self): def addModuleToContext(self, module: str): """ This Function adds a module to the context of state machine - + Parameters: module (str): The module to add. """ @@ -158,7 +158,7 @@ def addModuleToContext(self, module: str): def InjectEvent(self, event: str): """ This Function execute the event injected - + Parameters: event (str): Event injected """ diff --git a/tests/testBaseStateMachine.py b/tests/testBaseStateMachine.py index f84b4ce..1ce433a 100644 --- a/tests/testBaseStateMachine.py +++ b/tests/testBaseStateMachine.py @@ -7,18 +7,26 @@ def everFalse(): + """Return false""" + return False def everTrue(): + """Return false""" + return True def testPrint(): + """Return Test""" + print("Test") def setTestTo3(): + """Sets Test to 3""" + global test # Needed to modify global copy of globvar print(test) test = 3 @@ -26,6 +34,8 @@ def setTestTo3(): def printTest(): + """Print Test""" + print("Test: ", test) @@ -33,7 +43,7 @@ class TestBaseStateMachine(unittest.TestCase): """Test state machine using various samples""" def test1(self): - + """Test Statemachine""" sm = StateMachine("../sample/sample1.xml") sm.LoadStateMachine() @@ -55,6 +65,7 @@ def test1(self): self.assertEqual(sm.get_current_state(), "Null", "Should be Null") def test2(self): + """Second Test for Statemachine""" sm = StateMachine("../sample/sample2.xml") @@ -81,6 +92,8 @@ def test2(self): self.assertEqual(sm.get_current_state(), "Exit", "Should be Exit") def test3(self): + """Test 3 for State Machine""" + sm = StateMachine("../sample/sample3.xml") sm.LoadStateMachine() @@ -106,6 +119,8 @@ def test3(self): self.assertEqual(sm.get_current_state(), "Exit", "Should be Exit") def test4(self): + """Test 4 for state machine""" + sm = StateMachine("../sample/sample4.xml") sm.LoadStateMachine() @@ -131,6 +146,8 @@ def test4(self): self.assertEqual(sm.get_current_state(), "Null", "Should be Null") def test5(self): + """Test 5 for state machine""" + sm = StateMachine("../sample/sample5.xml") sm.LoadStateMachine() @@ -156,6 +173,8 @@ def test5(self): self.assertEqual(sm.get_current_state(), "Null", "Should be Null") def test6(self): + """Test 6 for state machine""" + global test test = 2 sm = StateMachine("../sample/sample6.xml") From bdd0814acd258736ab373fef4cc667153e94500e Mon Sep 17 00:00:00 2001 From: afro-coder Date: Tue, 5 Oct 2021 23:24:01 +0530 Subject: [PATCH 2/2] Added Docstring for functions Deleted new lines after functions Signed-off-by: afro-coder --- src/Action.py | 2 -- src/Actions.py | 2 -- src/Condition.py | 2 -- src/Conditions.py | 2 -- src/Event.py | 2 -- src/ReadStateMachine.py | 1 - src/State.py | 2 -- tests/testBaseStateMachine.py | 11 ----------- 8 files changed, 24 deletions(-) diff --git a/src/Action.py b/src/Action.py index 69cb96d..7ae2b7d 100644 --- a/src/Action.py +++ b/src/Action.py @@ -3,12 +3,10 @@ class Action: def __init__(self, expression: str): """Initialize Action""" - self.expression = expression @staticmethod def to_string(): """To string""" - result_s = "" return result_s diff --git a/src/Actions.py b/src/Actions.py index 6cbbf1f..0f5e843 100644 --- a/src/Actions.py +++ b/src/Actions.py @@ -3,7 +3,6 @@ class Actions: def __init__(self, actions=None): """Initialize Actions class""" - if actions is None: actions = [] self.actions_list = actions @@ -11,6 +10,5 @@ def __init__(self, actions=None): @staticmethod def to_string(): """To string""" - result_s = "" return result_s diff --git a/src/Condition.py b/src/Condition.py index fb43ce5..786d19c 100644 --- a/src/Condition.py +++ b/src/Condition.py @@ -3,13 +3,11 @@ class Condition: def __init__(self, expression: str, result: str): """Initialize Condition class""" - self.expression = expression self.result = result @staticmethod def to_string(): """To string""" - result_s = "" return result_s diff --git a/src/Conditions.py b/src/Conditions.py index d6b2d2c..5aac4ca 100644 --- a/src/Conditions.py +++ b/src/Conditions.py @@ -3,7 +3,6 @@ class Conditions: def __init__(self, conditions=None): """Initialize Conditions""" - if conditions is None: conditions = [] self.conditions_list = conditions @@ -11,6 +10,5 @@ def __init__(self, conditions=None): @staticmethod def to_string(): """To string""" - result_s = "" return result_s diff --git a/src/Event.py b/src/Event.py index 6df35ac..d68e9b1 100644 --- a/src/Event.py +++ b/src/Event.py @@ -7,7 +7,6 @@ class Event: def __init__(self, name: str, to_state: str, pre_conditions: Conditions, post_conditions: Conditions, pre_actions: Actions, post_actions: Actions): """Initialize Event Class""" - self.name = name self.to_state = to_state self.pre_conditions = pre_conditions @@ -17,7 +16,6 @@ def __init__(self, name: str, to_state: str, pre_conditions: Conditions, post_co def to_string(self): """To string""" - result_s = "Event: \n" result_s += "\tName: " + self.name + "\n" result_s += "\tToState: " + self.to_state + "\n" diff --git a/src/ReadStateMachine.py b/src/ReadStateMachine.py index 947a38c..bf51bf4 100644 --- a/src/ReadStateMachine.py +++ b/src/ReadStateMachine.py @@ -9,7 +9,6 @@ def ReadStateMachineFile(xml_file: str): """Read the xml file and parse it""" - states = {} initial_state = "" tree = ET.parse(xml_file) diff --git a/src/State.py b/src/State.py index 8249ea7..92ac03a 100644 --- a/src/State.py +++ b/src/State.py @@ -3,7 +3,6 @@ class State: def __init__(self, name: str, events=None): """Initialize State""" - if events is None: events = {} self.name = name @@ -11,7 +10,6 @@ def __init__(self, name: str, events=None): def to_string(self): """To string""" - result_s = "State:\n" result_s += " Name: " + self.name + "\n" result_s += " Events: \n" diff --git a/tests/testBaseStateMachine.py b/tests/testBaseStateMachine.py index 1ce433a..ea67bac 100644 --- a/tests/testBaseStateMachine.py +++ b/tests/testBaseStateMachine.py @@ -8,25 +8,21 @@ def everFalse(): """Return false""" - return False def everTrue(): """Return false""" - return True def testPrint(): """Return Test""" - print("Test") def setTestTo3(): """Sets Test to 3""" - global test # Needed to modify global copy of globvar print(test) test = 3 @@ -35,7 +31,6 @@ def setTestTo3(): def printTest(): """Print Test""" - print("Test: ", test) @@ -45,7 +40,6 @@ class TestBaseStateMachine(unittest.TestCase): def test1(self): """Test Statemachine""" sm = StateMachine("../sample/sample1.xml") - sm.LoadStateMachine() self.assertEqual(sm.get_current_state(), "Enter", "Should be Enter") # OK Event @@ -66,7 +60,6 @@ def test1(self): def test2(self): """Second Test for Statemachine""" - sm = StateMachine("../sample/sample2.xml") sm.LoadStateMachine() @@ -93,7 +86,6 @@ def test2(self): def test3(self): """Test 3 for State Machine""" - sm = StateMachine("../sample/sample3.xml") sm.LoadStateMachine() @@ -120,7 +112,6 @@ def test3(self): def test4(self): """Test 4 for state machine""" - sm = StateMachine("../sample/sample4.xml") sm.LoadStateMachine() @@ -147,7 +138,6 @@ def test4(self): def test5(self): """Test 5 for state machine""" - sm = StateMachine("../sample/sample5.xml") sm.LoadStateMachine() @@ -174,7 +164,6 @@ def test5(self): def test6(self): """Test 6 for state machine""" - global test test = 2 sm = StateMachine("../sample/sample6.xml")