Passed
Push — main ( 926b62...785ef6 )
by Eran
01:21
created

polygonal_graph.polygonal_graph_edges()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import graphinate
2
import graphinate.modeling
3
import networkx as nx
4
5
6
def polygonal_graph_edges(edges_count: int):
7
    for i in range(1, edges_count):
8
        yield {'source': i, 'target': i + 1}
9
    yield {'source': edges_count, 'target': 1}
10
11
12
def polygonal_graph_model(name: str, number_of_sides: int) -> graphinate.GraphModel:
13
    """
14
    Create a polygonal graph model.
15
16
    Args:
17
        name (str): The Graph's name.
18
        number_of_sides (int): Number of sides in the polygon.
19
20
    Returns:
21
        GraphModel: A graph model representing a polygonal graph.
22
    """
23
24
    # Define GraphModel
25
    graph_model = graphinate.model(name)
26
27
    # Register edges supplier function
28
    @graph_model.edge()
29
    def edge():
30
        yield from polygonal_graph_edges(number_of_sides)
31
32
    return graph_model
33
34
35
model = polygonal_graph_model("Octagonal Graph", 8)
36
37 View Code Duplication
if __name__ == '__main__':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
38
    use_materialize = True
39
40
    if use_materialize:
41
        # Materialize the GraphModel
42
        graphinate.materialize(
43
            model,
44
            builder=graphinate.builders.GraphQLBuilder,
45
            actualizer=graphinate.graphql
46
        )
47
48
    else:
49
        # Or
50
51
        # 1. Define Graph Builder
52
        builder = graphinate.builders.NetworkxBuilder(model=model)
53
54
        # Then
55
        # 2. Build the Graph object
56
        graph: nx.Graph = builder.build()
57
58
        # Then
59
        # 3. Option A - Output to console
60
        print(graph)
61
62
        # Or
63
        # 3. Option B - Output as a plot
64
        graphinate.materializers.plot(graph)
65