Skip to main content

A Python dictionary-like interface for an Amazon DynamoDB table.

Project description

DynamoDB Mapping

https://img.shields.io/pypi/v/dynamodb_mapping.svg https://codecov.io/gh/mrtj/dynamodb-mapping/branch/main/graph/badge.svg?token=Y44R08UKEG Documentation Status

A Python dictionary-like interface for an Amazon DynamoDB table.

DynamoDBMapping is an alternative API for Amazon DynamoDB that implements the Python collections.abc.MutableMapping abstract base class, effectively allowing you to use a DynamoDB table as if it were a Python dictionary.

Getting started

To do anything useful with this module you need an Amazon Web Services account and an Amazon DynamoDB table. In every AWS account several DynamoDB tables can be created for free. Open an AWS account and create a DynamoDB table. You also need to create a IAM user and configure the access keys on your workstation. The easiest way to do so is to install and configure the AWS Command Line Interface. Once the AWS CLI works correctly, the AWS Python libraries (boto3) will correctly pick up the credentials.

Installation

Stable release

To install DynamoDB Mapping, run this command in your terminal:

$ pip install dynamodb_mapping

This is the preferred method to install DynamoDB Mapping, as it will always install the most recent stable release.

If you don’t have pip installed, this Python installation guide can guide you through the process.

From sources

The sources for DynamoDB Mapping can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://github.com/mrtj/dynamodb_mapping

Or download the tarball:

$ curl -OJL https://github.com/mrtj/dynamodb_mapping/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

Usage

Once the credentials are correctly configured, you can start reading and writing to your DynamoDB table with DynamoDBMapping as it was an ordinal Python dictionary:

from dynamodb_mapping import DynamoDBMapping

mapping = DynamoDBMapping(table_name="my_table")

# Create or modify an item:
mapping["my_item"] = {"description": "foo", "price": 123}
mapping["my_item"]["price"] = 456

# Iterate over all items:
for key, value in mapping.items():
    print(key, value)

# Get a single item:
print(mapping["my_item"])

# Number of items in table:
# (read bellow on how to get the estimated vs precise number of items)
print(len(mapping))

# Delete an item:
del mapping["my_item"]

All methods that iterate over the elements of the table do so in a lazy manner, in that the successive pages of the scan operation are queried only on demand. Examples of such operations include scan, iteration over keys, iteration over values, and iteration over items (key-value tuples). You should pay particular attention to certain patterns that fetch all items in the table, for example, calling list(mapping.values()). This call will execute an exhaustive scan on your table, which can be costly, and attempt to load all items into memory, which can be resource-demanding if your table is particularly large.

The __len__ implementation of this class returns a best-effort estimate of the number of items in the table using the TableDescription DynamoDB API. The number of items are updated at DynamoDB service side approximately once in every 6 hours. If you need the exact number of items currently in the table, you can use len(list(mapping.keys())). Note however that this will cause to run an exhaustive scan operation on your table.

Advanced configuration

You have the following options to configure the underlying boto3 session:

  • Automatic configuration: pass nothing to DynamoDBMapping initializer. This will prompt DynamoDBMapping to load the default boto3.Session object, which in turn will use the standard boto3 credentials chain to find AWS credentials (e.g., the ~/.aws/credentials file, environment variables, etc.).

  • Pass a preconfigured boto3.Session object

  • Pass aws_access_key_id and aws_secret_access_key as keyword arguments. Additionally, the optional aws_region and aws_profile arguments are also considered.

History

0.1.0 (2023-07-28)

  • First release on PyPI.

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

dynamodb_mapping-0.1.2.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

dynamodb_mapping-0.1.2-py2.py3-none-any.whl (11.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dynamodb_mapping-0.1.2.tar.gz.

File metadata

  • Download URL: dynamodb_mapping-0.1.2.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for dynamodb_mapping-0.1.2.tar.gz
Algorithm Hash digest
SHA256 46ff3e141c15a19a63c43601929b4f939bbe2f5ef774c5d626985e31a904b650
MD5 4fd1bfd9eee610fa3039d7d102a7d2b9
BLAKE2b-256 bef266c637c71e9a8e82866052cc14bde171a4b3bbe33afc90d947b7c333fbc1

See more details on using hashes here.

File details

Details for the file dynamodb_mapping-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dynamodb_mapping-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e185a3414934d284245a4a7224de0afa2cbd2b3da4dd131462912b5436b7d5ed
MD5 61d198a3de420f487e3632dcad345d7a
BLAKE2b-256 958ade67659a1b726ab6149fb5a5bd17024b1cc199358afca4afe6d23a8dfcd9

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