MoviesItemOps03.py - AWS Code Sample

MoviesItemOps03.py

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to update an item in an Amazon DynamoDB table that stores movies. The update is performed in a single step by using the update_item function. The updated item is identified by its primary and secondary keys and an update expression is used to describe the shape of the update data. """ from decimal import Decimal from pprint import pprint import boto3 def update_movie(title, year, rating, plot, actors, dynamodb=None): if not dynamodb: dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000") table = dynamodb.Table('Movies') response = table.update_item( Key={ 'year': year, 'title': title }, UpdateExpression="set info.rating=:r, info.plot=:p, info.actors=:a", ExpressionAttributeValues={ ':r': Decimal(rating), ':p': plot, ':a': actors }, ReturnValues="UPDATED_NEW" ) return response if __name__ == '__main__': update_response = update_movie( "The Big New Movie", 2015, 5.5, "Everything happens all at once.", ["Larry", "Moe", "Curly"]) print("Update movie succeeded:") pprint(update_response, sort_dicts=False)