Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #191 from delira-dev/fix_utils_imports
Browse files Browse the repository at this point in the history
Fix Utils imports
  • Loading branch information
justusschock committed Aug 29, 2019
2 parents c754caa + 27ac191 commit b84c229
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 51 deletions.
16 changes: 11 additions & 5 deletions delira/utils/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ class SlackMessenger(BaseMessenger):
top-right corner (you may need to create an own workspace
where you can install your bot).
.. warning:: Slack messenger has ´slackclient´ as a dependency which
is not included in the requirement!
.. warning:: Slack messenger has ´slackclient==1.3.1´ as a dependency which
is not included in the requirements!
"""

def __init__(self, experiment: BaseExperiment, token: str,
Expand Down Expand Up @@ -366,12 +366,18 @@ def __init__(self, experiment: BaseExperiment, token: str,
:class:`BaseMessenger`
"""
super().__init__(experiment, notify_epochs=notify_epochs)

# switch between different versions (with changed imports)
try:
from slackclient import SlackClient
except ImportError as e:
warnings.warn("Coudl not import `slackclient`. This package is not"
"included in the default depencies of delira!")
raise e
try:
from slack import WebClient as SlackClient
except ImportError as e:

warnings.warn("Could not import `slackclient`. This package is not"
"included in the default depencies of delira!")
raise e

self._client = SlackClient(token, **kwargs)
self._channel = channel
Expand Down
2 changes: 2 additions & 0 deletions scripts/ci/install_before_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ elif [[ "$BACKEND" == "TorchScript" ]]; then
pip install -r requirements/torch.txt
elif [[ "$BACKEND" == "Chainer" ]]; then
pip install -r requirements/chainer.txt
else
pip install slackclient==1.3.1
fi

pip install coverage;
Expand Down
46 changes: 0 additions & 46 deletions tests/utils.py

This file was deleted.

46 changes: 46 additions & 0 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from delira import get_backends
import os


def check_for_environment_variable(variable: str, value: str):
if variable not in os.environ or os.environ[variable] == value:
return True
return False


def check_for_backend(backend_name, environment_variable):
backend_installed = backend_name in get_backends()
backend_specified = check_for_environment_variable("BACKEND",
environment_variable)

return backend_installed and backend_specified


def check_for_torch_backend():
return check_for_backend("TORCH", "Torch")


def check_for_torchscript_backend():
return check_for_backend("TORCH", "TorchScript")


def check_for_tf_eager_backend():
return check_for_backend("TF", "TFEager")


def check_for_tf_graph_backend():
return check_for_backend("TF", "TFGraph")


def check_for_chainer_backend():
return check_for_backend("CHAINER", "Chainer")


def check_for_sklearn_backend():
return check_for_backend("SKLEARN", "Sklearn")


def check_for_no_backend():
# sklearn backend is always installed, so this check is mainly a check if
# installation was successfull and checks for environment variable
return check_for_backend("SKLEARN", "None")
34 changes: 34 additions & 0 deletions tests/utils/test_messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from ..training.backends.utils import DummyDataset

from . import check_for_no_backend

import unittest
import logging
import copy
Expand Down Expand Up @@ -269,29 +271,57 @@ def kfold_experiment(self, raise_error=False, expected_msg=None):
else:
self.assertEqual(cm.output, expected_msg)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_create_experiment(self):
self.create_experiment(self.msg_create_experiment)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_run_successful(self):
self.run_experiment(raise_error=False,
expected_msg=self.msg_run_successful)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_run_failed(self):
self.run_experiment(raise_error=True,
expected_msg=self.msg_run_failed)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_test_successful(self):
self.t_experiment(raise_error=False,
expected_msg=self.msg_test_successful)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_test_failed(self):
self.t_experiment(raise_error=True,
expected_msg=self.msg_test_failed)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_kfold_successful(self):
self.kfold_experiment(raise_error=False,
expected_msg=self.msg_kfold_successful)

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_kfold_failed(self):
self.kfold_experiment(raise_error=True,
expected_msg=self.msg_kfold_failed)
Expand All @@ -313,6 +343,10 @@ def setUp(self) -> None:
self.messenger_kwargs = {"notify_epochs": 1, "token": "dummyToken",
"channel": "dummyChannel"}

@unittest.skipUnless(
check_for_no_backend(),
"Test should only be executed "
"if no backend is installed")
def test_create_experiment(self):
with self.assertLogs(level='ERROR') as cm:
self.create_experiment(self.msg_create_experiment)
Expand Down

0 comments on commit b84c229

Please sign in to comment.