-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhandler.py
More file actions
60 lines (48 loc) · 1.68 KB
/
handler.py
File metadata and controls
60 lines (48 loc) · 1.68 KB
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
"""
Auxiliary handler methods for data summary extraction
"""
from typing import Any, Callable, Dict, List, Sequence
import networkx as nx
from visions import VisionsTypeset
def compose(functions: Sequence[Callable]) -> Callable:
"""
Compose a sequence of functions.
:param functions: sequence of functions
:return: combined function applying all functions in order.
"""
def composed_function(*args) -> List[Any]:
result = args # Start with the input arguments
for func in functions:
result = func(*result) if isinstance(result, tuple) else func(result)
return result # type: ignore
return composed_function # type: ignore
class Handler:
"""A generic handler
Allows any custom mapping between data types and functions
"""
def __init__(
self,
mapping: Dict[str, List[Callable]],
typeset: VisionsTypeset,
*args,
**kwargs
):
self.mapping = mapping
self.typeset = typeset
self._complete_dag()
def _complete_dag(self) -> None:
for from_type, to_type in nx.topological_sort(
nx.line_graph(self.typeset.base_graph)
):
self.mapping[str(to_type)] = (
self.mapping[str(from_type)] + self.mapping[str(to_type)]
)
def handle(self, dtype: str, *args, **kwargs) -> dict:
"""
Returns:
object: a tuple containing the config, the dataset series and the summary extracted
"""
funcs = self.mapping.get(dtype, [])
op = compose(funcs)
summary = op(*args)[-1]
return summary