Conditions | 17 |
Total Lines | 72 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
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 000_vlan_pool.aggregate_outdated_interfaces() 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 | import datetime |
||
129 | def aggregate_outdated_interfaces(mongo: Mongo): |
||
130 | """Aggregate outdated inteface details""" |
||
131 | db = mongo.client[mongo.db_name] |
||
132 | document_ids = set() |
||
133 | result = db.interface_details.aggregate( |
||
134 | [ |
||
135 | {"$sort": {"_id": 1}}, |
||
136 | {"$project": { |
||
137 | "_id": 0, |
||
138 | "id": 1, |
||
139 | "max_number": {"$max": "$available_vlans"}, # MAX deleted in 6.0 |
||
140 | "min_number": {"$min": "$available_vlans"}, # MIN deleted in 6.0 |
||
141 | "available_vlans": 1, |
||
142 | }} |
||
143 | ] |
||
144 | ) |
||
145 | |||
146 | messages = "" |
||
147 | for document in result: |
||
148 | document_ids.add(document["id"]) |
||
149 | if document.get("available_vlans") is None: |
||
150 | continue |
||
151 | document.pop("available_vlans") |
||
152 | messages += str(document) + "\n" |
||
153 | |||
154 | if messages != "": |
||
155 | print("Here are the outdated interfaces. 'available_vlans' have a massive" |
||
156 | " amount of items, minimum and maximum items will be shown only") |
||
157 | print(messages) |
||
158 | |||
159 | evc_documents = db.evcs.find({"archived": False}) |
||
160 | evc_intf = defaultdict(str) |
||
161 | evc_tags = defaultdict(set) |
||
162 | |||
163 | for evc in evc_documents: |
||
164 | tag_a = evc["uni_a"].get("tag") |
||
165 | if tag_a: |
||
166 | intf_id = evc["uni_a"]["interface_id"] |
||
167 | if tag_a["value"] in evc_tags[intf_id] and isinstance(tag_a["value"], int): |
||
168 | print(f"WARNING: Detected duplicated {tag_a['value']} TAG" |
||
169 | f" in EVCs {evc['id']} and {evc_intf[intf_id+str(tag_a['value'])]}" |
||
170 | f" in interface {intf_id}") |
||
171 | print() |
||
172 | evc_tags[intf_id].add(tag_a["value"]) |
||
173 | evc_intf[intf_id+str(tag_a["value"])] = evc["id"] |
||
174 | |||
175 | tag_z = evc["uni_z"].get("tag") |
||
176 | if tag_z: |
||
177 | intf_id = evc["uni_z"]["interface_id"] |
||
178 | if tag_z["value"] in evc_tags[intf_id] and isinstance(tag_z["value"], int): |
||
179 | print(f"WARNING: Detected duplicated {tag_z['value']} TAG" |
||
180 | f" in EVCs {evc['id']} and {evc_intf[intf_id+str(tag_z['value'])]}" |
||
181 | f" in interface {intf_id}") |
||
182 | print() |
||
183 | evc_tags[intf_id].add(tag_z["value"]) |
||
184 | evc_intf[intf_id+str(tag_z["value"])] = evc["id"] |
||
185 | |||
186 | for id_ in document_ids: |
||
187 | evc_tags.pop(id_, None) |
||
188 | |||
189 | if evc_tags: |
||
190 | print("New documents are going to be created. From the next interfaces," |
||
191 | " these tags should be avoided") |
||
192 | |||
193 | for intf, avoid_tags in evc_tags.items(): |
||
194 | if intf in document_ids: |
||
195 | continue |
||
196 | aux = {"id": intf, "avoid_tags": avoid_tags} |
||
197 | print(aux) |
||
198 | |||
199 | if not evc_tags and messages == "": |
||
200 | print("There is nothing to update or add") |
||
201 | |||
217 |