def test_future_years(self): nums = [random.randint(1, 1000) for range_index in xrange(1000)] for num in nums: future = when.future(years=num) leap_days = when.how_many_leap_days(self.now, future) now_plus = when.now() + self.one_day * (num * 365 + leap_days) self.assertTrue(now_plus - future < self.one_second)
def __init__(self, text, lang): self.lang = lang self.stem = text.strip().split('\t')[0] # [[query, freq, timestamp]] self.query_items = map( lambda t: t.strip().split(':'), text.strip().split('\t')[1:]) self.querys = [item[0] for item in self.query_items] self.querys = list(set(self.querys)) self.this_year = when.past(years=0).strftime("%Y") self.last_year = when.past(years=1).strftime("%Y") self.next_year = when.future(years=1).strftime("%Y") self.year_before_last = when.past(years=2).strftime("%Y") self.recent_three_month = [ self.month_name_helper(i) for i in xrange(3)] self.total_freq = 0 self.this_year_freq = 0 self.last_year_freq = 0 self.recent_three_month_freq = 0 self.month_freq_dict = defaultdict(int) self.processors = [] self.main_request_type = None self.main_request_degree = 0 for i in dir(self): if i.startswith('general_processor'): self.add_analyze_processor(getattr(self, i)) for i in dir(self): if i.startswith(self.lang + '_processor'): self.add_analyze_processor(getattr(self, i))
def test_future_months(self): nums = [random.randint(1, 1000) for range_index in xrange(1000)] for num in nums: future = when.future(months=num) now_plus = when.now() + self.one_month * num self.assertTrue( (now_plus - future) < self.one_day * 3 * num)
def set_access_token(self, access_token=None, expires_in=None, expires=None, refresh_token=None, openid=None): self.access_token = access_token if expires_in: self.expires = when.future(seconds=int(expires_in)) elif expires: self.expires = expires self.refresh_token = refresh_token self.openid = openid
def obtain_token(user): token, created = Token.objects.get_or_create(user=user) token.expire_time = when.future(months=1) token.save() return token
def get(self, request): u""" 获取图信息(包括曲线历史数据) --- parameters: - name: id description: 图id paramType: query required: True """ data = request.query_params.dict() chart_id = data['id'] chart = Chart.objects.get(pk=chart_id) chart_lines = ChartLine.objects.filter(chart_id=chart_id) data = [] if chart.compare == Chart.COMPARE_YEAR: date, start, end = [], None, None for i in range(3): year = when.future(years=-i).strftime('%Y-01-01') if start: end = start - datetime.timedelta(days=1) else: end = None start = datetime.datetime.strptime(year, '%Y-%m-%d').date() date.insert(0, (start, end)) for chart_line in chart_lines: line_data = ProxyData(chart_line.data_code, chart_line.table_name) year_data = [] for start, end in date: line_data.start = start line_data.end = end year_data.append({'year': start.strftime('%Y'), 'data': line_data.get_list(timestamp=True)}) data.append({'title': line_data.title, 'unit': line_data.unit, 'line': year_data, 'line_type': line_data.table, 'count': line_data.count(), 'data_code': line_data.data_code}) elif chart.compare == Chart.COMPARE_MONTH: date, start, end = [], None, None for i in range(3): month = when.future(months=-i).strftime('%Y-%m-01') if start: end = start - datetime.timedelta(days=1) else: end = None start = datetime.datetime.strptime(month, '%Y-%m-%d').date() date.insert(0, (start, end)) for chart_line in chart_lines: line_data = ProxyData(chart_line.data_code, chart_line.table_name) month_data = [] for start, end in date: line_data.start = start line_data.end = end month_data.append({'month': start.strftime('%Y-%m'), 'data': line_data.get_list(timestamp=True)}) data.append({'title': line_data.title, 'unit': line_data.unit, 'line': month_data, 'line_type': line_data.table, 'count': line_data.count(), 'data_code': line_data.data_code}) else: six_month_ago = when.future(months=-6).date() for chart_line in chart_lines: line_data = ProxyData(chart_line.data_code, chart_line.table_name, start=six_month_ago) line_data = line_data.get_all(timestamp=True) line_data['title'] = chart_line.line_name data.append(line_data) return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL, data=data, message='获取图数据成功').to_response()