-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.py
executable file
·380 lines (328 loc) · 13.1 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python
# vim: set ts=4 sw=4 st=4 expandtab :
import time
startTime = time.time()
import datetime
import sys
import IndexedRedis
from IndexedRedis import IndexedRedisModel, IRField, setDefaultRedisConnectionParams
from IndexedRedis.fields import IRCompressedField, IRFieldChain, IRRawField, IRBytesField, IRUnicodeField
# vim: set ts=8 sw=8 st=8 expandtab :
# Change this to match your Redis server info
REDIS_CONNECTION_PARAMS = { 'host' : '127.0.0.1', 'port' : 6379, 'db' : 0 }
# Define the model
class Song(IndexedRedisModel):
FIELDS = [ \
IRField('artist'),
IRField('title'),
IRField('album'),
IRField('track_number', valueType=int), # Convert automatically to/from int
IRField('duration'),
IRField('releaseDate', valueType=datetime.datetime),
IRField('description'),
IRField('copyright'),
IRRawField('mp3_data'), # Do not perform any conversion on the data.
IRFieldChain('thumbnail', [IRBytesField(), IRCompressedField(compressMode='gzip')]), # Compress this field in storage using "gzip" compression
IRField('tags', valueType=list),
IRFieldChain('lyrics', [ IRUnicodeField(encoding='utf-8'), IRCompressedField() ], defaultValue='No lyrics found'),
]
INDEXED_FIELDS = [ \
'artist',
'title',
'track_number',
]
KEY_NAME = 'Songs'
if __name__ == '__main__':
setDefaultRedisConnectionParams(REDIS_CONNECTION_PARAMS)
fakeMp3 = b"\x99\x12\x14"
fakeThumbnail = b"\x15\x1A\x1A\x1A\x1A\x1A\x1A\x1B\x1A\x1A\x1A\x1B" + (b"\x1A" * 30) # Compressable data
sys.stdout.write('Testing IndexedRedis version %s\n' %(IndexedRedis.__version__,))
if '--keep-data' not in sys.argv:
newSongs = []
# Add data
songObj = Song(artist='The Merry Men',
title='Happy Go Lucky',
album='The Merry Men LP',
track_number=1,
duration='1:58',
description='A song about happy people',
mp3_data=fakeMp3,
thumbnail=fakeThumbnail,
tags=['happy', 'guitar', 'smooth'],
copyright='Copyright 2012 (c) Media Mogul Incorporated')
songObj.lyrics = '''To love, to lie,
to feel something rather than die
A line, a fall
I guess we can't be loved by them all..
'''
newSongs.append(songObj)
songObj = Song(artist='The Merry Men',
title='Joy to Joy',
album='The Merry Men LP',
track_number=2,
duration='2:54',
description='A song about joy',
mp3_data=fakeMp3,
thumbnail=fakeThumbnail,
tags=['joyful', 'piano', 'Key C'],
copyright='Copyright 2012 (c) Media Mogul Incorporated')
newSongs.append(songObj)
songObj = Song(artist='The Unhappy Folk',
title='Sadly she waits',
album='Misery loses comfort',
track_number=1,
duration='15:44',
description='A sad song',
mp3_data=fakeMp3,
thumbnail=fakeThumbnail,
copyright='Copyright 2014 (c) Cheese Industries')
newSongs.append(songObj)
# Atomically reset the Song dataset to just the songs in "newSongs" list
Song.reset(newSongs)
# Add some additional songs one-at-a-time
songObj = Song(artist='Mega Men',
title='Nintendo 1',
album='Super Tracks',
track_number=1,
duration='1:15',
description='Super Nintendo',
mp3_data=fakeMp3,
thumbnail=fakeThumbnail,
copyright='Copyright 2014 (c) Cheese Industries')
songObj.save()
songObj = Song(artist='Mega Men',
title='Nintendo 2',
album='Super Tracks',
track_number=2,
duration='1:55',
description='Super Nintendo',
mp3_data=fakeMp3,
thumbnail=fakeThumbnail,
copyright='Copyright 2014 (c) Cheese Industries')
songObj.save()
# Query some songs by artist
merryMenSongs = Song.objects.filter(artist='The Merry Men').all()
sys.stdout.write("Merry Men Songs:\n")
merryMenSongs.pprint()
# Query some songs not by artist
sys.stdout.write('\n\nNot Mega Men Songs:\n')
notMegaMenSongs = Song.objects.filterInline(artist__ne='Mega Men').all()
notMegaMenSongs.pprint()
sys.stdout.write('\n\n')
# Show passing filter objects around functions, and not actually fetching until .all is called.
def getTracks(filterSet, trackNo):
return filterSet.filter(track_number=trackNo).all()
sys.stdout.write('\nAll track one songs:\n')
allTrackOneSongs = getTracks(Song.objects, 1)
for song in allTrackOneSongs:
song.pprint()
sys.stdout.write('\n')
sys.stdout.write('\nMega Men track ones:')
megaMenTracks = Song.objects.filter(artist='Mega Men')
objs = getTracks(megaMenTracks, 1)
for song in objs:
song.pprint()
sys.stdout.write('\n')
sys.stdout.write('\nMega Men track twos (should be just one entry):\n')
objs = getTracks(megaMenTracks, 2)
for song in objs:
song.pprint()
sys.stdout.write('\n')
pk = song.getPk()
sys.stdout.write('\nPrimary key of previous song: %s\n' %(pk,))
sys.stdout.write('\nSong pk=%s exists? (should be True) %s\n' %(pk, str(Song.objects.exists(pk)) ) )
sys.stdout.write('\nAfter Delete, Mega Men Track twos (should be blank):\n')
song.delete()
objs = getTracks(megaMenTracks, 2)
for song in objs:
song.pprint()
sys.stdout.write('\n')
sys.stdout.write('\nSong pk=%s exists? (should now be False) %s\n' %(pk, str(Song.objects.exists(pk)) ) )
# delete remaining Mega Men songs
numDeleted = Song.objects.filter(artist='Mega Men').delete()
sys.stdout.write('\nDeleting remaining Mega Men Tracks: %d deleted.\n' %(numDeleted,))
sys.stdout.write('\nRemaining Mega Men tracks (should be blank):\n')
songs = Song.objects.filter(artist='Mega Men').all()
sys.stdout.write(str(songs) + '\n')
endTime = time.time()
sys.stderr.write("TIME: %.6f\n" %(endTime - startTime, ))
# vim: set ts=4 sw=4 st=4 expandtab :
###############################
## OUTPUT: #
###############################
#Testing IndexedRedis version 4.1.4
#Merry Men Songs:
#{'album': 'The Merry Men LP',
# 'artist': 'The Merry Men',
# 'copyright': 'Copyright 2012 (c) Media Mogul Incorporated',
# 'description': 'A song about joy',
# 'duration': '2:54',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': ['joyful', 'piano', 'Key C'],
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Joy to Joy',
# 'track_number': 2}
#
#{'album': 'The Merry Men LP',
# 'artist': 'The Merry Men',
# 'copyright': 'Copyright 2012 (c) Media Mogul Incorporated',
# 'description': 'A song about happy people',
# 'duration': '1:58',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': ['happy', 'guitar', 'smooth'],
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Happy Go Lucky',
# 'track_number': 1}
#
#
#
#Not Mega Men Songs:
#{'album': 'The Merry Men LP',
# 'artist': 'The Merry Men',
# 'copyright': 'Copyright 2012 (c) Media Mogul Incorporated',
# 'description': 'A song about joy',
# 'duration': '2:54',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': ['joyful', 'piano', 'Key C'],
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Joy to Joy',
# 'track_number': 2}
#
#{'album': 'Misery loses comfort',
# 'artist': 'The Unhappy Folk',
# 'copyright': 'Copyright 2014 (c) Cheese Industries',
# 'description': 'A sad song',
# 'duration': '15:44',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': IRNullType(),
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Sadly she waits',
# 'track_number': 1}
#
#{'album': 'The Merry Men LP',
# 'artist': 'The Merry Men',
# 'copyright': 'Copyright 2012 (c) Media Mogul Incorporated',
# 'description': 'A song about happy people',
# 'duration': '1:58',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': ['happy', 'guitar', 'smooth'],
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Happy Go Lucky',
# 'track_number': 1}
#
#
#
#
#All track one songs:
#{'album': 'Super Tracks',
# 'artist': 'Mega Men',
# 'copyright': 'Copyright 2014 (c) Cheese Industries',
# 'description': 'Super Nintendo',
# 'duration': '1:15',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': IRNullType(),
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Nintendo 1',
# 'track_number': 1}
#
#{'album': 'Misery loses comfort',
# 'artist': 'The Unhappy Folk',
# 'copyright': 'Copyright 2014 (c) Cheese Industries',
# 'description': 'A sad song',
# 'duration': '15:44',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': IRNullType(),
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Sadly she waits',
# 'track_number': 1}
#
#{'album': 'The Merry Men LP',
# 'artist': 'The Merry Men',
# 'copyright': 'Copyright 2012 (c) Media Mogul Incorporated',
# 'description': 'A song about happy people',
# 'duration': '1:58',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': ['happy', 'guitar', 'smooth'],
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Happy Go Lucky',
# 'track_number': 1}
#
#
#Mega Men track ones:{'album': 'Super Tracks',
# 'artist': 'Mega Men',
# 'copyright': 'Copyright 2014 (c) Cheese Industries',
# 'description': 'Super Nintendo',
# 'duration': '1:15',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': IRNullType(),
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Nintendo 1',
# 'track_number': 1}
#
#
#Mega Men track twos (should be just one entry):
#{'album': 'Super Tracks',
# 'artist': 'Mega Men',
# 'copyright': 'Copyright 2014 (c) Cheese Industries',
# 'description': 'Super Nintendo',
# 'duration': '1:55',
# 'mp3_data': b'\x99\x12\x14',
# 'releaseDate': IRNullType(),
# 'tags': IRNullType(),
# 'thumbnail': b'\x15\x1a\x1a\x1a\x1a\x1a\x1a\x1b\x1a\x1a\x1a\x1b'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a\x1a'
# b'\x1a\x1a\x1a\x1a\x1a\x1a',
# 'title': 'Nintendo 2',
# 'track_number': 2}
#
#
#Primary key of previous song: 6
#
#Song pk=6 exists? (should be True) True
#
#After Delete, Mega Men Track twos (should be blank):
#
#Song pk=6 exists? (should now be False) False
#
#Deleting remaining Mega Men Tracks: 1 deleted.
#
#Remaining Mega Men tracks (should be blank):
#IRQueryableList([])
# vim: set ts=4 sw=4 st=4 expandtab :