| Total Complexity | 3 |
| Total Lines | 65 |
| Duplicated Lines | 43.08 % |
| 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 | |||
| 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__': |
|
|
|
|||
| 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 |