Skip to content
Snippets Groups Projects
Commit c7229918 authored by shined day's avatar shined day
Browse files

Merge branch '24-generate-csv' into 'master'

Resolve "Generate CSV"

Closes #24

See merge request be-pyamak/pyamak-noyau!13
parents 3b3dd420 91417fac
Branches
Tags
No related merge requests found
...@@ -221,3 +221,6 @@ class Agent: ...@@ -221,3 +221,6 @@ class Agent:
if other is None: if other is None:
return False return False
return self.__id == other.__id return self.__id == other.__id
def __repr__(self):
return str(self.__id)
...@@ -3,11 +3,14 @@ Amas Class ...@@ -3,11 +3,14 @@ Amas Class
""" """
from threading import Thread from threading import Thread
from typing import List from typing import List
import sys import sys
import pathlib import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.tools.loggable import Loggable
from pyAmakCore.classes.schedulable import Schedulable from pyAmakCore.classes.schedulable import Schedulable
from pyAmakCore.classes.scheduler import Scheduler from pyAmakCore.classes.scheduler import Scheduler
from pyAmakCore.classes.environment import Environment from pyAmakCore.classes.environment import Environment
...@@ -16,14 +19,15 @@ from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy ...@@ -16,14 +19,15 @@ from pyAmakCore.enumeration.executionPolicy import ExecutionPolicy
from pyAmakCore.exception.override import ToOverrideWarning from pyAmakCore.exception.override import ToOverrideWarning
class Amas(Schedulable): class Amas(Schedulable, Loggable):
""" """
Amas Class Amas Class
""" """
def __init__(self, environment: Environment) -> None: def __init__(self, environment: Environment) -> None:
super().__init__() Schedulable.__init__(self)
Loggable.__init__(self)
self.__environment: Environment = environment self.__environment: Environment = environment
self.__agents: List[Agent] = [] self.__agents: List[Agent] = []
self.__nbrcycle: int = 0 self.__nbrcycle: int = 0
...@@ -115,7 +119,6 @@ class Amas(Schedulable): ...@@ -115,7 +119,6 @@ class Amas(Schedulable):
Main behavior of Amas Main behavior of Amas
""" """
print("Cycle : ", self.__nbrcycle) print("Cycle : ", self.__nbrcycle)
self.__nbrcycle += 1
self.on_cycle_begin() self.on_cycle_begin()
self.synchronization() self.synchronization()
...@@ -143,6 +146,8 @@ class Amas(Schedulable): ...@@ -143,6 +146,8 @@ class Amas(Schedulable):
self.synchronization() self.synchronization()
self.on_cycle_end() self.on_cycle_end()
self.to_csv(self.get_cycle(), self.get_agents())
self.__nbrcycle += 1
def put_token(self) -> None: def put_token(self) -> None:
""" """
......
"""
class allowing to save the state of the system at a given moment
"""
from os import path
from pandas import DataFrame
class Loggable:
"""
class Loggable
"""
def __init__(self):
self.__do_log = False
self.__file_path = None
def to_csv(self, cycle, var_list):
"""
get cycle and agent list and print them
"""
if not self.__do_log:
return
ignore_attribute = ["_Agent__amas", "_Agent__environment"]
table = [{**{e: x[e] for e in x if e not in ignore_attribute},
**{'nombre_cycle': cycle}} for x in map(vars, var_list)]
dataframe = DataFrame(table)
if self.__file_path is None:
print(dataframe.to_csv(index=False))
else:
if path.exists(self.__file_path):
dataframe.to_csv(path_or_buf=self.__file_path, mode='a', header=False, index=False)
else:
dataframe.to_csv(path_or_buf=self.__file_path, index=False)
def set_do_log(self, boolean):
self.__do_log = boolean
def set_file_path(self, path_to_file):
self.__file_path = path_to_file
pandas
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment