Anonymous Anonymous - 13 days ago
96 0

No description

Python

treeview optimization

object_dict = {o.id: o for o in self.db_map.object_list().all()}
object_class_dict = {o.id: o for o in self.db_map.object_class_list().all()}
wide_relationship_class_dict = {x.id: x for x in self.db_map.wide_relationship_class_list()}
wide_relationship_dict = {x.id: x for x in self.db_map.wide_relationship_list()}
oc_rc_classes = {oc: [rc for rc in wide_relationship_class_dict.values() if str(oc) in rc.object_class_id_list.split(',')] for oc in object_class_dict.keys()}
tree_dict = {oc_id: {'children': {}, 'model': self.new_object_class_item(oc)} for oc_id, oc in object_class_dict.items()}
not_visited_objects = set(object_dict.keys())
for r in wide_relationship_dict.values():
    rc = wide_relationship_class_dict[r.class_id]
    oc_ids = map(int, rc.object_class_id_list.split(','))
    o_ids = map(int, r.object_id_list.split(','))
    for o_id, oc_id in zip(o_ids, oc_ids):
        o_id = int(o_id)
        if o_id not in tree_dict[oc_id]['children']:
            o = object_dict[int(o_id)]
            o_item = QStandardItem(o.name)
            o_item.setData('object', Qt.UserRole)
            o_item.setData(o._asdict(), Qt.UserRole + 1)
            o_item.setData(o.description, Qt.ToolTipRole)
            tree_dict[o.class_id]['model'].appendRow(o_item)
            rc_items = []
            for rc_ob in oc_rc_classes[o.class_id]:
                rc_items.append(self.new_relationship_class_item(rc_ob,[]))
            tree_dict[o.class_id]['children'][o.id] = {'children':{rc_o.id: rc_item for rc_o, rc_item in zip(oc_rc_classes[o.class_id], rc_items)},'model':o_item}
            o_item.appendRows(rc_items)
            not_visited_objects.discard(o.id)
        r_item = self.new_relationship_item(r)
        tree_dict[oc_id]['children'][o_id]['children'][rc.id].appendRow(r_item)
# add objects not visited
for o_id in not_visited_objects:
    o = object_dict[o_id]
    if o_id not in tree_dict[o.class_id]['children']:
        o_item = QStandardItem(o.name)
        o_item.setData('object', Qt.UserRole)
        o_item.setData(o._asdict(), Qt.UserRole + 1)
        o_item.setData(o.description, Qt.ToolTipRole)
        tree_dict[o.class_id]['model'].appendRow(o_item)
        rc_items = []
        for rc_ob in oc_rc_classes[o.class_id]:
            rc_items.append(self.new_relationship_class_item(rc,[]))
        tree_dict[o.class_id]['children'][o.id] = {'children':{rc_o.id: rc_item for rc_o, rc_item in zip(oc_rc_classes[o.class_id], rc_items)},'model':o_item}
        o_item.appendRows(rc_items)

root_item = QStandardItem(db_name)
 root_item.setData('root', Qt.UserRole)
icon = QIcon(":/icons/Spine_db_icon.png")
root_item.setData(icon, Qt.DecorationRole)
root_item.appendRows([t['model'] for t in tree_dict.values()])
self.root_item = root_item
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download