-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
75 lines (59 loc) · 2.15 KB
/
utils.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
from threading import Lock
class ThreadSafeSet(set):
def __init__(self, *args, **kwargs):
super(ThreadSafeSet, self).__init__(*args, **kwargs)
self.lock = Lock()
def add(self, *args):
with self.lock:
retval = super(ThreadSafeSet, self).add(*args)
return retval
def clear(self, *args):
with self.lock:
retval = super(ThreadSafeSet, self).clear(*args)
return retval
def pop(self, *args):
with self.lock:
if len(self):
retval = super(ThreadSafeSet, self).pop(*args)
else:
retval = None
return retval
def remove(self, item):
with self.lock:
if item in self:
retval = super(ThreadSafeSet, self).remove(item)
else:
retval = None
return retval
class ThreadSafeDict(dict):
def __init__(self, *args, **kwargs):
super(ThreadSafeDict, self).__init__(*args, **kwargs)
self.lock = Lock()
def clear(self, *args, **kwargs):
with self.lock:
retval = super(ThreadSafeDict, self).clear(*args, **kwargs)
return retval
def pop(self, *args, **kwargs):
with self.lock:
retval = super(ThreadSafeDict, self).pop(*args, **kwargs)
return retval
def __setitem__(self, *args, **kwargs):
with self.lock:
retval = super(ThreadSafeDict, self).__setitem__(*args, **kwargs)
return retval
def __delitem__(self, item):
with self.lock:
if item in self:
retval = super(ThreadSafeDict, self).__delitem__(item)
else:
retval = None
return retval
def questionnaire_to_surveyjs(questionnaire, current_step, pagify):
if pagify:
survey_object = {"pages":[]}
for key, value in questionnaire.items():
if current_step in value["steps"]:
survey_object["pages"].append({"elements":[value]})
else:
survey_object={"elements" :[value for key,value in questionnaire.items() if current_step in value["steps"]] }
return survey_object