To all,
I am a python beginner and I have a docker local docker environment of dynamoDB but I wanted to create a class that allows me to list tables or create tables for example on either my local docker dynamodb or aws dynamo db.
import boto3
class DDB():
def __init__(self):
print("Welcome to the DynamoDB py")
def ddbconnect(self,env):
self.env = env
if self.env == "l":
client = boto3.client('dynamodb',endpoint_url='http://localhost:8000')
return env
else:
client = boto3.client('dynamodb')
return env
def ddbcreateTable(self,tbname,pk,sk):
createResponse = client.create_table(
AttributeDefinitions=[
{
'AttributeName': pk,
'AttributeType': 'S',
},
{
'AttributeName': sk,
'AttributeType':'N'
}
],
KeySchema=[
{
'AttributeName': pk,
'KeyType':'HASH'
},
{
'AttributeName': sk,
'KeyType':'RANGE'
},
],
BillingMode = 'PAY_PER_REQUEST',
TableName=tbname
)
mydb=DDB()
env = input("enter L if it is a Local Dynamodb")
if env.lower() == "l":
mydb.ddbconnect(env)
while True:
prompt = input("Choices are (C)reate or (L)ist Table or (E)xit - L | C | E : " )
if prompt.lower() == "l":
env=input()
mydb.ddblist()
continue
elif prompt.lower() == "c":
tbname=input("Enter the table name: ")
pk=input("Enter the primary key: ")
sk=input("Enter the sort key w/ numberic attribute: ")
mydb.ddbcreateTable(tbname,pk,sk)
continue
elif prompt.lower() == "e" :
print("Exiting")
break
else:
print("Enter a valid selection")
continue
I am sure there is a more efficient way of writing but this is kind of the logic