4
4
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleTablesAndData.html
5
5
"""
6
6
import logging
7
+ from typing import Any
8
+
7
9
from pynamodb .models import Model
8
10
from pynamodb .attributes import (
9
- UnicodeAttribute , NumberAttribute , UnicodeSetAttribute , UTCDateTimeAttribute
11
+ ListAttribute ,
12
+ NumberAttribute ,
13
+ UnicodeAttribute ,
14
+ UnicodeSetAttribute ,
15
+ UTCDateTimeAttribute ,
10
16
)
11
17
from datetime import datetime
12
18
@@ -29,7 +35,7 @@ class Meta:
29
35
answered = NumberAttribute (default = 0 )
30
36
tags = UnicodeSetAttribute ()
31
37
last_post_datetime = UTCDateTimeAttribute (null = True )
32
- notes = ListAttribute (default = list )
38
+ notes : ListAttribute [ Any ] = ListAttribute (default = list )
33
39
34
40
35
41
# Delete the table
@@ -60,7 +66,7 @@ class Meta:
60
66
threads = []
61
67
for x in range (100 ):
62
68
thread = Thread ('forum-{0}' .format (x ), 'subject-{0}' .format (x ))
63
- thread .tags = [ 'tag1' , 'tag2' ]
69
+ thread .tags = { 'tag1' , 'tag2' }
64
70
thread .last_post_datetime = datetime .now ()
65
71
threads .append (thread )
66
72
@@ -75,16 +81,16 @@ class Meta:
75
81
76
82
# Batch get
77
83
item_keys = [('forum-{0}' .format (x ), 'subject-{0}' .format (x )) for x in range (100 )]
78
- for item in Thread .batch_get (item_keys ):
79
- print (item )
84
+ for thread_item in Thread .batch_get (item_keys ):
85
+ print (thread_item )
80
86
81
87
# Scan
82
- for item in Thread .scan ():
83
- print (item )
88
+ for thread_item in Thread .scan ():
89
+ print (thread_item )
84
90
85
91
# Query
86
- for item in Thread .query ('forum-1' , Thread .subject .startswith ('subject' )):
87
- print (item )
92
+ for thread_item in Thread .query ('forum-1' , Thread .subject .startswith ('subject' )):
93
+ print (thread_item )
88
94
89
95
90
96
print ("-" * 80 )
@@ -103,57 +109,58 @@ class Meta:
103
109
tags = UnicodeSetAttribute (attr_name = 't' )
104
110
last_post_datetime = UTCDateTimeAttribute (attr_name = 'lp' )
105
111
112
+
106
113
if not AliasedModel .exists ():
107
114
AliasedModel .create_table (read_capacity_units = 1 , write_capacity_units = 1 , wait = True )
108
115
109
116
# Create a thread
110
- thread_item = AliasedModel (
117
+ aliased_thread_item = AliasedModel (
111
118
'Some Forum' ,
112
119
'Some Subject' ,
113
120
tags = ['foo' , 'bar' ],
114
121
last_post_datetime = datetime .now ()
115
122
)
116
123
117
124
# Save the thread
118
- thread_item .save ()
125
+ aliased_thread_item .save ()
119
126
120
127
# Batch write operation
121
128
with AliasedModel .batch_write () as batch :
122
- threads = []
123
- for x in range (100 ):
124
- thread = AliasedModel ('forum-{0}' .format (x ), 'subject-{0}' .format (x ))
125
- thread .tags = [ 'tag1' , 'tag2' ]
126
- thread .last_post_datetime = datetime .now ()
127
- threads .append (thread )
129
+ aliased_threads = []
130
+ for idx in range (100 ):
131
+ aliased_thread_item = AliasedModel ('forum-{0}' .format (idx ), 'subject-{0}' .format (idx ))
132
+ aliased_thread_item .tags = { 'tag1' , 'tag2' }
133
+ aliased_thread_item .last_post_datetime = datetime .now ()
134
+ aliased_threads .append (aliased_thread_item )
128
135
129
- for thread in threads :
130
- batch .save (thread )
136
+ for aliased_thread_item in aliased_threads :
137
+ batch .save (aliased_thread_item )
131
138
132
139
# Batch get
133
140
item_keys = [('forum-{0}' .format (x ), 'subject-{0}' .format (x )) for x in range (100 )]
134
- for item in AliasedModel .batch_get (item_keys ):
135
- print ("Batch get item: {0}" .format (item ))
141
+ for aliased_thread_item in AliasedModel .batch_get (item_keys ):
142
+ print ("Batch get item: {0}" .format (aliased_thread_item ))
136
143
137
144
# Scan
138
- for item in AliasedModel .scan ():
139
- print ("Scanned item: {0}" .format (item ))
145
+ for aliased_thread_item in AliasedModel .scan ():
146
+ print ("Scanned item: {0}" .format (aliased_thread_item ))
140
147
141
148
# Query
142
- for item in AliasedModel .query ('forum-1' , AliasedModel .subject .startswith ('subject' )):
143
- print ("Query using aliased attribute: {0}" .format (item ))
149
+ for aliased_thread_item in AliasedModel .query ('forum-1' , AliasedModel .subject .startswith ('subject' )):
150
+ print ("Query using aliased attribute: {0}" .format (aliased_thread_item ))
144
151
145
152
# Query with filters
146
- for item in Thread .query ('forum-1' , (Thread .views == 0 ) | (Thread .replies == 0 )):
147
- print ("Query result: {0}" .format (item ))
153
+ for thread_item in Thread .query ('forum-1' , (Thread .views == 0 ) | (Thread .replies == 0 )):
154
+ print ("Query result: {0}" .format (thread_item ))
148
155
149
156
150
157
# Scan with filters
151
- for item in Thread .scan (Thread .subject .startswith ('subject' ) & (Thread .views == 0 )):
152
- print ("Scanned item: {0} {1}" .format (item .subject , item .views ))
158
+ for thread_item in Thread .scan (Thread .subject .startswith ('subject' ) & (Thread .views == 0 )):
159
+ print ("Scanned item: {0} {1}" .format (thread_item .subject , thread_item .views ))
153
160
154
161
# Scan with null filter
155
- for item in Thread .scan (Thread .subject .startswith ('subject' ) & Thread .last_post_datetime .does_not_exist ()):
156
- print ("Scanned item: {0} {1}" .format (item .subject , item .views ))
162
+ for thread_item in Thread .scan (Thread .subject .startswith ('subject' ) & Thread .last_post_datetime .does_not_exist ()):
163
+ print ("Scanned item: {0} {1}" .format (thread_item .subject , thread_item .views ))
157
164
158
165
# Conditionally save an item
159
166
thread_item = Thread (
0 commit comments