From cd94df9d23cdef3044bb5d408ffc6be4c83f245b Mon Sep 17 00:00:00 2001 From: demetry pascal Date: Wed, 13 Mar 2024 20:56:45 +0300 Subject: [PATCH] - --- README.md | 8 ++++++++ geneticalgorithm2/aliases.py | 11 ++++++----- geneticalgorithm2/classes.py | 8 +++++--- geneticalgorithm2/files.py | 27 +++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 geneticalgorithm2/files.py diff --git a/README.md b/README.md index 6ee59f6..bad0b82 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ https://pasaopasen.github.io/geneticalgorithm2/ - [Installation](#installation) - [Updates information](#updates-information) - [**Future**](#future) + - [**TODO firstly**](#todo-firstly) + - [6.8.7 minor update](#687-minor-update) - [6.8.6 minor update](#686-minor-update) - [6.8.5 minor update](#685-minor-update) - [6.8.4 minor update](#684-minor-update) @@ -153,6 +155,12 @@ pip install geneticalgorithm2[full] ## **TODO firstly** - Remove old style mensions from README +## 6.8.7 minor update + +- some code refactor +- fixes: + - ensure the directory of generation file exists on save + ## 6.8.6 minor update - small package installation update: add `pip install geneticalgorithm2[full]` version diff --git a/geneticalgorithm2/aliases.py b/geneticalgorithm2/aliases.py index cc2171b..e474ffc 100644 --- a/geneticalgorithm2/aliases.py +++ b/geneticalgorithm2/aliases.py @@ -1,11 +1,10 @@ -import sys + from typing import List, Tuple, Dict, Sequence, Optional, Any, Callable, Union, TypeVar, Literal +from typing_extensions import TypeAlias + +import os -if sys.version_info.minor < 10: - from typing_extensions import TypeAlias -else: - from typing import TypeAlias Number: TypeAlias = Union[int, float] @@ -14,3 +13,5 @@ array1D: TypeAlias = np.ndarray array2D: TypeAlias = np.ndarray +PathLike: TypeAlias = Union[str, os.PathLike] + diff --git a/geneticalgorithm2/classes.py b/geneticalgorithm2/classes.py index b296cdd..17cc9f7 100644 --- a/geneticalgorithm2/classes.py +++ b/geneticalgorithm2/classes.py @@ -7,7 +7,8 @@ import numpy as np -from .aliases import array1D, array2D, TypeAlias +from .aliases import array1D, array2D, TypeAlias, PathLike +from .files import mkdir_of_file from .crossovers import Crossover, CrossoverFunc from .mutations import Mutations, MutationIntFunc, MutationFloatFunc @@ -183,11 +184,12 @@ def as_wide_matrix(self) -> array2D: # should not be used in main code -- was needed for old versions return union_to_matrix(self.variables, self.scores) - def save(self, path: str): + def save(self, path: PathLike): + mkdir_of_file(path) np.savez(path, population=self.variables, scores=self.scores) @staticmethod - def load(path: str): + def load(path: PathLike): try: st = np.load(path) except Exception as err: diff --git a/geneticalgorithm2/files.py b/geneticalgorithm2/files.py new file mode 100644 index 0000000..30563ca --- /dev/null +++ b/geneticalgorithm2/files.py @@ -0,0 +1,27 @@ + + +from pathlib import Path + +from .aliases import PathLike + + +def _mkdir(path: Path): + path.mkdir(parents=True, exist_ok=True) + + +def mkdir_of_file(file_path: PathLike): + """ + для этого файла создаёт папку, в которой он должен лежать + """ + _mkdir(Path(file_path).parent) + + +def mkdir(path: PathLike): + """mkdir with parents""" + _mkdir(Path(path)) + + +def touch(path: PathLike): + """makes empty file, makes directories for this file automatically""" + mkdir_of_file(path) + Path(path).touch()