/
run_loggers.py
97 lines (74 loc) · 3.02 KB
/
run_loggers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from abc import abstractmethod, ABCMeta
from pypads import logger
from pypads.app.injections.base_logger import FunctionHolder
from pypads.app.misc.mixins import DefensiveCallableMixin, IntermediateCallableMixin, TimedCallableMixin, \
DependencyMixin, OrderMixin, ConfigurableCallableMixin
# Default init_run fns
from pypads.utils.util import inheritors
class BaseDefensiveCallable(DefensiveCallableMixin):
"""
Defensive callable ignoring errors but printing a warning to console.
"""
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, *args, message=None, **kwargs):
self._message = message if message else "Couldn't execute {}, because of exception: {}"
super().__init__(*args, **kwargs)
def _handle_error(self, *args, ctx, _pypads_env, error, **kwargs):
logger.warning(self._message.format(str(self.__name__), str(error)))
class BaseRunLogger(BaseDefensiveCallable, IntermediateCallableMixin, FunctionHolder, TimedCallableMixin,
DependencyMixin, OrderMixin, ConfigurableCallableMixin):
"""
Base run logger. This function is to be called after or before a mlflow run.
"""
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, *args, fn=None, **kwargs):
super().__init__(*args, fn=fn, **kwargs)
if self._fn is None:
self._fn = self._call
@property
def __name__(self):
if hasattr(self, "_fn") and self._fn is not self._call:
return self._fn.__name__
else:
return self.__class__.__name__
@abstractmethod
def _call(self, pads, *args, **kwargs):
"""
Function where to add you custom code to execute before starting or ending the run.
:param pads: the current instance of PyPads.
"""
return NotImplementedError()
class PreRunFunction(BaseRunLogger, metaclass=ABCMeta):
"""
This class should be used to define new pre run functions
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __real_call__(self, *args, **kwargs):
from pypads.app.pypads import get_current_pads
logger.debug("Called pre run function " + str(self))
return super().__real_call__(get_current_pads(), *args, **kwargs)
class PostRunFunction(BaseRunLogger, metaclass=ABCMeta):
"""
This class should be used to define new post run functions
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __real_call__(self, *args, **kwargs):
from pypads.app.pypads import get_current_pads
logger.debug("Called post run function " + str(self))
return super().__real_call__(get_current_pads(), *args, **kwargs)
def pre_run_functions():
"""
Find all pre run functions defined in our imported context.
:return:
"""
return inheritors(PreRunFunction)
def post_run_functions():
"""
Find all post run functions defined in our imported context.
:return:
"""
return inheritors(PostRunFunction)