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

math.polygonal_graph   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 20
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A polygonal_graph_model() 0 21 1
A polygonal_graph_edges() 0 4 2
1
import networkx as nx
2
3
import graphinate
4
5
# import graphinate.modeling
6
from graphinate import GraphModel
7
8
9
def polygonal_graph_edges(edges_count: int):
10
    for i in range(1, edges_count):
11
        yield {'source': i, 'target': i + 1}
12
    yield {'source': edges_count, 'target': 1}
13
14
15
def polygonal_graph_model(name: str, number_of_sides: int) -> graphinate.GraphModel:
16
    """
17
    Create a polygonal graph model.
18
19
    Args:
20
        name (str): The Graph's name.
21
        number_of_sides (int): Number of sides in the polygon.
22
23
    Returns:
24
        GraphModel: A graph model representing a polygonal graph.
25
    """
26
27
    # Define GraphModel
28
    graph_model: GraphModel = graphinate.model(name)
29
30
    # Register edges supplier function
31
    @graph_model.edge()
32
    def edge():
33
        yield from polygonal_graph_edges(number_of_sides)
34
35
    return graph_model
36
37
38
model = polygonal_graph_model("Octagonal Graph", 8)
39
40
if __name__ == '__main__':
41
    # 1. Define Graph Builder
42
    builder = graphinate.builders.NetworkxBuilder(model=model)
43
44
    # Then
45
    # 2. Build the Graph object
46
    graph: nx.Graph = builder.build()
47
48
    # Then
49
    # 3. Option A - Output to console
50
    print(graph)
51
52
    # Or
53
    # 3. Option B - Output as a plot
54
    graphinate.materializers.plot(graph)
55