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

Commit 7586390

Browse files
authored
Merge pull request #162 from delira-dev/refactor_init
Refactor __init__.py
2 parents b5ad5b4 + eb354c2 commit 7586390

File tree

4 files changed

+129
-118
lines changed

4 files changed

+129
-118
lines changed

delira/__init__.py

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,13 @@
1+
from delira._debug_mode import get_current_debug_mode, switch_debug_mode, \
2+
set_debug_mode
3+
from delira._backends import get_backends
4+
15
from ._version import get_versions
26
import json
37
import os
48
import warnings
59
warnings.simplefilter('default', DeprecationWarning)
610
warnings.simplefilter('ignore', ImportWarning)
711

8-
# to register new possible backends, they have to be added to this list.
9-
# each backend should consist of a tuple of length 2 with the first entry
10-
# being the package import name and the second being the backend abbreviation.
11-
# E.g. TensorFlow's package is named 'tensorflow' but if the package is found,
12-
# it will be considered as 'tf' later on
13-
__POSSIBLE_BACKENDS = [("torch", "torch"), ("tensorflow", "tf")]
14-
__BACKENDS = []
15-
16-
__DEBUG_MODE = False
17-
18-
19-
def _determine_backends():
20-
21-
_config_file = __file__.replace("__init__.py", ".delira")
22-
# look for config file to determine backend
23-
# if file exists: load config into environment variables
24-
25-
if not os.path.isfile(_config_file):
26-
_backends = {}
27-
# try to import all possible backends to determine valid backends
28-
29-
import importlib
30-
for curr_backend in __POSSIBLE_BACKENDS:
31-
try:
32-
assert len(curr_backend) == 2
33-
assert all([isinstance(_tmp, str) for _tmp in curr_backend]), \
34-
"All entries in current backend must be strings"
35-
36-
# check if backend can be imported
37-
bcknd = importlib.util.find_spec(curr_backend[0])
38-
39-
if bcknd is not None:
40-
_backends[curr_backend[1]] = True
41-
else:
42-
_backends[curr_backend[1]] = False
43-
del bcknd
44-
45-
except ValueError:
46-
_backends[curr_backend[1]] = False
47-
48-
with open(_config_file, "w") as f:
49-
json.dump({"version": __version__, "backend": _backends},
50-
f, sort_keys=True, indent=4)
51-
52-
del _backends
53-
54-
# set values from config file to variable
55-
with open(_config_file) as f:
56-
_config_dict = json.load(f)
57-
for key, val in _config_dict.pop("backend").items():
58-
if val:
59-
__BACKENDS.append(key.upper())
60-
del _config_dict
61-
62-
del _config_file
63-
64-
65-
def get_backends():
66-
"""
67-
Return List of currently available backends
68-
69-
Returns
70-
-------
71-
list
72-
list of strings containing the currently installed backends
73-
74-
"""
75-
76-
if not __BACKENDS:
77-
_determine_backends()
78-
return __BACKENDS
79-
80-
81-
# Functions to get and set the internal __DEBUG_MODE variable. This variable
82-
# currently only defines whether to use multiprocessing or not. At the moment
83-
# this is only used inside the BaseDataManager, which either returns a
84-
# MultiThreadedAugmenter or a SingleThreadedAugmenter depending on the current
85-
# debug mode.
86-
# All other functions using multiprocessing should be aware of this and
87-
# implement a functionality without multiprocessing
88-
# (even if this slows down things a lot!).
89-
90-
def get_current_debug_mode():
91-
"""
92-
Getter function for the current debug mode
93-
94-
Returns
95-
-------
96-
bool
97-
current debug mode
98-
99-
"""
100-
return __DEBUG_MODE
101-
102-
103-
def switch_debug_mode():
104-
"""
105-
Alternates the current debug mode
106-
107-
"""
108-
set_debug_mode(not get_current_debug_mode())
109-
110-
111-
def set_debug_mode(mode: bool):
112-
"""
113-
Sets a new debug mode
114-
115-
Parameters
116-
----------
117-
mode : bool
118-
the new debug mode
119-
120-
"""
121-
global __DEBUG_MODE
122-
__DEBUG_MODE = mode
123-
124-
12512
__version__ = get_versions()['version']
12613
del get_versions

