From 202f231107b0a0947571c53d99ce2b87c753f36b Mon Sep 17 00:00:00 2001 From: lukas leufen <l.leufen@fz-juelich.de> Date: Fri, 8 Nov 2019 15:26:38 +0100 Subject: [PATCH] moved check_path_and_create from DataPrep class method to helpers.py, because is needed for DataGenerator too --- src/data_preparation.py | 10 +--------- src/helpers.py | 12 ++++++++++++ test/test_data_preparation.py | 12 ------------ test/test_helpers.py | 18 +++++++++++++++++- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/data_preparation.py b/src/data_preparation.py index c23c9a9f..873433f4 100644 --- a/src/data_preparation.py +++ b/src/data_preparation.py @@ -71,7 +71,7 @@ class DataPrep(object): data is available. The latter case, store downloaded data locally if wished (default yes). """ - self.check_path_and_create() + helpers.check_path_and_create(self.path) file_name = self._set_file_name() meta_file = self._set_meta_file_name() try: @@ -113,14 +113,6 @@ class DataPrep(object): return f"Dataprep(path='{self.path}', network='{self.network}', station={self.station}, " \ f"variables={self.variables}, **{self.kwargs})" - def check_path_and_create(self): - try: - os.makedirs(self.path) - logging.info(f"Created path: {self.path}") - except FileExistsError: - logging.info(f"Path already exists: {self.path}") - pass - def interpolate(self, dim: str, method: str = 'linear', limit: int = None, use_coordinate: Union[bool, str] = True, **kwargs): """ diff --git a/src/helpers.py b/src/helpers.py index 424b2fb5..28667097 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -2,7 +2,19 @@ __author__ = 'Lukas Leufen' __date__ = '2019-10-21' +import os +import logging + + def to_list(arg): if not isinstance(arg, list): arg = [arg] return arg + + +def check_path_and_create(path): + try: + os.makedirs(path) + logging.info(f"Created path: {path}") + except FileExistsError: + logging.info(f"Path already exists: {path}") diff --git a/test/test_data_preparation.py b/test/test_data_preparation.py index 0e0984f0..c118e0d5 100644 --- a/test/test_data_preparation.py +++ b/test/test_data_preparation.py @@ -29,18 +29,6 @@ class TestDataPrep: with pytest.raises(NotImplementedError): DataPrep('data/', 'dummy', 'DEBW107', ['o3', 'temp']) - def test_check_path_and_create(self, caplog): - caplog.set_level(logging.INFO) - d = object.__new__(DataPrep) - d.path = 'data/test' - assert not os.path.exists('data/test') - d.check_path_and_create() - assert os.path.exists('data/test') - assert caplog.messages[0] == "Created path: data/test" - d.check_path_and_create() - assert caplog.messages[1] == "Path already exists: data/test" - os.rmdir('data/test') - def test_repr(self): d = object.__new__(DataPrep) d.path = 'data/test' diff --git a/test/test_helpers.py b/test/test_helpers.py index a88eb9c2..fa5f1dcc 100644 --- a/test/test_helpers.py +++ b/test/test_helpers.py @@ -1,5 +1,7 @@ import pytest -from src.helpers import to_list +from src.helpers import to_list, check_path_and_create +import logging +import os class TestToList: @@ -9,3 +11,17 @@ class TestToList: assert to_list('abcd') == ['abcd'] assert to_list([1, 2, 3]) == [1, 2, 3] assert to_list([45]) == [45] + + +class TestCheckPath: + + def test_check_path_and_create(self, caplog): + caplog.set_level(logging.INFO) + path = 'data/test' + assert not os.path.exists('data/test') + check_path_and_create(path) + assert os.path.exists('data/test') + assert caplog.messages[0] == "Created path: data/test" + check_path_and_create(path) + assert caplog.messages[1] == "Path already exists: data/test" + os.rmdir('data/test') -- GitLab