'Remove a single record, or the records in an iterable
Before starting deletion, test if all records are in the base
and don\'t have twice the same __id__
Return the number of deleted items'

def delete(self, removed):

    if isinstance(removed, dict):
        removed = [removed]
    else:
        removed = [r for r in removed]
    if (not removed):
        return 0
    _ids = [r['__id__'] for r in removed]
    _ids.sort()
    keys = set(self.records.keys())
    if (not set(_ids).issubset(keys)):
        missing = list(set(_ids).difference(keys))
        raise IndexError(('Delete    aborted.    Records    with    these    ids    not    found    in    the    base    :    %s' % str(missing)))
    for i in range((len(_ids) - 1)):
        if (_ids[i] == _ids[(i + 1)]):
            raise IndexError(('Delete    aborted.    Duplicate    id    :    %s' % _ids[i]))
    deleted = len(removed)
    while removed:
        r = removed.pop()
        _id = r['__id__']
        for indx in self.indices.keys():
            pos = (bisect.bisect(self.indices[indx][r[indx]], _id) - 1)
            del self.indices[indx][r[indx]][pos]
            if (not self.indices[indx][r[indx]]):
                del self.indices[indx][r[indx]]
        del self.records[_id]
    return deleted
