MoviesItemOps06.py - AWS Code Sample

MoviesItemOps06.py

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use a condition to control whether an item is deleted from an Amazon DynamoDB table that stores movies. The item is deleted only when its rating is below a specified threshold. Otherwise, the delete operation is rejected and an error is raised. """ from decimal import Decimal from pprint import pprint import boto3 from botocore.exceptions import ClientError def delete_underrated_movie(title, year, rating, dynamodb=None): if not dynamodb: dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000") table = dynamodb.Table('Movies') try: response = table.delete_item( Key={ 'year': year, 'title': title }, ConditionExpression="info.rating <= :val", ExpressionAttributeValues={ ":val": Decimal(rating) } ) except ClientError as e: if e.response['Error']['Code'] == "ConditionalCheckFailedException": print(e.response['Error']['Message']) else: raise else: return response if __name__ == '__main__': print("Attempting a conditional delete...") delete_response = delete_underrated_movie("The Big New Movie", 2015, 5) if delete_response: print("Delete movie succeeded:") pprint(delete_response, sort_dicts=False)