Passed
Push — main ( 2bcd5c...05fcff )
by Eran
01:27
created

math.materializers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 29
dl 0
loc 62
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B materialize() 0 37 6
1
import functools
2
import json
3
from collections.abc import Callable, Mapping
4
from enum import Enum
5
from typing import Optional
6
7
from graphinate import GraphModel, GraphType, builders, graphql, plot
8
9
10
class Materializers(Enum):
11
    """Materializers Enum
12
13
    Attributes:
14
        D3Graph: create a D3 Graph and print it to stdout
15
        GraphQL: create a GraphQL Schema and serve it in a web server
16
        NetworkX: create a NetworkX Graph and plot+show it with matplotlib
17
        NetworkX_with_edge_labels: create a NetworkX Graph and plot+show it with matplotlib
18
    """
19
    D3Graph: tuple = (builders.D3Builder, lambda d: print(json.dumps(d, indent=2, default=str)))
20
    GraphQL: tuple = (builders.GraphQLBuilder, graphql)
21
    NetworkX: tuple = (builders.NetworkxBuilder, plot)
22
    NetworkX_with_edge_labels: tuple = (builders.NetworkxBuilder, functools.partial(plot, with_edge_labels=True))
23
24
25
def materialize(model: GraphModel,
26
                graph_type: GraphType = GraphType.Graph,
27
                default_node_attributes: Optional[Mapping] = None,
28
                builder: Optional[type[builders.Builder]] = None,
29
                builder_output_handler: Optional[Callable] = None,
30
                **kwargs):
31
    """
32
    Materialize a GraphModel using a Builder and an Actualizer
33
34
    Args:
35
        model: GraphModel - the model to be materialized
36
        graph_type: GraphType - the type of graph to be built.
37
                                Default is Graph.
38
        default_node_attributes: Mapping - A Mapping containing attributes that are added to all nodes.
39
        builder: Builder - the builder to be used to build the graph.
40
        builder_output_handler: function that will consume the resulting built graph and
41
                    outputs it (e.g., display, serve, print, etc.).
42
        **kwargs:
43
44
45
    Returns:
46
        None
47
    """
48
    if builder is None and builder_output_handler is None:
49
        raise ValueError("Missing: builder, builder_output_handler")
50
51
    if builder:
52
        graph = builders.build(builder,
53
                               model,
54
                               graph_type,
55
                               default_node_attributes=default_node_attributes,
56
                               **kwargs)
57
58
        if builder_output_handler and callable(builder_output_handler):
59
            builder_output_handler(graph, **kwargs)
60
        else:
61
            print(graph)
62