def update_fields(self, record_id, fields): """Safely update a number of fields. 'fields' being a dictionary with path_tuple: value for only the fields we want to change the value of, where path_tuple is a tuple of fieldnames indicating the path to the possibly nested field we're interested in. """ while True: record = self.db[record_id] modified = False for path, value in fields.items(): if not isinstance(path, tuple): path = (path,) parent = record for field in path[:-1]: parent = parent.setdefault(field, {}) if parent.get(path[-1]) == value: continue modified = True parent[path[-1]] = value if modified: try: self.db[record.id] = record except ResourceConflict: logger.debug( "Update of record with id %s in database %s failed." % ( record_id, self.db.name)) continue break