delira/_backends.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import os
2+
import json
3+
from delira._version import get_versions as _get_versions
4+
5+
# to register new possible backends, they have to be added to this list.
6+
# each backend should consist of a tuple of length 2 with the first entry
7+
# being the package import name and the second being the backend abbreviation.
8+
# E.g. TensorFlow's package is named 'tensorflow' but if the package is found,
9+
# it will be considered as 'tf' later on
10+
__POSSIBLE_BACKENDS = (("torch", "torch"), ("tensorflow", "tf"))
11+
__BACKENDS = ()
12+
13+
14+
def _determine_backends():
15+
"""
16+
Internal Helper Function to determine the currently valid backends by
17+
trying to import them. The valid backends are not returned, but appended
18+
to the global ``__BACKENDS`` variable
19+
20+
"""
21+
22+
_config_file = __file__.replace("_backends.py", ".delira")
23+
# look for config file to determine backend
24+
# if file exists: load config into environment variables
25+
26+
if not os.path.isfile(_config_file):
27+
_backends = {}
28+
# try to import all possible backends to determine valid backends
29+
30+
import importlib
31+
for curr_backend in __POSSIBLE_BACKENDS:
32+
try:
33+
assert len(curr_backend) == 2
34+
assert all([isinstance(_tmp, str) for _tmp in curr_backend]), \
35+
"All entries in current backend must be strings"
36+
37+
# check if backend can be imported
38+
bcknd = importlib.util.find_spec(curr_backend[0])
39+
40+
if bcknd is not None:
41+
_backends[curr_backend[1]] = True
42+
else:
43+
_backends[curr_backend[1]] = False
44+
del bcknd
45+
46+
except ValueError:
47+
_backends[curr_backend[1]] = False
48+
49+
with open(_config_file, "w") as f:
50+
json.dump({"version": _get_versions()['version'],
51+
"backend": _backends},
52+
f, sort_keys=True, indent=4)
53+
54+
del _backends
55+
56+
# set values from config file to variable and empty Backend-List before
57+
global __BACKENDS
58+
__BACKENDS = []
59+
with open(_config_file) as f:
60+
_config_dict = json.load(f)
61+
for key, val in _config_dict.pop("backend").items():
62+
if val:
63+
__BACKENDS.append(key.upper())
64+
del _config_dict
65+
66+
del _config_file
67+
68+
# make __BACKENDS non mutable
69+
__BACKENDS = tuple(__BACKENDS)
70+
71+
72+
def get_backends():
73+
"""
74+
Return List of currently available backends
75+
76+
Returns
77+
-------
78+
list
79+
list of strings containing the currently installed backends
80+
"""
81+
global __BACKENDS
82+
83+
if not __BACKENDS:
84+
_determine_backends()
85+
return __BACKENDS

delira/_debug_mode.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
__DEBUG_MODE = False
2+
3+
# Functions to get and set the internal __DEBUG_MODE variable. This variable
4+
# currently only defines whether to use multiprocessing or not. At the moment
5+
# this is only used inside the BaseDataManager, which either returns a
6+
# MultiThreadedAugmenter or a SingleThreadedAugmenter depending on the current
7+
# debug mode.
8+
# All other functions using multiprocessing should be aware of this and
9+
# implement a functionality without multiprocessing
10+
# (even if this slows down things a lot!).
11+
12+
13+
def get_current_debug_mode():
14+
"""
15+
Getter function for the current debug mode
16+
Returns
17+
-------
18+
bool
19+
current debug mode
20+
"""
21+
return __DEBUG_MODE
22+
23+
24+
def switch_debug_mode():
25+
"""
26+
Alternates the current debug mode
27+
"""
28+
set_debug_mode(not get_current_debug_mode())
29+
30+
31+
def set_debug_mode(mode: bool):
32+
"""
33+
Sets a new debug mode
34+
Parameters
35+
----------
36+
mode : bool
37+
the new debug mode
38+
"""
39+
global __DEBUG_MODE
40+
__DEBUG_MODE = mode

delira/_version.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# This file helps to compute a version number in source trees obtained from
32
# git-archive tarball (such as those provided by githubs download-from-tag
43
# feature). Distribution tarballs (built by setup.py sdist) and build

0 commit comments

Comments
 (0)