Skip to main content

A collection of tools to try and make AWS boto3 more pythonic

Project description

awspytools

##DynamoDBDataStore

This is a helper python package to make DynamoDB easier to work with.

To get started, import DynamoDBDatastore from awspytools:

from awspytools import DynamoDBDataStore

Initialize a store using the Table Name:

store = DynamoDBDataStore(table_name='DynamoDB Table Name')

By default, we assume the following hash, sort and index keys:

hash_key = 'PK'
sort_key = 'SK'
GSI1_HASH_KEY = 'GSI1PK'
GSI1_SORT_KEY = 'GSI1SK'
GSI2_HASH_KEY = 'GSI2PK'
GSI2_SORT_KEY = 'GSI2SK'

To override these values, you may define your store as follows:

store = DynamoDBDataStore(
    table_name='DynamoDB Table Name',
    hash_key='custom_hash',
    sort_key='custom_sort',
    use_default_index_keys=False
)

You may add custom Index keys as follows:

store.add_index_key('custom gsi 1')

or you may add many index keys as follows:

store.add_index_keys(['custom gsi 1 hash', 'custom gsi 1 sort'])

To get a document, do the following:

result = store.get_document(index=('hash_key', 'sort_key'), return_index=True)

If you specify:

return_index=False

The response object will not have the hash /sort key or GSI keys specified in the response.

To save a document, simple do:

store.save_document(
    document={
        "number": 1,
        "hello": "world",
        "foo": ['bar', 'spam'],
        "foo": {
            "spam": "eggs"
        }
    },
    index=('hash_id', 'sort_id')
)

To get multiple documents, you need to provide a query. Note this paginates the result for you so all documents are returned!

query = {
		'ExpressionAttributeNames': {'#PK': 'PK'},
		'ExpressionAttributeValues': {':PK': {'S': 'hash_key'}},
		'KeyConditionExpression': '#PK = :PK'
	}
result = store.get_documents(query)

To delete a document: result = store.delete_document(index=('hash_key', 'sort_key'))

To perform a batch request, we have a wrapper for boto3's batch_write_item:

batch_request = store.batch_request(
    request_items=[ 
         { 
            "DeleteRequest": {...}, #As per boto3's parameter structure
            "PutRequest": {...} #As per boto3's document structure parameter structure
         }
      ]
)

To perform a transaction:

transaction_items = [
    {
        'Update': {
            'Key': {
                'PK': {'S': 'hash'},
                'SK': {'S': 'sort'},
            },
            'UpdateExpression': 'SET some_key = :some_value',
            'ExpressionAttributeValues': {
                ':some_value': {'S': 'hello world'},
            },
            'TableName': store.table_name
        }
    },
    {
        'Update': {
            'Key': {
                'PK': {'S': 'hash'},
                'SK': {'S': 'sort'},
            },
            'UpdateExpression': 'SET new_key = :new_value',
            'ExpressionAttributeValues': {
                ':new_value': {'S': 'hello world'},
            },
            'TableName': store.table_name
        }
    }
]
store.transaction_write(transaction_items, "some_unique_id")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

awspytools-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

awspytools-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file awspytools-0.1.1.tar.gz.

File metadata

  • Download URL: awspytools-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.13

File hashes

Hashes for awspytools-0.1.1.tar.gz
Algorithm Hash digest
SHA256 496a4e19943329781c74c3ff5260c6f3c4d658e015ee57882357505481bc9f2d
MD5 6d2a1c66386afae73f3f1d690b32372d
BLAKE2b-256 84fbfae418e6a13f335a6b31cb0bdcaf869be0bc78875052ac1c7d0e92ae9c3b

See more details on using hashes here.

File details

Details for the file awspytools-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: awspytools-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.13

File hashes

Hashes for awspytools-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a196cd312aa08d68d619b3a6dd410e9d0ffeed1c36a4d6ff67977cb1607856f
MD5 c0f9c74a007748c68293eff29a7cd01b
BLAKE2b-256 4a5c7f501a9ee501d1a541435cff76f29b587c7e6792eb11932602855e17c499

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page