-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_utils.py
123 lines (96 loc) · 3.35 KB
/
app_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
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
import datetime
import random
from app import db
from models import Ping
def gen_dates(start_date, duration):
"""
yields a vector of days based on the duration provided, excludes
weekdays.
"""
for i in range(duration):
if i > 0:
# increment date only after the first iteration
start_date += datetime.timedelta(1)
if start_date.weekday() < 5:
yield start_date
def gen_times(date, frequency):
"""
yields times between 9 and 5 based on frequency given, completely randomly spaced out
"""
choices = []
for i in range(480):
choices.append(i)
for i in range(frequency):
new_time = random.choice(choices)
ping_time = datetime.datetime(year=date.year, month=date.month, day=date.day, hour=9, minute=0)
ping_time += datetime.timedelta(minutes=new_time)
yield ping_time
def build_ping_vector(date, frequency):
ping_choices = []
ping_set = []
for i in range(8):
ping_time = datetime.datetime(year=date.year, month=date.month, day=date.day, hour=9, minute=15)
ping_time += datetime.timedelta(hours=i)
ping_choices.append(ping_time)
for i in range(frequency):
new_ping = random.choice(ping_choices)
ping_set.append(new_ping)
ping_choices.remove(new_ping)
return ping_set
def gen_times_morning(date, frequency):
"""
yields times
"""
for i in range(frequency):
ping_time = datetime.datetime(year=date.year, month=date.month, day=date.day, hour=9, minute=15)
yield ping_time
def gen_ping_object(start, duration, frequency, survey_id, participant_id, morning=None):
"""
Create dictionary to hold pings for a given person
start: datetime.date(year, month, day)
frequency: integer, number of pings in a day
survey_id: id of survey
participant_id: id of participant to add pings for
"""
pings = {}
ping_times = []
start = start
for day in gen_dates(start, duration):
# for time in gen_times(day, frequency):
# ping_times.append(time)
ping_set = build_ping_vector(day, frequency)
ping_times.extend(ping_set)
pings['ping_times'] = ping_times
pings['survey_id'] = survey_id
pings['participant_id'] = participant_id
return pings
def ping_loader(ping_obj):
for ping_time in ping_obj['ping_times']:
# ping = {}
# ping['sent_time'] = ping_time
# ping['survey_id'] = ping_obj['survey_id']
# ping['participant_id'] = ping_obj['participant_id']
p = Ping(ping_obj['participant_id'],
ping_obj['survey_id'],
sent_time=ping_time)
db.session.add(p)
db.session.commit()
print 'ping {} added'.format(p)
def get_child_options(s_body):
child_options = []
for parent_option in s_body['question'].keys():
for child_option in s_body['question'][parent_option]['options'].keys():
child_options.append(child_option)
return child_options
def get_parent_options(s):
parent_options = []
for parent in s['question'].keys():
parent_options.append(parent)
return sorted(parent_options)
def cast_ans(ans):
try:
return int(ans)
except ValueError:
return ans
def datetime_east():
return datetime.datetime.utcnow() - datetime.timedelta(hours=5)