@@ -22,7 +22,7 @@ def _batches(items, batch_size):
2222 start += batch_size
2323
2424
25- def get_item (* , key , table ):
25+ def get_item (* , key , table , attributes = None ):
2626 """Get a single item from a table.
2727
2828 Parameters
@@ -33,6 +33,10 @@ def get_item(*, key, table):
3333 table : str
3434 Name of the DynamoDB table.
3535
36+ attributes : list[str]
37+ Names of the item attributes to return. If None (default), all attributes are
38+ returned.
39+
3640 Returns
3741 -------
3842 dict, None
@@ -49,15 +53,29 @@ def get_item(*, key, table):
4953 'last_play': '2021-01-19 19:07:54',
5054 'rating': 3.8,
5155 'play_time': '0 days 22:07:34'}
56+
57+ Get only specific attributes:
58+
59+ >>> item = get_item(
60+ ... key={"player_id": "player_two"},
61+ ... table="players",
62+ ... attributes=["play_time", "rating"]
63+ ... )
64+ >>> print(item)
65+ {'rating': 3.8, 'play_time': '0 days 22:07:34'}
5266 """
5367 table = boto3 .resource ("dynamodb" ).Table (table )
5468
55- item = table .get_item (Key = key ).get ("Item" )
69+ kwargs = {}
70+ if attributes is not None :
71+ kwargs ["ProjectionExpression" ] = ", " .join (attributes )
72+
73+ item = table .get_item (Key = key , ** kwargs ).get ("Item" )
5674
5775 return _deserialize (item )
5876
5977
60- def get_items (* , keys , table ):
78+ def get_items (* , keys , table , attributes = None ):
6179 """Get multiple items from a table.
6280
6381 Parameters
@@ -68,6 +86,10 @@ def get_items(*, keys, table):
6886 table : str
6987 Name of the DynamoDB table.
7088
89+ attributes : list[str]
90+ Names of the item attributes to return. If None (default), all attributes are
91+ returned.
92+
7193 Returns
7294 -------
7395 list[dict]
@@ -88,12 +110,27 @@ def get_items(*, keys, table):
88110 >>> print(items)
89111 [{'bonus_points': 3, 'player_id': 'player_one', 'last_play': '2021-01-18 22:47:23', 'rating': 4.3, 'play_time': '2 days 17:41:55'},
90112 {'bonus_points': 1, 'player_id': 'player_two', 'last_play': '2021-01-19 19:07:54', 'rating': 3.8, 'play_time': '0 days 22:07:34'}]
113+
114+ Get only specific attributes:
115+
116+ >>> items = get_items(
117+ ... keys=[{"player_id": "player_two"}, {"player_id": "player_one"}],
118+ ... table="players",
119+ ... attributes=["player_id", "play_time"]
120+ ... )
121+ >>> print(items)
122+ [{'player_id': 'player_one', 'play_time': '2 days 17:41:55'}, {'player_id': 'player_two', 'play_time': '0 days 22:07:34'}]
91123 """ # noqa: E501
92124
93- def _request (keys , table = table ):
94- return {table : {"Keys" : keys }}
125+ def _request (keys , table = table , attributes = attributes ):
126+ table_dict = {"Keys" : keys }
127+
128+ if attributes is not None :
129+ table_dict ["ProjectionExpression" ] = ", " .join (attributes )
130+
131+ return {table : table_dict }
95132
96- def _get_items (keys , table = table ):
133+ def _get_items (keys , table = table , attributes = attributes ):
97134 response = resource .batch_get_item (RequestItems = _request (keys ))
98135 items = response ["Responses" ][table ]
99136
@@ -114,7 +151,7 @@ def _get_items(keys, table=table):
114151 return _deserialize (items )
115152
116153
117- def get_all_items (* , table ):
154+ def get_all_items (* , table , attributes = None ):
118155 """Get all the items in a table.
119156
120157 This function performs a scan of the table.
@@ -124,6 +161,10 @@ def get_all_items(*, table):
124161 table : str
125162 Name of the DynamoDB table.
126163
164+ attributes : list[str]
165+ Names of the item attributes to return. If None (default), all attributes are
166+ returned.
167+
127168 Returns
128169 -------
129170 list[dict]
@@ -138,14 +179,28 @@ def get_all_items(*, table):
138179 {'bonus_points': None, 'player_id': 'player_four', 'last_play': '2021-01-22 13:51:12', 'rating': 4.8, 'play_time': '0 days 03:45:49'},
139180 {'bonus_points': 3, 'player_id': 'player_one', 'last_play': '2021-01-18 22:47:23', 'rating': 4.3, 'play_time': '2 days 17:41:55'},
140181 {'bonus_points': 1, 'player_id': 'player_two', 'last_play': '2021-01-19 19:07:54', 'rating': 3.8, 'play_time': '0 days 22:07:34'}]
182+
183+ Get only specific attributes:
184+
185+ >>> items = get_all_items(table="players", attributes=["player_id", "play_time"])
186+ >>> print(items)
187+ [{'player_id': 'player_three', 'play_time': '1 days 14:01:19'},
188+ {'player_id': 'player_four', 'play_time': '0 days 03:45:49'},
189+ {'player_id': 'player_one', 'play_time': '2 days 17:41:55'},
190+ {'player_id': 'player_two', 'play_time': '0 days 22:07:34'}]
141191 """ # noqa: E501
142192 table = boto3 .resource ("dynamodb" ).Table (table )
143193
144- response = table .scan ()
194+ kwargs = {}
195+ if attributes is not None :
196+ kwargs ["ProjectionExpression" ] = ", " .join (attributes )
197+
198+ response = table .scan (** kwargs )
199+
145200 items = response ["Items" ]
146201
147202 while "LastEvaluatedKey" in response :
148- response = table .scan (ExclusiveStartKey = response ["LastEvaluatedKey" ])
203+ response = table .scan (ExclusiveStartKey = response ["LastEvaluatedKey" ], ** kwargs )
149204 items .extend (response ["Items" ])
150205
151206 return _deserialize (items )
@@ -207,7 +262,7 @@ def put_items(*, items, table):
207262 Parameters
208263 ----------
209264 items : list[dict]
210- List of dictionaties where each dictionary represents an item's attributes.
265+ List of dictionaries where each dictionary represents an item's attributes.
211266
212267 table : str
213268 Name of the DynamoDB table.
0 commit comments