| Conditions | 12 |
| Total Lines | 62 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 22 |
| CRAP Score | 19.3771 |
| 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:
Complex classes like build.main.Main._validate_payload() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Main module of kytos/pathfinder Kytos Network Application.""" |
||
| 84 | 1 | def _validate_payload(self, data): |
|
| 85 | """Validate shortest_path v2/ POST endpoint.""" |
||
| 86 | 1 | if data.get("desired_links"): |
|
| 87 | 1 | if not isinstance(data["desired_links"], list): |
|
| 88 | raise BadRequest( |
||
| 89 | f"TypeError: desired_links is supposed to be a list." |
||
| 90 | f" type: {type(data['desired_links'])}" |
||
| 91 | ) |
||
| 92 | |||
| 93 | 1 | if data.get("undesired_links"): |
|
| 94 | if not isinstance(data["undesired_links"], list): |
||
| 95 | raise BadRequest( |
||
| 96 | f"TypeError: undesired_links is supposed to be a list." |
||
| 97 | f" type: {type(data['undesired_links'])}" |
||
| 98 | ) |
||
| 99 | |||
| 100 | 1 | parameter = data.get("parameter") |
|
| 101 | 1 | spf_attr = data.get("spf_attribute") |
|
| 102 | 1 | if not spf_attr: |
|
| 103 | 1 | spf_attr = parameter or "hop" |
|
| 104 | 1 | data["spf_attribute"] = spf_attr |
|
| 105 | |||
| 106 | 1 | if spf_attr not in self.graph.spf_edge_data_cbs: |
|
| 107 | raise BadRequest( |
||
| 108 | "Invalid 'spf_attribute'. Valid values: " |
||
| 109 | f"{', '.join(self.graph.spf_edge_data_cbs.keys())}" |
||
| 110 | ) |
||
| 111 | |||
| 112 | 1 | try: |
|
| 113 | 1 | data["spf_max_paths"] = max(int(data.get("spf_max_paths", 2)), 1) |
|
| 114 | except (TypeError, ValueError): |
||
| 115 | raise BadRequest( |
||
| 116 | f"spf_max_paths {data.get('spf_max_pahts')} must be an int" |
||
| 117 | ) |
||
| 118 | |||
| 119 | 1 | spf_max_path_cost = data.get("spf_max_path_cost") |
|
| 120 | 1 | if spf_max_path_cost: |
|
| 121 | try: |
||
| 122 | spf_max_path_cost = max(int(spf_max_path_cost), 1) |
||
| 123 | data["spf_max_path_cost"] = spf_max_path_cost |
||
| 124 | except (TypeError, ValueError): |
||
| 125 | raise BadRequest( |
||
| 126 | f"spf_max_path_cost {data.get('spf_max_path_cost')} must" |
||
| 127 | " be an int" |
||
| 128 | ) |
||
| 129 | |||
| 130 | 1 | data["mandatory_metrics"] = data.get("mandatory_metrics", {}) |
|
| 131 | 1 | data["flexible_metrics"] = data.get("flexible_metrics", {}) |
|
| 132 | |||
| 133 | 1 | try: |
|
| 134 | 1 | minimum_hits = data.get("minimum_flexible_hits") |
|
| 135 | 1 | if minimum_hits: |
|
| 136 | 1 | minimum_hits = min( |
|
| 137 | len(data["flexible_metrics"]), max(0, int(minimum_hits)) |
||
| 138 | ) |
||
| 139 | 1 | data["minimum_flexible_hits"] = minimum_hits |
|
| 140 | except (TypeError, ValueError): |
||
| 141 | raise BadRequest( |
||
| 142 | f"minimum_hits {data.get('minimum_flexible_hits')} must be an int" |
||
| 143 | ) |
||
| 144 | |||
| 145 | 1 | return data |
|
| 146 | |||
| 216 |