Skip to content

Instantly share code, notes, and snippets.

@whunter
Last active August 3, 2020 21:13
Show Gist options
  • Save whunter/ca6ab3bcce68fa5a5143f78919262e60 to your computer and use it in GitHub Desktop.
Save whunter/ca6ab3bcce68fa5a5143f78919262e60 to your computer and use it in GitHub Desktop.
import boto3
import json
import os
# Environment variables
region_name = "us-east-1"
test_table = "Collection-cg5wa4as3fgkdpyrghj5g2z2jq-leestest"
source_table_name = test_table
target_table_name = test_table
try:
dyndb = boto3.resource('dynamodb', region_name=region_name)
source_table = dyndb.Table(source_table_name)
target_table = dyndb.Table(target_table_name)
response = source_table.scan()
source_table_items = response['Items']
while response.get('LastEvaluatedKey', False):
response = source_table.scan(
ExclusiveStartKey=response['LastEvaluatedKey'])
source_table_items.extend(response['Items'])
except Exception as e:
print(f"An error occurred: {str(e)}")
raise e
def item_needs_updating(item):
ret_val = False
if(item['collection_category'] == "IAWA" and 'parent_collection' in item ):
ret_val = True
return ret_val
def get_top_level_parent(parent_id):
parent = None
while(parent_id):
parent = get_item_parent(parent_id)
if('parent_collection' in parent):
parent_id = parent['parent_collection'][0]
else:
parent_id = False
return parent
def get_item_parent(parent_id):
parent = source_table.get_item(Key={'id': parent_id})
return parent['Item']
def updated_item_title(item):
item_title_string = ""
# item_parent = get_top_level_parent(item['parent_collection'][0])
# parent_title = item_parent['title'].split(",")[0]
item_title_array = item['title'].split("||")
# for i in range(len(item_title_array)):
# if i > 0:
# current = item_title_array[i]
title_last = item_title_array[len(item_title_array) - 1]
title_last_array = title_last.split('_')
current = title_last_array[ len(title_last_array) - 1 ]
if current.find('Box') > -1:
item_title_string = current[current.find('Box'):].replace("Box", "Box ").strip()
elif current.find('Folder') > -1:
item_title_string = current[current.find('Folder'):].replace("Folder", "Folder ").strip()
return item_title_string
def lambda_handler(event, context):
for item in source_table_items:
try:
if(item_needs_updating(item)):
item_title = updated_item_title(item)
print(item['id'])
print(item['title'])
print(item_title)
print("----------------------------------------")
# response = target_table.update_item(
# Key={
# 'id': item['id'],
# },
# UpdateExpression="set title = :val",
# ExpressionAttributeValues={
# ':val': item_title
# },
# ReturnValues="UPDATED_NEW"
# )
desc = "Born in Berlin, Germany, King was an architect and professor who practiced in Germany, the United States, and the Bahamas. She graduated from the Hochschule fur Bildende Kunste (HBK) in 1962 with the title Diplom Architect, HBK Berlin. Upon graduating from HBK she worked for two years in the architectural office of Hilde Westrom, one of the few independently working women architects in Berlin. King went on to receive the Airlift Memorial Scholarship and Fulbright Travel Grant allowing her to study under Louis I. Kahn in his Master Studio at the University of Pennsylvania (1965-1967). Her experiences with Kahn greatly influenced her own style of teaching at the Pratt Institute School of Architecture (1969-1981) and other organizations. King and her husband, fellow architect Douglas King, were offered the opportunity to work as architects with the Government of the Bahamas in Nassau (1981-1985). There she primarily worked on projects for the Ministries of Health and Housing, including public rental units with a senior citizen complex, design for a high school library in Cooperstown, and as project architect and head construction supervisor for the Bahamas Nursing School. The years 1985-1991 saw King commuting between New York, Nassau, and Florida as she finished supervising the Bahamas Nursing School and partnered with her husband on a design-build company called Kingston Homes in West Palm Beach, Florida. Shortly after returning to New York full-time (1991) King began teaching at several New York institutions and public schools within the city (1993-2008). The collection consists of material created by King while a student and during her professional career."
response = target_table.update_item(
Key={
'id': item['id'],
},
UpdateExpression="set description = :val",
ExpressionAttributeValues={
':val': desc
},
ReturnValues="UPDATED_NEW"
)
except Exception as e:
print(f"An error occurred: {str(e)}")
raise e
return {
"statusCode": 200,
"body": json.dumps({
"message": "Table copied.",
}),
}
# {
# "Version": "2012-10-17",
# "Statement": [
# {
# "Effect": "Allow",
# "Action": [
# "dynamodb:DeleteItem",
# "dynamodb:GetItem",
# "dynamodb:PutItem",
# "dynamodb:Scan",
# "dynamodb:UpdateItem"
# ],
# "Resource": "arn:aws:dynamodb:us-east-1:{account}:table/{table_name}"
# }
# ]
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment