A simple Python wrapper to AWS Dynamodb
Project description
A minimalistic wrapper to AWS DynamoDB
Table of contents
- Installation
- Example
- Connect to DynamodDB
- Create a new table
- Get all table names
- Create a New Item
- Read an Item
- Increase an existing attribute value
- Update existing attribute in an item
- Add a new attribute in an item
- Add an attribute to the list
- Add an attribute to the string set
- Delete an attribute from the string set
- Delete an attribute from an item
- Read items by filter
- Delete a table
- Running tests
- Github Workflow Artifacts
- License
Installation
pip install LucidDynamodb
Note: Prerequisite for Python3 development
Example
Connect to DynamoDB
You can connect to DynamoDB by following any of these two ways.
- Using AWS config
- Using AWS secret key
Create a new table
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
table_schema = {
"TableName": "dev_jobs",
"KeySchema": [
{
"AttributeName": "company_name",
"KeyType": "HASH"
},
{
"AttributeName": "role_id",
"KeyType": "RANGE"
}
],
"AttributeDefinitions": [
{
"AttributeName": "company_name",
"AttributeType": "S"
},
{
"AttributeName": "role_id",
"AttributeType": "S"
}
],
"GlobalSecondaryIndexes": [],
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
if __name__ == "__main__":
db = DynamoDb()
table_creation_status = db.create_table(
TableName=table_schema.get("TableName"),
KeySchema=table_schema.get("KeySchema"),
AttributeDefinitions=table_schema.get("AttributeDefinitions"),
GlobalSecondaryIndexes=table_schema.get("GlobalSecondaryIndexes"),
ProvisionedThroughput=table_schema.get("ProvisionedThroughput")
)
if(table_creation_status == True):
logging.info("{} table created successfully".format(table_schema.get("TableName")))
else:
logging.error("{} table creation failed".format(table_schema.get("TableName")))
"""
dineshsonachalam@macbook examples % python 1-create-a-new-table.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:dev_jobs table created successfully
"""
Get all table names
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
table_names = db.read_all_table_names()
logging.info("Table names: {}".format(table_names))
"""
dineshsonachalam@macbook examples % python 2-get-all-table-names.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Table names: ['dev_jobs', 'dev_test']
"""
Create a New Item
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_creation_status = db.create_item(
TableName="dev_jobs",
Item={
"company_name": "Google",
"role_id": "111",
"role": "Software Engineer 1",
"salary": "$1,50,531",
"locations": ["Mountain View, California", "Austin, Texas", "Chicago, IL"],
"yearly_hike_percent": 8,
"benefits": set(["Internet, Medical, Edu reimbursements",
"Health insurance",
"Travel reimbursements"
]),
"overall_review":{
"overall_rating" : "4/5",
"compensation_and_benefits": "3.9/5"
}
}
)
if(item_creation_status == True):
logging.info("Item created successfully")
else:
logging.warning("Item creation failed")
"""
dineshsonachalam@macbook examples % python 3-create-a-new-item.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Item created successfully
"""
Read an Item
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 4-read-an-item.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5'
},
'company_name': 'Google',
'role': 'Software Engineer 1',
'yearly_hike_percent': Decimal('8'),
'salary': '$1,50,531',
'benefits': {
'Travel reimbursements',
'Internet, Medical, Edu reimbursements',
'Health insurance'
}
}
"""
Increase an existing attribute value
from LucidDynamodb.Operations import DynamoDb
import os
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
increase_attribute_status = db.increase_attribute_value(
TableName='dev_jobs',
Key={
"company_name": "Google",
"role_id": "111"
},
AttributeName="yearly_hike_percent",
IncrementValue=5
)
if(increase_attribute_status==True):
logging.info("Attribute value increment completed")
else:
logging.warning("Attribute value increment failed")
item = db.read_item(
TableName='dev_jobs',
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 5-increase-an-existing-attribute-value.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Attribute value increment completed
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5'
},
'company_name': 'Google',
'role': 'Software Engineer 1',
'yearly_hike_percent': Decimal('13'),
'salary': '$1,50,531',
'benefits': {
'Internet, Medical, Edu reimbursements',
'Health insurance',
'Travel reimbursements'
}
}
"""
Update existing attribute in an item
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_update_status = db.update_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
},
AttributesToUpdate={
'role': 'Staff Software Engineer 2'
}
)
if(item_update_status == True):
logging.info("Update is successful")
else:
logging.warning("Update failed")
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 6-update-existing-attribute-in-an-item.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Update is successful
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5'
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'yearly_hike_percent': Decimal('13'),
'salary': '$1,50,531',
'benefits': {
'Internet, Medical, Edu reimbursements',
'Travel reimbursements',
'Health insurance'
}
}
"""
Add a new attribute in an item
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_update_status = db.update_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
},
AttributesToUpdate={
'overall_review.yearly_bonus_percent': 12
}
)
if(item_update_status == True):
logging.info("Update is successful")
else:
logging.warning("Update failed")
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 7-add-a-new-attribute-in-an-item.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Update is successful
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5',
'yearly_bonus_percent': Decimal('12')
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'yearly_hike_percent': Decimal('13'),
'salary': '$1,50,531',
'benefits': {
'Health insurance',
'Internet, Medical, Edu reimbursements',
'Travel reimbursements'
}
}
"""
Add an attribute to the list
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_update_status = db.update_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
},
AttributesToUpdate={
'locations': "Detroit, Michigan"
},
Operation="ADD_ATTRIBUTE_TO_LIST"
)
if(item_update_status == True):
logging.info("Update is successful")
else:
logging.warning("Update failed")
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 8-add-an-attribute-to-the-list.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Update is successful
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL', 'Detroit, Michigan'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5',
'yearly_bonus_percent': Decimal('12')
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'yearly_hike_percent': Decimal('13'),
'salary': '$1,50,531',
'benefits': {
'Travel reimbursements',
'Internet, Medical, Edu reimbursements',
'Health insurance'
}
}
"""
Add an attribute to the string set
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_update_status = db.update_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
},
AttributesToUpdate={
'benefits': "Free Food"
},
Operation="ADD_ATTRIBUTE_TO_STRING_SET"
)
if(item_update_status == True):
logging.info("Update is successful")
else:
logging.warning("Update failed")
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 9-add-an-attribute-to-the-string-set.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Update is successful
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL', 'Detroit, Michigan'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5',
'yearly_bonus_percent': Decimal('12')
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'yearly_hike_percent': Decimal('13'),
'salary': '$1,50,531',
'benefits': {
'Travel reimbursements',
'Internet, Medical, Edu reimbursements',
'Health insurance',
'Free Food'
}
}
"""
Delete an attribute from the string set
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_update_status = db.update_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
},
AttributesToUpdate={
'benefits': "Free Food"
},
Operation="DELETE_ATTRIBUTE_FROM_STRING_SET"
)
if(item_update_status == True):
logging.info("Update is successful")
else:
logging.warning("Update failed")
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 10-delete-an-attribute-from-the-string-set.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Update is successful
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL', 'Detroit, Michigan'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5',
'yearly_bonus_percent': Decimal('12')
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'yearly_hike_percent': Decimal('13'),
'salary': '$1,50,531',
'benefits': {
'Internet, Medical, Edu reimbursements',
'Travel reimbursements',
'Health insurance'
}
}
"""
Delete an attribute from an item
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
attribute_delete_status = db.delete_attribute(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
},
AttributeName="yearly_hike_percent")
if(attribute_delete_status == True):
logging.info("The attribute is deleted successfully")
else:
logging.warning("The attribute delete operation failed")
item = db.read_item(
TableName="dev_jobs",
Key={
"company_name": "Google",
"role_id": "111"
})
if(item != None):
logging.info("Item: {}".format(item))
else:
logging.warning("Item doesn't exist")
"""
dineshsonachalam@macbook examples % python 11-delete-an-attribute-from-an-item.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:The attribute is deleted successfully
INFO:root:Item: {
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL', 'Detroit, Michigan'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5',
'yearly_bonus_percent': Decimal('12')
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'salary': '$1,50,531',
'benefits': {
'Internet, Medical, Edu reimbursements',
'Travel reimbursements',
'Health insurance'
}
}
"""
Read items by filter
from LucidDynamodb.Operations import DynamoDb
import logging
from boto3.dynamodb.conditions import Key
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
item_creation_status = db.create_item(
TableName="dev_jobs",
Item={
"company_name": "Google",
"role_id": "112",
"role": "Software Architect",
"salary": "$4,80,000",
"locations": ["Mountain View, California"],
"yearly_hike_percent": 13,
"benefits": set(["Internet reimbursements"]),
"overall_review":{
"overall_rating" : "3/5",
"compensation_and_benefits": "4.2/5"
}
}
)
if(item_creation_status == True):
logging.info("Item created successfully")
else:
logging.warning("Item creation failed")
items = db.read_items_by_filter(
TableName='dev_jobs',
KeyConditionExpression=Key("company_name").eq("Google")
)
if(len(items)>0):
logging.info("Items: {}".format(items))
else:
logging.warning("Items doesn't exist")
"""
dineshsonachalam@macbook examples % python 12-read-items-by-filter.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Item created successfully
INFO:root:Items: [{
'locations': ['Mountain View, California', 'Austin, Texas', 'Chicago, IL', 'Detroit, Michigan'],
'role_id': '111',
'overall_review': {
'compensation_and_benefits': '3.9/5',
'overall_rating': '4/5',
'yearly_bonus_percent': Decimal('12')
},
'company_name': 'Google',
'role': 'Staff Software Engineer 2',
'salary': '$1,50,531',
'benefits': {
'Internet, Medical, Edu reimbursements',
'Travel reimbursements',
'Health insurance'
}
}, {
'locations': ['Mountain View, California'],
'role_id': '112',
'overall_review': {
'compensation_and_benefits': '4.2/5',
'overall_rating': '3/5'
},
'company_name': 'Google',
'role': 'Software Architect',
'yearly_hike_percent': Decimal('13'),
'salary': '$4,80,000',
'benefits': {
'Internet reimbursements'
}
}]
"""
Delete a table
from LucidDynamodb.Operations import DynamoDb
import logging
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
db = DynamoDb()
delete_table_status = db.delete_table(TableName='dev_jobs')
if(delete_table_status == True):
logging.info("Table deleted successfully")
else:
logging.warning("Table delete operation failed")
table_names = db.read_all_table_names()
logging.info("Table names: {}".format(table_names))
"""
dineshsonachalam@macbook examples % python 13-delete-a-table.py
INFO:botocore.credentials:Found credentials in environment variables.
INFO:root:Table deleted successfully
INFO:root:Table names: ['dev_test']
"""
Running Tests
To run tests, run the following command
pytest -s
Github Workflow Artifacts
Artifact | Workflow |
---|---|
dependency-graph | integration-tests |
module-dependencies-license-report | integration-tests |
Pytest-report | integration-tests |
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
LucidDynamodb-1.0.101.tar.gz
(12.6 kB
view hashes)
Built Distribution
Close
Hashes for LucidDynamodb-1.0.101-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e7432a468fce6d5eee6c28fbd1cb60ade6a40f429ad2ba40303bbc365a0e222 |
|
MD5 | 1cc627dfa924487243632f4d8ff91e2d |
|
BLAKE2b-256 | 4973bc0701d9b67aaa51aad57d3c69d30de62533088bc0e0bafbbb8b73a92dd1 |