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