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