| Total Complexity | 2 |
| Total Lines | 62 |
| Duplicated Lines | 45.16 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | import graphinate |
||
| 2 | import graphinate.modeling |
||
| 3 | import networkx as nx |
||
| 4 | |||
| 5 | N: int = 8 |
||
| 6 | |||
| 7 | |||
| 8 | def polygonal_graph_model(number_of_sides: int = N): |
||
| 9 | """ |
||
| 10 | Create a polygonal graph model. |
||
| 11 | |||
| 12 | Args: |
||
| 13 | number_of_sides (int): Number of sides in the polygon. Defaults to N. |
||
| 14 | |||
| 15 | Returns: |
||
| 16 | GraphModel: A graph model representing a polygonal graph. |
||
| 17 | """ |
||
| 18 | |||
| 19 | # Define GraphModel |
||
| 20 | graph_model = graphinate.model(name="Octagonal Graph") |
||
| 21 | |||
| 22 | # Register edges supplier function |
||
| 23 | @graph_model.edge() |
||
| 24 | def edge(): |
||
| 25 | for i in range(number_of_sides - 1): |
||
| 26 | yield {'source': i, 'target': i + 1} |
||
| 27 | yield {'source': number_of_sides - 1, 'target': 0} |
||
| 28 | |||
| 29 | return graph_model |
||
| 30 | |||
| 31 | |||
| 32 | model = polygonal_graph_model() |
||
| 33 | |||
| 34 | View Code Duplication | if __name__ == '__main__': |
|
|
|
|||
| 35 | use_materialize = True |
||
| 36 | |||
| 37 | if use_materialize: |
||
| 38 | # Materialize the GraphModel |
||
| 39 | graphinate.materialize( |
||
| 40 | model, |
||
| 41 | builder=graphinate.builders.GraphQLBuilder, |
||
| 42 | actualizer=graphinate.graphql |
||
| 43 | ) |
||
| 44 | |||
| 45 | else: |
||
| 46 | # Or |
||
| 47 | |||
| 48 | # 1. Define Graph Builder |
||
| 49 | builder = graphinate.builders.NetworkxBuilder(model=model) |
||
| 50 | |||
| 51 | # Then |
||
| 52 | # 2. Build the Graph object |
||
| 53 | graph: nx.Graph = builder.build() |
||
| 54 | |||
| 55 | # Then |
||
| 56 | # 3. Option A - Output to console |
||
| 57 | print(graph) |
||
| 58 | |||
| 59 | # Or |
||
| 60 | # 3. Option B - Output as a plot |
||
| 61 | graphinate.materializers.plot(graph) |
||
| 62 |