Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bob
bob.bio.base
Commits
9ccf6ba8
Commit
9ccf6ba8
authored
Nov 11, 2020
by
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
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
bob/bio/base/test/utils.py
View file @
9ccf6ba8
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment