| Conditions | 5 |
| Total Lines | 68 |
| Code Lines | 26 |
| Lines | 68 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | # -*- coding: utf-8 -*- |
||
| 48 | View Code Duplication | def draw_graph( |
|
|
|
|||
| 49 | grph, |
||
| 50 | edge_labels=True, |
||
| 51 | node_color="#AFAFAF", |
||
| 52 | edge_color="#CFCFCF", |
||
| 53 | plot=True, |
||
| 54 | node_size=2000, |
||
| 55 | with_labels=True, |
||
| 56 | arrows=True, |
||
| 57 | layout="neato", |
||
| 58 | ): |
||
| 59 | """ |
||
| 60 | Draw a graph. This function will be removed in future versions. |
||
| 61 | |||
| 62 | Parameters |
||
| 63 | ---------- |
||
| 64 | grph : networkxGraph |
||
| 65 | A graph to draw. |
||
| 66 | edge_labels : boolean |
||
| 67 | Use nominal values of flow as edge label |
||
| 68 | node_color : dict or string |
||
| 69 | Hex color code oder matplotlib color for each node. If string, all |
||
| 70 | colors are the same. |
||
| 71 | |||
| 72 | edge_color : string |
||
| 73 | Hex color code oder matplotlib color for edge color. |
||
| 74 | |||
| 75 | plot : boolean |
||
| 76 | Show matplotlib plot. |
||
| 77 | |||
| 78 | node_size : integer |
||
| 79 | Size of nodes. |
||
| 80 | |||
| 81 | with_labels : boolean |
||
| 82 | Draw node labels. |
||
| 83 | |||
| 84 | arrows : boolean |
||
| 85 | Draw arrows on directed edges. Works only if an optimization_model has |
||
| 86 | been passed. |
||
| 87 | layout : string |
||
| 88 | networkx graph layout, one of: neato, dot, twopi, circo, fdp, sfdp. |
||
| 89 | """ |
||
| 90 | if isinstance(node_color, dict): |
||
| 91 | node_color = [node_color.get(g, "#AFAFAF") for g in grph.nodes()] |
||
| 92 | |||
| 93 | # set drawing options |
||
| 94 | options = { |
||
| 95 | "prog": "dot", |
||
| 96 | "with_labels": with_labels, |
||
| 97 | "node_color": node_color, |
||
| 98 | "edge_color": edge_color, |
||
| 99 | "node_size": node_size, |
||
| 100 | "arrows": arrows, |
||
| 101 | } |
||
| 102 | |||
| 103 | # draw graph |
||
| 104 | pos = nx.drawing.nx_agraph.graphviz_layout(grph, prog=layout) |
||
| 105 | |||
| 106 | nx.draw(grph, pos=pos, **options) |
||
| 107 | |||
| 108 | # add edge labels for all edges |
||
| 109 | if edge_labels is True and plt: |
||
| 110 | labels = nx.get_edge_attributes(grph, "weight") |
||
| 111 | nx.draw_networkx_edge_labels(grph, pos=pos, edge_labels=labels) |
||
| 112 | |||
| 113 | # show output |
||
| 114 | if plot is True: |
||
| 115 | plt.show() |
||
| 116 | |||
| 192 |