5
5
import json
6
6
import os
7
7
import codecs
8
- from datetime import date
8
+ import calendar
9
+ import time
10
+ from datetime import date ,datetime ,timedelta
9
11
from timeparameters import *
10
12
from calendar import timegm
11
13
14
+ START_DATE = datetime .strptime ("2010-01-01" ,"%Y-%m-%d" )
15
+ END_DATE = datetime .strptime ("2013-01-01" ,"%Y-%m-%d" )
16
+
17
+ def format_date (date ):
18
+ return int (time .mktime (date .timetuple ())* 1000 )
19
+
12
20
# class ParamsWriter:
13
21
# def __init__(self, name, num_params):
14
22
# self.files = []
@@ -105,7 +113,6 @@ def post_month_params(sample, lower_bound, upper_bound):
105
113
# results.append([[start_day, end_day], count_sum])
106
114
# return results
107
115
108
-
109
116
def key_params (sample , lower_bound , upper_bound ):
110
117
results = []
111
118
for key , count in sample :
@@ -124,14 +131,14 @@ def serialize_q2(country_sets, post_day_ranges):
124
131
for country_set , count_country in country_sets :
125
132
for day_range , count_post in post_day_ranges :
126
133
if random .randint (0 ,len (country_sets ) + len (post_day_ranges )) == 0 :
127
- writer .append ([str (day_range [0 ]), str (day_range [1 ]), "2013-01-01" , "," .join (country_set )], [count_post ,count_post ,count_country ,333 ])
134
+ writer .append ([str (day_range [0 ]), str (day_range [1 ]), ";" .join (country_set ), str ( format_date ( END_DATE ) )], [count_post ,count_post ,count_country ,333 ])
128
135
129
136
def serialize_q3 (post_months ):
130
- writer = ParamsWriter ("q3" , ["todo1 " ,"todo2 " ])
137
+ writer = ParamsWriter ("q3" , ["range1Start " ,"range1End" , "range2Start" , "range2End " ])
131
138
for ix in range (0 ,len (post_months )):
132
139
week_range_a , count_a = post_months [ix ]
133
140
for week_range_b , count_b in post_months [ix + 1 :]:
134
- writer .append ([str (week_range_a ),str (week_range_b )], [count_a ,count_b ])
141
+ writer .append ([str (week_range_a [ 0 ] ),str (week_range_a [ 1 ]), str ( week_range_b [ 0 ]), str ( week_range_b [ 1 ] )], [count_a ,count_b ])
135
142
136
143
def serialize_q4 (tagclasses , countries ):
137
144
writer = ParamsWriter ("q4" , ["tagClass" ,"country" ])
@@ -172,6 +179,9 @@ def serialize_q10(tags):
172
179
for tag , count in tags :
173
180
writer .append ([tag ], [count ])
174
181
182
+ def serialize_q11 ():
183
+ writer = ParamsWriter ("q11" , ["country" , "blacklist" ])
184
+
175
185
def serialize_q12 (post_weeks ):
176
186
writer = ParamsWriter ("q12" , ["creationDate" , "likeCount" ])
177
187
for week , count in post_weeks :
@@ -185,7 +195,7 @@ def serialize_q13(countries):
185
195
def serialize_q14 (creationdates ):
186
196
writer = ParamsWriter ("q14" , ["begin" ,"todoEnd" ])
187
197
for creation , count in creationdates :
188
- writer .append ([str (creation ),"todo" ], [count ])
198
+ writer .append ([str (creation ),str ( 1278888800000 ) ], [count ])
189
199
190
200
def serialize_q15 (countries ):
191
201
writer = ParamsWriter ("q15" , ["country" ])
@@ -215,10 +225,13 @@ def serialize_q19(tagclasses):
215
225
for tag_class_b , count_b in tagclasses [ix + 1 :]:
216
226
writer .append ([str ("1989-1-1" ),tag_class_a , tag_class_b ], [count_a , count_b ])
217
227
228
+ def serialize_q20 ():
229
+ writer = ParamsWriter ("q20" , [])
230
+
218
231
def serialize_q21 (countries ):
219
232
writer = ParamsWriter ("q21" , ["country" ,"endDate" ])
220
233
for country , count in countries :
221
- writer .append ([country ,str ("2013-02-01" )], [count ])
234
+ writer .append ([country ,str (format_date ( END_DATE ) )], [count ])
222
235
223
236
def serialize_q22 (countries ):
224
237
writer = ParamsWriter ("q22" , ["country1" ,"country2" ])
@@ -237,15 +250,23 @@ def serialize_q24(tagclasses):
237
250
for tagclass , count in tagclasses :
238
251
writer .append ([tagclass ], [count ])
239
252
253
+ def add_months (sourcedate ,months ):
254
+ month = sourcedate .month - 1 + months
255
+ year = int (sourcedate .year + month / 12 )
256
+ month = month % 12 + 1
257
+ day = min (sourcedate .day ,calendar .monthrange (year ,month )[1 ])
258
+ return sourcedate .replace (year , month , day )
259
+
240
260
def convert_posts_histo (histogram ):
241
261
week_posts = []
242
262
month = 0
243
263
while (histogram .existParam (month )):
244
264
monthTotal = histogram .getValue (month , "p" )
245
- week_posts .append ([month * 30 , monthTotal / 4 ])
246
- week_posts .append ([month * 30 + 7 , monthTotal / 4 ])
247
- week_posts .append ([month * 30 + 14 , monthTotal / 4 ])
248
- week_posts .append ([month * 30 + 21 , monthTotal / 4 ])
265
+ baseDate = add_months (START_DATE ,month )
266
+ week_posts .append ([format_date (baseDate ), monthTotal / 4 ])
267
+ week_posts .append ([format_date (baseDate + timedelta (days = 7 )), monthTotal / 4 ])
268
+ week_posts .append ([format_date (baseDate + timedelta (days = 14 )), monthTotal / 4 ])
269
+ week_posts .append ([format_date (baseDate + timedelta (days = 21 )), monthTotal / 4 ])
249
270
month = month + 1
250
271
return week_posts
251
272
@@ -299,8 +320,10 @@ def main(argv=None):
299
320
post_upper_threshold = 0.1 * total_posts * 1.1
300
321
post_day_ranges = post_date_range_params (week_posts , post_lower_threshold , post_upper_threshold )
301
322
302
- post_lower_threshold = (total_posts / (week_posts [len (week_posts )- 1 ][0 ]/ 7 / 4 ))* 0.8
303
- post_upper_threshold = (total_posts / (week_posts [len (week_posts )- 1 ][0 ]/ 7 / 4 ))* 1.2
323
+ #post_lower_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*0.8
324
+ #post_upper_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*1.2
325
+ post_lower_threshold = (total_posts / (len (week_posts )/ 4 ))* 0.8
326
+ post_upper_threshold = (total_posts / (len (week_posts )/ 4 ))* 1.2
304
327
post_months = post_month_params (week_posts , post_lower_threshold , post_upper_threshold )
305
328
306
329
serialize_q2 (country_sets , post_day_ranges )
@@ -329,5 +352,8 @@ def main(argv=None):
329
352
serialize_q23 (key_params (country_sample , total_posts / 200 , total_posts / 100 ))
330
353
serialize_q24 (key_params (tagclass_posts , total_posts / 140 , total_posts / 5 ))
331
354
355
+ serialize_q11 ()
356
+ serialize_q20 ()
357
+
332
358
if __name__ == "__main__" :
333
359
sys .exit (main ())
0 commit comments