Skip to content

Commit 5e6b70f

Browse files
author
Janos Tolgyesi
committed
Add usage info
1 parent 5ea45ec commit 5e6b70f

File tree

4 files changed

+92
-29
lines changed

4 files changed

+92
-29
lines changed

docs/dynamodb_mapping.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ dynamodb\_mapping.dynamodb\_mapping module
1212
:undoc-members:
1313
:show-inheritance:
1414

15-
dynamodb\_mapping.utils module
16-
------------------------------
17-
18-
.. automodule:: dynamodb_mapping.utils
19-
:members:
20-
:undoc-members:
21-
:show-inheritance:
22-
2315
Module contents
2416
---------------
2517

docs/usage.rst

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,82 @@
22
Usage
33
=====
44

5-
To use DynamoDB Mapping in a project::
5+
DynamoDBMapping is an alternative API for `Amazon DynamoDB`_ that implements the Python
6+
``collections.abc.MutableMapping`` abstract base class, effectively allowing you to use a DynamoDB
7+
table as if it were a Python dictionary.
8+
9+
.. _Amazon DynamoDB: https://aws.amazon.com/dynamodb/
10+
11+
12+
Getting started
13+
---------------
14+
15+
To do anything useful with this module you need an Amazon Web Services account and an Amazon
16+
DynamoDB table. In every AWS account several DynamoDB tables can be created for free. Open
17+
an `AWS account`_ and `create a DynamoDB table`_. You also need to create a IAM user and configure
18+
the access keys on your workstation. The easiest way to do so is to install and configure the
19+
`AWS Command Line Interface`_. Once the AWS CLI works correctly, the AWS Python libraries (boto3)
20+
will correctly pick up the credentials.
21+
22+
.. _AWS account: https://aws.amazon.com/free/
23+
.. _create a DynamoDB table: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html
24+
.. _AWS Command Line Interface: https://docs.aws.amazon.com/cli/index.html
25+
26+
27+
Usage
28+
-----
29+
30+
Once the credentials are correctly configured, you can start reading and writing to your DynamoDB
31+
table with DynamoDBMapping as it was an ordinal Python dictionary::
32+
33+
from dynamodb_mapping import DynamoDBMapping
34+
35+
mapping = DynamoDBMapping(table_name="my_table")
36+
37+
# Create or modify an item:
38+
mapping["my_item"] = {"description": "foo", "price": 123}
39+
mapping["my_item"]["price"] = 456
40+
41+
# Iterate over all items:
42+
for key, value in mapping.items():
43+
print(key, value)
44+
45+
# Get a single item:
46+
print(mapping["my_item"])
47+
48+
# Number of items in table:
49+
# (read bellow on how to get the estimated vs precise number of items)
50+
print(len(mapping))
51+
52+
# Delete an item:
53+
del mapping["my_item"]
54+
55+
56+
All methods that iterate over the elements of the table do so in a lazy manner, in that the
57+
successive pages of the scan operation are queried only on demand. Examples of such operations
58+
include scan, iteration over keys, iteration over values, and iteration over items (key-value
59+
tuples). You should pay particular attention to certain patterns that fetch all items in the table,
60+
for example, calling ``list(mapping.values())``. This call will execute an exhaustive scan on your
61+
table, which can be costly, and attempt to load all items into memory, which can be
62+
resource-demanding if your table is particularly large.
63+
64+
The ``__len__`` implementation of this class returns a best-effort estimate of the number of items
65+
in the table using the TableDescription DynamoDB API. The number of items are updated at DynamoDB
66+
service side approximately once in every 6 hours. If you need the exact number of items currently in
67+
the table, you can use ``len(list(mapping.keys()))``. Note however that this will cause to run an
68+
exhaustive scan operation on your table.
69+
70+
71+
Advanced configuration
72+
----------------------
73+
74+
You have the following options to configure the underlying boto3 session:
75+
76+
- Automatic configuration: pass nothing to DynamoDBMapping initializer. This will prompt
77+
DynamoDBMapping to load the default ``boto3.Session`` object, which in turn will use the
78+
standard boto3 credentials chain to find AWS credentials (e.g., the ``~/.aws/credentials``
79+
file, environment variables, etc.).
80+
- Pass a preconfigured ``boto3.Session`` object
81+
- Pass ``aws_access_key_id`` and ``aws_secret_access_key`` as keyword arguments. Additionally,
82+
the optional ``aws_region`` and ``aws_profile`` arguments are also considered.
683

7-
import dynamodb_mapping

dynamodb_mapping/dynamodb_mapping.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import boto3
1212
from boto3.dynamodb.types import Binary
1313

14-
from .utils import boto3_session_from_config
15-
1614
try:
1715
import mypy_boto3_dynamodb
1816
DynamoDBTable = mypy_boto3_dynamodb.service_resource.Table
@@ -59,6 +57,17 @@
5957

6058
logger = logging.getLogger(__name__)
6159

60+
def _boto3_session_from_config(config: Dict[str, Any]) -> Optional[boto3.Session]:
61+
if "aws_access_key_id" in config and "aws_secret_access_key" in config:
62+
return boto3.Session(
63+
aws_access_key_id=config["aws_access_key_id"],
64+
aws_secret_access_key=config["aws_secret_access_key"],
65+
region_name=config.get("aws_region"),
66+
profile_name=config.get("aws_profile")
67+
)
68+
else:
69+
return None
70+
6271
def get_key_names(table: DynamoDBTable) -> DynamoDBKeyName:
6372
"""Gets the key names of the DynamoDB table.
6473
@@ -234,8 +243,8 @@ class DynamoDBMapping(MutableMapping):
234243
successive pages of the scan operation are queried only on demand. Examples of such operations
235244
include scan, iteration over keys, iteration over values, and iteration over items (key-value
236245
tuples). You should pay particular attention to certain patterns that fetch all items in the
237-
table, for example, calling list(mapping.values()). This call will execute an exhaustive scan on
238-
your table, which can be costly, and attempt to load all items into memory, which can be
246+
table, for example, calling ``list(mapping.values())``. This call will execute an exhaustive
247+
scan on your table, which can be costly, and attempt to load all items into memory, which can be
239248
resource-demanding if your table is particularly large.
240249
241250
The ``__len__`` implementation of this class returns a best-effort estimate of the number of
@@ -257,7 +266,7 @@ def __init__(
257266
session = (
258267
boto3_session or
259268
kwargs.get("boto3_session") or
260-
boto3_session_from_config(kwargs) or
269+
_boto3_session_from_config(kwargs) or
261270
boto3.Session()
262271
)
263272
dynamodb = session.resource("dynamodb")

dynamodb_mapping/utils.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)