forked from parse-community/parse-server-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchers.py
More file actions
52 lines (40 loc) · 1.59 KB
/
Copy pathbatchers.py
File metadata and controls
52 lines (40 loc) · 1.59 KB
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
#!/usr/bin/python
import time
from parse_rest.datatypes import Object
from parse_rest.connection import ParseBatcher
class BatchSaver:
def __init__(self, delay):
self.batcher = ParseBatcher()
self.objects_to_save = []
self.delay = delay
self.save_count = 0
def add_object_to_save(self, object_to_save):
self.objects_to_save.append(object_to_save)
if len(self.objects_to_save) == 50:
self.save()
def save(self, delay=True):
if len(self.objects_to_save) > 0:
print "Saving", self.save_count+1, "-", self.save_count+len(self.objects_to_save)
self.batcher.batch_save(self.objects_to_save)
self.save_count += len(self.objects_to_save)
self.objects_to_save = []
if delay:
time.sleep(self.delay)
class BatchDeleter:
def __init__(self, delay):
self.batcher = ParseBatcher()
self.objects_to_delete = []
self.delay = delay
self.delete_count = 0
def add_object_to_delete(self, object_to_delete):
self.objects_to_delete.append(object_to_delete)
if len(self.objects_to_delete) == 50:
self.delete()
def delete(self, delay=True):
if len(self.objects_to_delete) > 0:
print "Deleting", self.delete_count+1, "-", self.delete_count+len(self.objects_to_delete)
self.batcher.batch_delete(self.objects_to_delete)
self.delete_count += len(self.objects_to_delete)
self.objects_to_delete = []
if delay:
time.sleep(self.delay)