Skip to content

Commit 5ea45ec

Browse files
author
Janos Tolgyesi
committed
Fix some docstrings
1 parent 3c56863 commit 5ea45ec

File tree

5 files changed

+143
-46
lines changed

5 files changed

+143
-46
lines changed

docs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# You can set these variables from the command line.
55
SPHINXOPTS =
6-
SPHINXBUILD = python -msphinx
6+
SPHINXBUILD = sphinx-build
77
SPHINXPROJ = dynamodb_mapping
88
SOURCEDIR = .
99
BUILDDIR = _build

docs/conf.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131

3232
# Add any Sphinx extension module names here, as strings. They can be
3333
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
34-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']
34+
extensions = [
35+
'sphinx.ext.autodoc',
36+
'sphinx.ext.viewcode',
37+
'sphinx.ext.napoleon',
38+
'sphinx.ext.intersphinx'
39+
]
3540

3641
# Add any paths that contain templates here, relative to this directory.
3742
templates_path = ['_templates']
@@ -64,7 +69,7 @@
6469
#
6570
# This is also used if you do content translation via gettext catalogs.
6671
# Usually you set "language" from the command line for these cases.
67-
language = None
72+
language = 'en'
6873

6974
# List of patterns, relative to source directory, that match files and
7075
# directories to ignore when looking for source files.
@@ -77,13 +82,12 @@
7782
# If true, `todo` and `todoList` produce output, else they produce nothing.
7883
todo_include_todos = False
7984

80-
8185
# -- Options for HTML output -------------------------------------------
8286

8387
# The theme to use for HTML and HTML Help pages. See the documentation for
8488
# a list of builtin themes.
8589
#
86-
html_theme = 'alabaster'
90+
html_theme = 'sphinx_rtd_theme'
8791

8892
# Theme options are theme-specific and customize the look and feel of a
8993
# theme further. For a list of options available for each theme, see the
@@ -94,7 +98,7 @@
9498
# Add any paths that contain custom static files (such as style sheets) here,
9599
# relative to this directory. They are copied after the builtin static files,
96100
# so a file named "default.css" will overwrite the builtin "default.css".
97-
html_static_path = ['_static']
101+
# html_static_path = ['_static']
98102

99103

100104
# -- Options for HTMLHelp output ---------------------------------------
@@ -105,7 +109,7 @@
105109

106110
# -- Options for LaTeX output ------------------------------------------
107111

108-
latex_elements = {
112+
# latex_elements = {
109113
# The paper size ('letterpaper' or 'a4paper').
110114
#
111115
# 'papersize': 'letterpaper',
@@ -121,7 +125,7 @@
121125
# Latex figure (float) alignment
122126
#
123127
# 'figure_align': 'htbp',
124-
}
128+
# }
125129

126130
# Grouping the document tree into LaTeX files. List of tuples
127131
# (source start file, target name, title, author, documentclass
@@ -159,4 +163,19 @@
159163
]
160164

161165

166+
# =======
167+
162168

169+
autodoc_typehints = 'description'
170+
autodoc_member_order = 'bysource'
171+
add_module_names = False
172+
autodoc_default_options = {
173+
'special-members': '__iter__,__len__,__getitem__,__setitem__,__delitem__',
174+
'undoc-members': False,
175+
}
176+
177+
intersphinx_mapping = {
178+
'python': ('http://docs.python.org/3', None),
179+
'boto3': ('https://boto3.amazonaws.com/v1/documentation/api/latest/', None),
180+
'botocore': ('https://botocore.readthedocs.io/en/latest/', None),
181+
}

docs/dynamodb_mapping.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ 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+
1523
Module contents
1624
---------------
1725

dynamodb_mapping/dynamodb_mapping.py

Lines changed: 103 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)