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

Merge branch 'save_and_load_in_specific_file' into 'master'

Save and load in specific file

See merge request be-pyamak/pyamak-noyau!25
parents c7adcccb 479024b7
No related branches found
No related tags found
No related merge requests found
"""
class Callable
"""
import pathlib
import sys
from threading import Semaphore
class Callable:
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.scheduler_tool.scheduler_state import State
from pyAmakCore.classes.scheduler_tool.savable import Savable
class Callable(Savable):
"""
Class that implement useful method to interact with the program
"""
def __init__(self) -> None:
def __init__(self, amas) -> None:
super().__init__(amas)
self.exit_bool: bool = False
self.sleep_time: float = 0
......@@ -26,12 +35,14 @@ class Callable:
"""
Unlock the scheduler
"""
self.state = State.RUNNING
self.semaphore_start_stop.release()
def stop(self) -> None:
"""
Lock the scheduler
"""
self.state = State.WAITING
self.semaphore_start_stop.acquire()
def set_sleep(self, sleep_time: int) -> None:
......
......@@ -2,20 +2,25 @@
Savable Class
"""
import pickle
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.amas import Amas
from pyAmakCore.classes.scheduler_tool.scheduler_state import State
class Savable:
"""
Class that implement convenient method to save and load an amas
"""
def __init__(self, amas: Amas) -> None:
self.amas = amas
self.amas: Amas = amas
self.state: State = State.RUNNING
self.save_path = None
def get_amas(self):
"""
......@@ -23,19 +28,30 @@ class Savable:
"""
return self.amas
def save(self) -> None:
def _dump(self, file):
"""
Save the current state of the system
"""
with open('filename.pickle', 'wb') as handle:
with open(file, 'wb') as handle:
pickle.dump(self.amas, handle, protocol=pickle.HIGHEST_PROTOCOL)
def save(self, file="filename.pickle") -> None:
"""
try to save if scheduler is not working
"""
if self.state == State.RUNNING:
self.save_path = file
self.state = State.NEED_TO_SAVE
if self.state == State.WAITING:
self._dump(file)
@classmethod
def load(cls) -> 'Savable':
def load(cls, file="filename.pickle") -> 'Savable':
"""
Load the last save of the system
"""
with open('filename.pickle', 'rb') as handle:
with open(file, 'rb') as handle:
amas_object = pickle.load(handle)
return cls(amas_object)
\ No newline at end of file
return cls(amas_object)
......@@ -6,6 +6,8 @@ from time import sleep
import sys
import pathlib
from pyAmakCore.classes.scheduler_tool.scheduler_state import State
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from pyAmakCore.classes.scheduler_tool.callable import Callable
......@@ -19,9 +21,7 @@ class SchedulerCore(Callable, Savable):
"""
def __init__(self, amas: Amas) -> None:
Callable.__init__(self)
Savable.__init__(self, amas)
Callable.__init__(self, amas)
def first_part(self) -> None:
"""
......@@ -38,6 +38,13 @@ class SchedulerCore(Callable, Savable):
last part of a cycle
"""
def try_to_save(self):
"""
try to save scheduler if need to
"""
if self.state == State.NEED_TO_SAVE:
self._dump(self.save_path)
def run(self) -> None:
"""
main part of amak core
......@@ -45,6 +52,7 @@ class SchedulerCore(Callable, Savable):
while not self.exit_bool:
print("Cycle : ", self.amas.get_cycle())
self.try_to_save()
self.semaphore_start_stop.acquire()
if self.exit_bool:
break
......@@ -54,4 +62,5 @@ class SchedulerCore(Callable, Savable):
self.main_part()
self.last_part()
self.try_to_save()
sleep(self.sleep_time)
"""
Scheduler State
"""
from enum import Enum, auto
class State(Enum):
"""
Scheduler State
"""
"""
Scheduler is waiting
"""
WAITING = auto()
"""
Scheduler is running
"""
RUNNING = auto()
"""
Scheduler is running and should save as soon as possible
"""
NEED_TO_SAVE = auto()
......@@ -23,6 +23,12 @@ class SimpleAmas(Amas):
class SimpleEnv(Environment):
pass
class SimpleSchedulerMono(SchedulerMono):
def last_part(self) -> None:
super().last_part()
self.save("test.pickle")
import time
start_time = time.time()
ToOverrideWarning.enable_warning(False)
......@@ -31,7 +37,7 @@ env = SimpleEnv()
amas = SimpleAmas(env)
#scheduler = Scheduler(amas)
scheduler = SchedulerMono(amas)
scheduler = SimpleSchedulerMono(amas)
scheduler.start()
scheduler.run()
......
v0.1.2:
* Add : Save & load can now accept path to save & load file
* Fix : You could save in a middle of a cycle, which makes the backup file corrupted
V0.1.1:
* Add mono threading scheduler
* Add seed in environment
v0.1.0:
WARNING : all previous example will no longer work in this version, and all v0.1.0+ example won't work in previous version
WARNING : all previous example will no longer work in this version, and all v0.1.0+ example won't work in previous version (need to create a scheduler)
* Way better thread management
* Add save and load
......
File added
......@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
setup(
name='pyAmakCore',
packages=find_packages(),
version='0.1.1',
version='0.1.2',
description='AmakFramework in python',
author='BE',
install_requires=[],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment