| Conditions | 12 | 
| Total Lines | 67 | 
| Code Lines | 51 | 
| 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 vlan_pool.update_database() 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  | 
            ||
| 55 | def update_database(mongo: Mongo):  | 
            ||
| 56 | """Update database"""  | 
            ||
| 57 | db = mongo.client[mongo.db_name]  | 
            ||
| 58 | intf_documents = db.interface_details.find()  | 
            ||
| 59 |     evc_documents = db.evcs.find({"archived": False}) | 
            ||
| 60 | |||
| 61 | evc_intf = defaultdict(str)  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 62 | evc_tags = defaultdict(set)  | 
            ||
| 63 | |||
| 64 | for evc in evc_documents:  | 
            ||
| 65 |         tag_a = evc["uni_a"].get("tag") | 
            ||
| 66 | if tag_a:  | 
            ||
| 67 | intf_id = evc["uni_a"]["interface_id"]  | 
            ||
| 68 | if tag_a["value"] in evc_tags[intf_id] and isinstance(tag_a["value"], int):  | 
            ||
| 69 |                 print(f"Error: Detected duplicated {tag_a['value']} TAG" | 
            ||
| 70 |                       f" in EVCs {evc['id']} and {evc_intf[intf_id+str(tag_a['value'])]}" | 
            ||
| 71 |                       f" in interface {intf_id}") | 
            ||
| 72 | sys.exit(1)  | 
            ||
| 73 | evc_tags[intf_id].add(tag_a["value"])  | 
            ||
| 74 | evc_intf[intf_id+str(tag_a["value"])] = evc["id"]  | 
            ||
| 75 | |||
| 76 |         tag_z = evc["uni_z"].get("tag") | 
            ||
| 77 | if tag_z:  | 
            ||
| 78 | intf_id = evc["uni_z"]["interface_id"]  | 
            ||
| 79 | if tag_z["value"] in evc_tags[intf_id] and isinstance(tag_z["value"], int):  | 
            ||
| 80 |                 print(f"Error: Detected duplicated {tag_z['value']} TAG" | 
            ||
| 81 |                       f" in EVCs {evc['id']} and {evc_intf[intf_id+str(tag_z['value'])]}" | 
            ||
| 82 |                       f" in interface {intf_id}") | 
            ||
| 83 | sys.exit(1)  | 
            ||
| 84 | evc_tags[intf_id].add(tag_z["value"])  | 
            ||
| 85 | evc_intf[intf_id+str(tag_z["value"])] = evc["id"]  | 
            ||
| 86 | |||
| 87 | intf_count = 0  | 
            ||
| 88 | for document in intf_documents:  | 
            ||
| 89 | avoid_tags = evc_tags.pop(document["id"], set())  | 
            ||
| 90 |         if document.get("available_vlans") is None: | 
            ||
| 91 | continue  | 
            ||
| 92 | ranges = get_range(document["available_vlans"], avoid_tags)  | 
            ||
| 93 | result = db.interface_details.update_one(  | 
            ||
| 94 |             {"id": document["id"]}, | 
            ||
| 95 |             { | 
            ||
| 96 | "$set":  | 
            ||
| 97 |                 { | 
            ||
| 98 |                     "available_tags": {"vlan": ranges}, | 
            ||
| 99 |                     "tag_ranges": {"vlan": DEFAULT_TAG_RANGES} | 
            ||
| 100 | },  | 
            ||
| 101 |                 "$unset": {"available_vlans": ""} | 
            ||
| 102 | }  | 
            ||
| 103 | )  | 
            ||
| 104 | intf_count += result.modified_count  | 
            ||
| 105 | |||
| 106 | evc_intf_count = 0  | 
            ||
| 107 | for intf_id, avoid_tags in evc_tags.items():  | 
            ||
| 108 | available_tags = generate_ranges(list(avoid_tags))  | 
            ||
| 109 | utc_now = datetime.datetime.utcnow()  | 
            ||
| 110 |         result = db.interface_details.insert_one({ | 
            ||
| 111 | "_id": intf_id,  | 
            ||
| 112 | "id": intf_id,  | 
            ||
| 113 | "inserted_at": utc_now,  | 
            ||
| 114 | "updated_at": utc_now,  | 
            ||
| 115 |             "available_tags": {"vlan": available_tags}, | 
            ||
| 116 |             "tag_ranges": {"vlan": DEFAULT_TAG_RANGES}, | 
            ||
| 117 | })  | 
            ||
| 118 | if result:  | 
            ||
| 119 | evc_intf_count += 1  | 
            ||
| 120 | |||
| 121 |     print(f"{intf_count} documents modified. {evc_intf_count} documents inserted") | 
            ||
| 122 | |||
| 211 |