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,7 +131,7 @@ 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
137
writer = ParamsWriter ("q3" , ["todo1" ,"todo2" ])
@@ -237,15 +244,23 @@ def serialize_q24(tagclasses):
237
244
for tagclass , count in tagclasses :
238
245
writer .append ([tagclass ], [count ])
239
246
247
+ def add_months (sourcedate ,months ):
248
+ month = sourcedate .month - 1 + months
249
+ year = int (sourcedate .year + month / 12 )
250
+ month = month % 12 + 1
251
+ day = min (sourcedate .day ,calendar .monthrange (year ,month )[1 ])
252
+ return sourcedate .replace (year , month , day )
253
+
240
254
def convert_posts_histo (histogram ):
241
255
week_posts = []
242
256
month = 0
243
257
while (histogram .existParam (month )):
244
258
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 ])
259
+ baseDate = add_months (START_DATE ,month )
260
+ week_posts .append ([format_date (baseDate ), monthTotal / 4 ])
261
+ week_posts .append ([format_date (baseDate + timedelta (days = 7 )), monthTotal / 4 ])
262
+ week_posts .append ([format_date (baseDate + timedelta (days = 14 )), monthTotal / 4 ])
263
+ week_posts .append ([format_date (baseDate + timedelta (days = 21 )), monthTotal / 4 ])
249
264
month = month + 1
250
265
return week_posts
251
266
@@ -299,8 +314,10 @@ def main(argv=None):
299
314
post_upper_threshold = 0.1 * total_posts * 1.1
300
315
post_day_ranges = post_date_range_params (week_posts , post_lower_threshold , post_upper_threshold )
301
316
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
317
+ #post_lower_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*0.8
318
+ #post_upper_threshold = (total_posts/(week_posts[len(week_posts)-1][0]/7/4))*1.2
319
+ post_lower_threshold = (total_posts / (len (week_posts )/ 4 ))* 0.8
320
+ post_upper_threshold = (total_posts / (len (week_posts )/ 4 ))* 1.2
304
321
post_months = post_month_params (week_posts , post_lower_threshold , post_upper_threshold )
305
322
306
323
serialize_q2 (country_sets , post_day_ranges )
0 commit comments