Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
PasaOpasen committed Mar 13, 2024
1 parent 93864a9 commit cd94df9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions geneticalgorithm2/aliases.py
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -14,3 +13,5 @@
array1D: TypeAlias = np.ndarray
array2D: TypeAlias = np.ndarray

PathLike: TypeAlias = Union[str, os.PathLike]

8 changes: 5 additions & 3 deletions geneticalgorithm2/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions geneticalgorithm2/files.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit cd94df9

Please sign in to comment.