<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def insertItem(self, childItem, position=None, parentIndex=None):
 """ Inserts a childItem before row 'position' under the parent index. If position is None the child will be appended as the last child of the parent. Returns the index of the new inserted child. """

        if parentIndex is None:
            parentIndex=QtCore.QModelIndex()

        parentItem = self.getItem(parentIndex, altItem=self.invisibleRootItem)

        nChildren = parentItem.nChildren()
        if position is None:
            position = nChildren

        assert 0 <= position <= nChildren, \
            "position should be 0 < {} <= {}".format(position, nChildren)

        self.beginInsertRows(parentIndex, position, position)
        try:
            parentItem.insertChild(childItem, position)
        finally:
            self.endInsertRows()

        childIndex = self.index(position, 0, parentIndex)
        assert childIndex.isValid(), "Sanity check failed: childIndex not valid"
        return childIndex