def _observe_objects(self, change):
        """ A change handler for the 'objects' list of the Include.

        If the object is initialized objects which are removed will be
        unparented and objects which are added will be reparented. Old
        objects will be destroyed if the 'destroy_old' flag is True.

        """
        # TODO clean this up
        if self.is_initialized:
            if change['type'] == 'update':
                oldvalue = change['oldvalue']
                newvalue = change['value']
                newset = set(newvalue)
                if self.destroy_old:
                    for obj in oldvalue:
                        if obj not in newset:
                            obj.destroy()
                else:
                    for obj in oldvalue:
                        if obj not in newset:
                            obj.set_parent(None)
                if newvalue:
                    self.parent.insert_children(self, newvalue)
            elif change['type'] == 'container':
                added = []
                removed = []
                op = change['operation']
                if op == '__delitem__':
                    item = change['item']
                    if isinstance(item, list):
                        removed.extend(item)
                    else:
                        removed.append(item)
                elif op == '__iadd__':
                    added.extend(change['items'])
                elif op == '__setitem__':
                    olditem = change['olditem']
                    newitem = change['newitem']
                    if isinstance(olditem, list):
                        removed.extend(olditem)
                    else:
                        removed.append(olditem)
                    if isinstance(newitem, list):
                        added.extend(newitem)
                    else:
                        added.append(newitem)
                elif op == 'append':
                    added.append(change['item'])
                elif op == 'extend':
                    added.extend(change['items'])
                elif op == 'insert':
                    added.append(change['item'])
                elif op == 'pop':
                    removed.append(change['item'])
                elif op == 'remove':
                    removed.append(change['item'])
                addset = set(added)
                if self.destroy_old:
                    for obj in removed:
                        if obj not in addset:
                            obj.destroy()
                else:
                    for obj in removed:
                        if obj not in addset:
                            obj.set_parent(None)
                self.parent.insert_children(self, change['value'])
dataset/ETHPy150Open ContinuumIO/ashiba/enaml/enaml/core/include.py/Include._observe_objects/original