def upsert(self, row, keys, ensure=None, types={}):
        """
        An UPSERT is a smart combination of insert and update.

        If rows with matching ``keys`` exist they will be updated, otherwise a
        new row is inserted in the table.
        ::

            data = dict(id=10, title='I am a banana!')
            table.upsert(data, ['id'])
        """
        # check whether keys arg is a string and format as a list
        if not isinstance(keys, (list, tuple)):
            keys = [keys]
        self._check_dropped()

        ensure = self.database.ensure_schema if ensure is None else ensure
        if ensure:
            self.create_index(keys)

        filters = {}
        for key in keys:
            filters[key] = row.get(key)

        res = self.find_one(**filters)
        if res is not None:
            row_count = self.update(row, keys, ensure=ensure, types=types)
            if row_count == 0:
                return False
            elif row_count == 1:
                try:
                    return res['id']
                except __HOLE__:
                    return True
            else:
                return True
        else:
            return self.insert(row, ensure=ensure, types=types)
dataset/ETHPy150Open pudo/dataset/dataset/persistence/table.py/Table.upsert