From fd04a07d9b12755dfa0f01e45c7e2a77ea17fbf1 Mon Sep 17 00:00:00 2001 From: shinedday <shinedday@gmail.com> Date: Thu, 20 May 2021 09:05:21 +0200 Subject: [PATCH] Add all missing docstring & type --- pyAmakCore/classes/agent.py | 2 +- pyAmakCore/classes/amas.py | 4 +- pyAmakCore/classes/communicating_agent.py | 16 ++--- pyAmakCore/classes/scheduler.py | 61 ++++++++++++++----- .../classes/scheduler_tool/agent_thread.py | 2 +- .../classes/scheduler_tool/amas_thread.py | 5 +- .../scheduler_tool/schedulable_thread.py | 18 ++++-- pyAmakCore/classes/tools/schedulable.py | 9 ++- pyAmakCore/classes/tools/schedulerIHM.py | 2 - 9 files changed, 83 insertions(+), 36 deletions(-) diff --git a/pyAmakCore/classes/agent.py b/pyAmakCore/classes/agent.py index a94ae75..6635186 100644 --- a/pyAmakCore/classes/agent.py +++ b/pyAmakCore/classes/agent.py @@ -183,7 +183,7 @@ class Agent: self.__phase = next_phase.get(self.__phase) def __eq__(self, other: 'Agent') -> bool: - # check class + # should check class if other is None: return False return self.__id == other.__id diff --git a/pyAmakCore/classes/amas.py b/pyAmakCore/classes/amas.py index 7a46093..ccb253d 100644 --- a/pyAmakCore/classes/amas.py +++ b/pyAmakCore/classes/amas.py @@ -50,7 +50,7 @@ class Amas(Schedulable, Loggable): self.__agent_to_add = [] return tmp - def remove_pending_agent(self): + def remove_pending_agent(self) -> List[Agent]: """ add pending agent into agent and return removed agents """ @@ -61,7 +61,7 @@ class Amas(Schedulable, Loggable): return tmp - def add_agent(self, agent: 'Agent') -> None: + def add_agent(self, agent: Agent) -> None: """ add agent in the amas agents list without duplicate """ diff --git a/pyAmakCore/classes/communicating_agent.py b/pyAmakCore/classes/communicating_agent.py index e31b4cc..d70cb0c 100644 --- a/pyAmakCore/classes/communicating_agent.py +++ b/pyAmakCore/classes/communicating_agent.py @@ -17,30 +17,30 @@ class Mail: """ def __init__(self, id_sender: int, id_receiver: int, message: Any, sending_date: int) -> None: - self.__id_sender = id_sender - self.__id_receiver = id_receiver - self.__message = message - self.__sending_date = sending_date + self.__id_sender: int = id_sender + self.__id_receiver: int = id_receiver + self.__message: Any = message + self.__sending_date: int = sending_date - def get_id_sender(self): + def get_id_sender(self) -> int: """ return sender id """ return self.__id_sender - def get_id_receiver(self): + def get_id_receiver(self) -> int: """ return receiver id """ return self.__id_receiver - def get_message(self): + def get_message(self) -> Any: """ return message """ return self.__message - def get_sending_date(self): + def get_sending_date(self) -> int: """ return sending_date """ diff --git a/pyAmakCore/classes/scheduler.py b/pyAmakCore/classes/scheduler.py index b16b13f..a1bdcc2 100644 --- a/pyAmakCore/classes/scheduler.py +++ b/pyAmakCore/classes/scheduler.py @@ -1,10 +1,13 @@ """ Scheduler class """ +from typing import List + import sys import pathlib import pickle from threading import Semaphore, Thread + from time import sleep sys.path.insert(0, str(pathlib.Path(__file__).parent)) @@ -12,6 +15,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).parent)) from pyAmakCore.classes.amas import Amas from pyAmakCore.classes.scheduler_tool.schedulable_thread import SchedulableThread from pyAmakCore.classes.scheduler_tool.amas_thread import AmasThread +from pyAmakCore.classes.tools.schedulable import Schedulable class Scheduler: @@ -22,22 +26,28 @@ class Scheduler: def __init__(self, amas: Amas) -> None: self.amas: Amas = amas - self.exit_bool = False + self.exit_bool: bool = False - self.semaphore_start_stop = Semaphore(0) + self.semaphore_start_stop: Semaphore = Semaphore(0) - self.schedulables = [] - self.schedulables_threads = [] + self.schedulables: List[SchedulableThread] = [] + self.schedulables_threads: List[Thread] = [] self.add_schedulables(amas, AmasThread) self.add_schedulables(amas.get_environment(), SchedulableThread) - self.sleep_time = 0 + self.sleep_time: float = 0 - def get_amas(self): + def get_amas(self) -> Amas: + """ + return amas pointer + """ return self.amas - def add_schedulables(self, schedulable, cls): + def add_schedulables(self, schedulable: Schedulable, cls) -> None: + """ + add a schedulable in scheduler + """ schedulable_thread = cls(schedulable) self.schedulables.append(schedulable_thread) current_thread = Thread(target=schedulable_thread.run) @@ -99,9 +109,12 @@ class Scheduler: self.last_part() sleep(self.sleep_time) - self.close_childs() + self.close_child() - def close_childs(self): + def close_child(self) -> None: + """ + tell all child to shut down + """ for schedulable in self.schedulables: schedulable.exit_bool = True schedulable.is_waiting.release() @@ -112,29 +125,47 @@ class Scheduler: program interface """ - def exit_program(self): + def exit_program(self) -> None: + """ + Exit the system as soon as possible + """ self.exit_bool = True self.semaphore_start_stop.release() - def start(self): + def start(self) -> None: + """ + Unlock the scheduler + """ self.semaphore_start_stop.release() - def stop(self): + def stop(self) -> None: + """ + Lock the scheduler + """ self.semaphore_start_stop.acquire() - def set_sleep(self, sleep_time: int): + def set_sleep(self, sleep_time: int) -> None: + """ + Set sleep value between 2 cycles + """ self.sleep_time = sleep_time """ load & save program """ - def save(self): + def save(self) -> None: + """ + Save the current state of the system + """ with open('filename.pickle', 'wb') as handle: pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL) @classmethod - def load(cls): + def load(cls) -> 'Scheduler': + """ + Load the last save of the system + """ with open('filename.pickle', 'rb') as handle: amas_object = pickle.load(handle) diff --git a/pyAmakCore/classes/scheduler_tool/agent_thread.py b/pyAmakCore/classes/scheduler_tool/agent_thread.py index 72098d2..493562d 100644 --- a/pyAmakCore/classes/scheduler_tool/agent_thread.py +++ b/pyAmakCore/classes/scheduler_tool/agent_thread.py @@ -20,7 +20,7 @@ class AgentThread: action_done = Semaphore(0) execution_policy = ExecutionPolicy.ONE_PHASE - def __init__(self, agent: Agent): + def __init__(self, agent: Agent) -> None: self.agent: Agent = agent self.is_waiting: Semaphore = Semaphore(0) diff --git a/pyAmakCore/classes/scheduler_tool/amas_thread.py b/pyAmakCore/classes/scheduler_tool/amas_thread.py index 1d70f23..dad5c74 100644 --- a/pyAmakCore/classes/scheduler_tool/amas_thread.py +++ b/pyAmakCore/classes/scheduler_tool/amas_thread.py @@ -7,10 +7,10 @@ from typing import List import sys import pathlib -from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) +from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy from pyAmakCore.classes.scheduler_tool.agent_thread import AgentThread from pyAmakCore.classes.amas import Amas from pyAmakCore.classes.agent import Agent @@ -105,6 +105,9 @@ class AmasThread(SchedulableThread): self.schedulable.cycle() def run(self) -> None: + """ + override run so when amas_thread try to stop, he closes all agents threads + """ super().run() self.close_child() diff --git a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py index 17f347a..9429e26 100644 --- a/pyAmakCore/classes/scheduler_tool/schedulable_thread.py +++ b/pyAmakCore/classes/scheduler_tool/schedulable_thread.py @@ -16,20 +16,28 @@ class SchedulableThread: thread class used to thread schedulable """ - def __init__(self, schedulable: Schedulable): + def __init__(self, schedulable: Schedulable) -> None: self.schedulable: Schedulable = schedulable self.is_waiting: Semaphore = Semaphore(0) self.exit_bool: bool = False self.action_done: Semaphore = Semaphore(0) - def on_cycle_begin(self): + def on_cycle_begin(self) -> None: + """ + first part of the cycle + """ self.schedulable.on_cycle_begin() - def main_cycle_part(self): - pass + def main_cycle_part(self) -> None: + """ + main part of the cycle + """ - def on_cycle_end(self): + def on_cycle_end(self) -> None: + """ + last part of the cycle + """ self.schedulable.on_cycle_end() self.schedulable.cycle() diff --git a/pyAmakCore/classes/tools/schedulable.py b/pyAmakCore/classes/tools/schedulable.py index c594ba8..dda929c 100644 --- a/pyAmakCore/classes/tools/schedulable.py +++ b/pyAmakCore/classes/tools/schedulable.py @@ -3,6 +3,7 @@ Schedulable interface """ import sys import pathlib + sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) from pyAmakCore.exception.override import ToOverrideWarning @@ -15,12 +16,18 @@ class Schedulable: def __init__(self): - self.__nbr_cycle = 0 + self.__nbr_cycle: int = 0 def get_cycle(self) -> int: + """ + return nbr_cycle + """ return self.__nbr_cycle def cycle(self) -> None: + """ + add 1 to nbr_cycle + """ self.__nbr_cycle += 1 def on_initialization(self) -> None: diff --git a/pyAmakCore/classes/tools/schedulerIHM.py b/pyAmakCore/classes/tools/schedulerIHM.py index 9390671..a9358b5 100644 --- a/pyAmakCore/classes/tools/schedulerIHM.py +++ b/pyAmakCore/classes/tools/schedulerIHM.py @@ -2,8 +2,6 @@ Scheduler class that need to be used for pyAmakIhm """ import pathlib -from time import sleep - import sys sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) -- GitLab