44http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleTablesAndData.html
55"""
66import logging
7+ from typing import Any
8+
79from pynamodb .models import Model
810from pynamodb .attributes import (
9- UnicodeAttribute , NumberAttribute , UnicodeSetAttribute , UTCDateTimeAttribute
11+ ListAttribute ,
12+ NumberAttribute ,
13+ UnicodeAttribute ,
14+ UnicodeSetAttribute ,
15+ UTCDateTimeAttribute ,
1016)
1117from datetime import datetime
1218
@@ -29,7 +35,7 @@ class Meta:
2935 answered = NumberAttribute (default = 0 )
3036 tags = UnicodeSetAttribute ()
3137 last_post_datetime = UTCDateTimeAttribute (null = True )
32- notes = ListAttribute (default = list )
38+ notes : ListAttribute [ Any ] = ListAttribute (default = list )
3339
3440
3541# Delete the table
@@ -60,7 +66,7 @@ class Meta:
6066 threads = []
6167 for x in range (100 ):
6268 thread = Thread ('forum-{0}' .format (x ), 'subject-{0}' .format (x ))
63- thread .tags = [ 'tag1' , 'tag2' ]
69+ thread .tags = { 'tag1' , 'tag2' }
6470 thread .last_post_datetime = datetime .now ()
6571 threads .append (thread )
6672
@@ -75,16 +81,16 @@ class Meta:
7581
7682# Batch get
7783item_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 )
8086
8187# Scan
82- for item in Thread .scan ():
83- print (item )
88+ for thread_item in Thread .scan ():
89+ print (thread_item )
8490
8591# 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 )
8894
8995
9096print ("-" * 80 )
@@ -103,57 +109,58 @@ class Meta:
103109 tags = UnicodeSetAttribute (attr_name = 't' )
104110 last_post_datetime = UTCDateTimeAttribute (attr_name = 'lp' )
105111
112+
106113if not AliasedModel .exists ():
107114 AliasedModel .create_table (read_capacity_units = 1 , write_capacity_units = 1 , wait = True )
108115
109116# Create a thread
110- thread_item = AliasedModel (
117+ aliased_thread_item = AliasedModel (
111118 'Some Forum' ,
112119 'Some Subject' ,
113120 tags = ['foo' , 'bar' ],
114121 last_post_datetime = datetime .now ()
115122)
116123
117124# Save the thread
118- thread_item .save ()
125+ aliased_thread_item .save ()
119126
120127# Batch write operation
121128with 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 )
128135
129- for thread in threads :
130- batch .save (thread )
136+ for aliased_thread_item in aliased_threads :
137+ batch .save (aliased_thread_item )
131138
132139# Batch get
133140item_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 ))
136143
137144# 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 ))
140147
141148# 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 ))
144151
145152# 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 ))
148155
149156
150157# 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 ))
153160
154161# 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 ))
157164
158165# Conditionally save an item
159166thread_item = Thread (
0 commit comments