@@ -67,7 +67,7 @@ def get_key_names(table: DynamoDBTable) -> DynamoDBKeyName:
6767
6868 Returns:
6969 DynamoDBKeyName: A tuple with either one (if only the partition key is defined on the table)
70- or two (if both the partition and range key is defined) elements.
70+ or two (if both the partition and range key is defined) elements.
7171 """
7272 schema : Dict [str , str ] = {s ["KeyType" ]: s ["AttributeName" ] for s in table .key_schema }
7373 return (schema ["HASH" ], schema ["RANGE" ]) if "RANGE" in schema else (schema ["HASH" ], )
@@ -204,31 +204,31 @@ class DynamoDBMapping(MutableMapping):
204204 You have the following options to configure the underlying boto3 session:
205205
206206 - Automatic configuration: pass nothing to DynamoDBMapping initializer. This will prompt
207- DynamoDBMapping to load the default `Session` object, which in turn will use the standard boto3
208- credentials chain to find AWS credentials (e.g., the ~/.aws/credentials file, environment
209- variables, etc.).
210- - Pass a preconfigured boto3 ` Session` object
211- - Pass `aws_access_key_id` and `aws_secret_access_key` as keyword arguments. Additionally,
212- the optional `aws_region` and `aws_profile` arguments are also considered.
207+ DynamoDBMapping to load the default ``boto3. Session`` object, which in turn will use the
208+ standard boto3 credentials chain to find AWS credentials (e.g., the `` ~/.aws/credentials``
209+ file, environment variables, etc.).
210+ - Pass a preconfigured ``boto3. Session` ` object
211+ - Pass `` aws_access_key_id`` and `` aws_secret_access_key` ` as keyword arguments. Additionally,
212+ the optional `` aws_region`` and `` aws_profile` ` arguments are also considered.
213213
214- Example:
215- ```python
216- from dynamodb_mapping import DynamoDBMapping
217- mapping = DynamoDBMapping(table_name="my_table")
214+ Example::
218215
219- # Iterate over all items:
220- for key, value in mapping.items():
221- print(key, value)
216+ from dynamodb_mapping import DynamoDBMapping
217+ mapping = DynamoDBMapping(table_name="my_table")
222218
223- # Get a single item:
224- print(mapping["my_key"])
219+ # Iterate over all items:
220+ for key, value in mapping.items():
221+ print(key, value)
225222
226- # Create or modify an item:
227- mapping["my_key"] = {"description": "foo", "price": 123}
223+ # Get a single item:
224+ print(mapping["my_key"])
225+
226+ # Create or modify an item:
227+ mapping["my_key"] = {"description": "foo", "price": 123}
228+
229+ # Delete an item:
230+ del mapping["my_key"]
228231
229- # Delete an item:
230- del mapping["my_key"]
231- ```
232232
233233 All methods that iterate over the elements of the table do so in a lazy manner, in that the
234234 successive pages of the scan operation are queried only on demand. Examples of such operations
@@ -238,17 +238,17 @@ class DynamoDBMapping(MutableMapping):
238238 your table, which can be costly, and attempt to load all items into memory, which can be
239239 resource-demanding if your table is particularly large.
240240
241- The `__len__` implementation of this class returns a best-effort estimate of the number of
241+ The `` __len__` ` implementation of this class returns a best-effort estimate of the number of
242242 items in the table using the TableDescription DynamoDB API. The number of items are updated
243243 at DynamoDB service side approximately once in every 6 hours. If you need the exact number of
244- items currently in the table, you can use `len(list(mapping.keys()))`. Note however that this
244+ items currently in the table, you can use `` len(list(mapping.keys()))` `. Note however that this
245245 will cause to run an exhaustive scan operation on your table.
246246
247247 Args:
248248 table_name (str): The name of the DynamoDB table.
249249 boto3_session (Optional[boto3.Session]): An optional preconfigured boto3 Session object.
250250 **kwargs: Additional keyword parameters for manual configuration of the boto3 client:
251- `aws_access_key_id`, `aws_secret_access_key`, `aws_region`, `aws_profile`.
251+ `` aws_access_key_id`` , `` aws_secret_access_key`` , `` aws_region`` , `` aws_profile` `.
252252 """
253253
254254 def __init__ (
@@ -275,8 +275,13 @@ def scan(self, **kwargs) -> Iterator[DynamoDBItemType]:
275275 """Performs a scan operation on the DynamoDB table. The scan is executed in a lazy manner,
276276 in that the successive pages are queried only on demand.
277277
278+ Example::
279+
280+ for item in mapping.scan():
281+ print(item)
282+
278283 Args:
279- **kwargs: keyword arguments to be passed to the underlying DynamoDB scan operation.
284+ **kwargs: keyword arguments to be passed to the underlying DynamoDB `` scan`` operation.
280285
281286 Returns:
282287 Iterator[DynamoDBItemType]: An iterator over all items in the table.
@@ -295,11 +300,16 @@ def get_item(self, keys: DynamoDBKeySimplified, **kwargs) -> DynamoDBItemType:
295300
296301 The value(s) of the item's key(s) should be specified.
297302
303+ Example::
304+
305+ print(mapping.get_item("my_key"))
306+
298307 Args:
299308 keys (DynamoDBKeySimplified): The key value. This can either be a simple Python type,
300309 if only the partition key is specified in the table's key schema, or a tuple of the
301310 partition key and the range key values, if both are specified in the key schema.
302- **kwargs: keyword arguments to be passed to the underlying DynamoDB get_item operation.
311+ **kwargs: keyword arguments to be passed to the underlying DynamoDB ``get_item``
312+ operation.
303313
304314 Raises:
305315 ValueError: If the required key values are not specified.
@@ -317,14 +327,19 @@ def get_item(self, keys: DynamoDBKeySimplified, **kwargs) -> DynamoDBItemType:
317327 return DynamoDBItemAccessor (parent = self , item_keys = keys , initial_data = data )
318328
319329 def set_item (self , keys : DynamoDBKeySimplified , item : DynamoDBItemType , ** kwargs ) -> None :
320- """Creates or overwrites a single item in the table.
330+ """Create or overwrite a single item in the table.
331+
332+ Example::
333+
334+ mapping.set_item("my_key", {"name": "my first object", "data": {"foo": "bar"}})
321335
322336 Args:
323337 keys (DynamoDBKeySimplified): The key value. This can either be a simple Python type,
324338 if only the partition key is specified in the table's key schema, or a tuple of the
325339 partition key and the range key values, if both are specified in the key schema.
326340 item (DynamoDBItemType): The new item.
327- **kwargs: keyword arguments to be passed to the underlying DynamoDB set_item operation.
341+ **kwargs: keyword arguments to be passed to the underlying DynamoDB ``set_item``
342+ operation.
328343
329344 """
330345 key_params = self ._create_key_param (keys )
@@ -333,11 +348,15 @@ def set_item(self, keys: DynamoDBKeySimplified, item: DynamoDBItemType, **kwargs
333348 self .table .put_item (Item = _item , ** kwargs )
334349
335350 def put_item (self , keys : DynamoDBKeySimplified , item : DynamoDBItemType , ** kwargs ) -> None :
336- """An alias for the `set_item` method."""
351+ """An alias for the `` set_item` ` method."""
337352 self .set_item (keys , item , ** kwargs )
338353
339354 def del_item (self , keys : DynamoDBKeySimplified , check_existing = True , ** kwargs ) -> None :
340- """Deletes a single item from the table.
355+ """Delete a single item from the table.
356+
357+ Example::
358+
359+ mapping.del_item("my_key")
341360
342361 Args:
343362 keys (DynamoDBKeySimplified): The key value. This can either be a simple Python type,
@@ -346,7 +365,7 @@ def del_item(self, keys: DynamoDBKeySimplified, check_existing=True, **kwargs) -
346365 check_existing (bool): Raise ValueError if the specified key does not exists in the
347366 table. Defaults to True to be consistent with python dict implementation, however
348367 this causes an additional get_item operation to be executed.
349- **kwargs: keyword arguments to be passed to the underlying DynamoDB delete_item
368+ **kwargs: keyword arguments to be passed to the underlying DynamoDB `` delete_item``
350369 operation.
351370 """
352371 key_params = self ._create_key_param (keys )
@@ -360,7 +379,11 @@ def modify_item(self,
360379 modifications : DynamoDBItemType ,
361380 ** kwargs
362381 ) -> None :
363- """Modifies the properties of an existing item.
382+ """Modify the properties of an existing item.
383+
384+ Example::
385+
386+ mapping.modify_item("my_key", {"title": "new_title"})
364387
365388 Args:
366389 keys (DynamoDBKeySimplified): The key value of the item. This can either be a simple
@@ -371,7 +394,7 @@ def modify_item(self,
371394 the fields of the item. This mapping follows the same format as the entire item,
372395 but it isn't required to contain all fields: fields that are omitted will be
373396 unaffected. To delete a field, set the field value to None.
374- **kwargs: keyword arguments to be passed to the underlying DynamoDB update_item
397+ **kwargs: keyword arguments to be passed to the underlying DynamoDB `` update_item``
375398 operation.
376399 """
377400 key_params = self ._create_key_param (keys )
@@ -421,7 +444,13 @@ def _key_values_from_item(self, item: DynamoDBItemType) -> DynamoDBKeyAny:
421444 def __iter__ (self ) -> Iterator :
422445 """Returns an iterator over the table.
423446
424- This method performs a lazy DynamoDB `scan` operation.
447+ This method performs a lazy DynamoDB ``scan`` operation.
448+
449+ Example::
450+
451+ for item in mapping:
452+ print(item)
453+
425454 """
426455 for item in self .scan (ProjectionExpression = ", " .join (self .key_names )):
427456 yield simplify_tuple_keys (self ._key_values_from_item (item ))
@@ -430,28 +459,49 @@ def __len__(self) -> int:
430459 """Returns a best effort estimation of the number of items in the table.
431460
432461 If you need the precise number of items in the table, you can use
433- `len(list(mapping.keys()))`. However this later can be a costly operation.
462+ ``len(list(mapping.keys()))``. However this later can be a costly operation.
463+
464+ Example::
465+
466+ print(len(mapping))
467+
434468 """
435469 return self .table .item_count
436470
437471 def __getitem__ (self , __key : Any ) -> Any :
438472 """Retrieves a single item from the table.
439473
440- Delegates the call to `get_item` method without additional keyword arguments.
474+ Delegates the call to ``get_item`` method without additional keyword arguments.
475+
476+ Example::
477+
478+ print(mapping["my_key"])
479+ mapping["my_key"]["info"] = "You can directly add or modify item attributes!"
480+
441481 """
442482 return self .get_item (__key )
443483
444484 def __setitem__ (self , __key : Any , __value : Any ) -> None :
445485 """Creates or overwrites a single item in the table.
446486
447- Delegates the call to `set_item` method without additional keyword arguments.
487+ Delegates the call to ``set_item`` method without additional keyword arguments.
488+
489+ Example::
490+
491+ mapping["my_key"] = {"name": "my first object", "data": {"foo": "bar"}}
492+
448493 """
449494 self .set_item (__key , __value )
450495
451496 def __delitem__ (self , __key : Any ) -> None :
452497 """Deletes a single item from the table.
453498
454- Delegates the call to `del_item` method without additional keyword arguments.
499+ Delegates the call to ``del_item`` method without additional keyword arguments.
500+
501+ Example::
502+
503+ del mapping["my_key"]
504+
455505 """
456506 self .del_item (__key )
457507
@@ -460,6 +510,11 @@ def items(self) -> ItemsView:
460510
461511 The returned view can be used to iterate over (key, value) tuples in the table.
462512
513+ Example::
514+
515+ for key, item in mapping.items():
516+ print(key, item)
517+
463518 Returns:
464519 ItemsView: The items view.
465520 """
@@ -470,6 +525,11 @@ def values(self) -> ValuesView:
470525
471526 The returned view can be used to iterate over the values in the table.
472527
528+ Example::
529+
530+ for item in mapping.values():
531+ print(item)
532+
473533 Returns:
474534 ValuesView: The values view.
475535 """
@@ -480,6 +540,11 @@ def keys(self) -> KeysView:
480540
481541 The returned view can be used to iterate over the keys in the table.
482542
543+ Example::
544+
545+ for key in mapping.keys():
546+ print(key)
547+
483548 Returns:
484549 KeysView: The keys view.
485550 """
0 commit comments