Commit 9ccf6ba8 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

[py] Implemented generic decorator that tests if a library is available

parent cdf7f1af
Pipeline #45393 failed with stage
in 4 minutes and 25 seconds
......@@ -9,6 +9,7 @@ import sys
import functools
from nose.plugins.skip import SkipTest
from bob.extension.download import download_and_unzip
import importlib
# based on: http://stackoverflow.com/questions/6796492/temporarily-redirect-stdout-stderr
class Quiet(object):
......@@ -66,21 +67,6 @@ def random_training_set_by_id(shape, count=50, minimum=0, maximum=1, seed=42):
return train_set
def grid_available(test):
"""Decorator to check if the gridtk is present, before running the test"""
@functools.wraps(test)
def wrapper(*args, **kwargs):
try:
import gridtk
return test(*args, **kwargs)
except ImportError as e:
raise SkipTest("Skipping test since gridtk is not available: %s" % e)
return wrapper
def db_available(dbname):
"""Decorator that checks if a given bob.db database is available.
This is a double-indirect decorator, see http://thecodeship.com/patterns/guide-to-python-function-decorators"""
......@@ -149,31 +135,19 @@ def atnt_database_directory():
return atnt_downloaded_directory
def mxnet_available(test):
def is_library_available(library):
"""Decorator to check if the mxnet is present, before running the test"""
@functools.wraps(test)
def wrapper(*args, **kwargs):
try:
import mxnet
return test(*args, **kwargs)
except ImportError as e:
raise SkipTest("Skipping test since `mxnet` is not available: %s" % e)
return wrapper
def tensorflow_available(test):
"""Decorator to check if the mxnet is present, before running the test"""
def _is_library_available(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
try:
importlib.import_module(library)
@functools.wraps(test)
def wrapper(*args, **kwargs):
try:
import tensorflow
return function(*args, **kwargs)
except ImportError as e:
raise SkipTest("Skipping test since `mxnet` is not available: %s" % e)
return test(*args, **kwargs)
except ImportError as e:
raise SkipTest("Skipping test since `mxnet` is not available: %s" % e)
return wrapper
return wrapper
return _is_library_available
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment