Пример #1
0
    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))
Пример #2
0
 def test_past_years(self):
     nums = [random.randint(1, 1000) for range_index in xrange(1000)]
     for num in nums:
         past = when.past(years=num)
         leap_days = when.how_many_leap_days(past, self.now)
         now_plus = when.now() - self.one_day * (num * 365 + leap_days)
         self.assertTrue(past - now_plus < self.one_second)
Пример #3
0
    def test_past_months(self):
        nums = [random.randint(1, 1000) for range_index in xrange(1000)]
        for num in nums:
            past = when.past(months=num)
            now_minus = when.now() - self.one_month * num

            self.assertTrue(
                (past - now_minus) < self.one_day * 3 * num)
Пример #4
0
def get_30days_start_end_time(st, ed):
    today = when.today()
    thirty_days_ago = when.past(days=30).date()
    format_str = '%s %s'

    if st < ed:
        st_time_str = format_str % (thirty_days_ago, st)
        ed_time_str = format_str % (today, ed)

        return st_time_str, ed_time_str

    tomorrow = when.tomorrow()
    st_time_str = format_str % (thirty_days_ago, st)
    ed_time_str = format_str % (tomorrow, ed)

    return st_time_str, ed_time_str
Пример #5
0
def month_re_cal():
    """每天重复计算一个月内的数据"""
    db = Session()
    var_obj = db.execute('select code, exchange from varieties ')
    var_vals = var_obj.fetchall()
    exchange_map = dict(var_vals)
    db.close()
    now = datetime.now()
    last_month = when.past(months=1)
    for i in range((now - last_month).days + 1):
        date = str(last_month + timedelta(days=i))[:10]
        # ctp日数据
        for varieties in DAY_VARIEYIES_DICT.keys():
            exchange = exchange_map.get(varieties.lower())
            if not exchange:
                exchange = exchange_map.get(varieties.upper())
            cal_day_info(varieties, date, exchange)
        # 总资产
        update_output(date)
Пример #6
0
    def get(self, request):
        u"""
        获取预警提示,主力合约,预警类别信息
        ---

        parameters:
            - name: alert_id
              description: 预警的id
              paramType: query
              required: true
            - name: limit
              description: 数据条数
              paramType: query
              required: false

        """
        query_dict = request.query_params.dict().copy()
        alert_id = query_dict.get('alert_id', None)
        limit = int(query_dict.get('limit', 3))
        logger.info('查看预警历史数据, 预警id: %s, 查看数量: %s' % (alert_id, limit))
        leta = math.floor(limit / 30)
        if leta < 1:
            leta = 1
        # now = datetime.datetime.strptime(str(datetime.datetime.now())[10] + ' 00:00:00', '%Y-%m-%d %H:%M:%S')
        past_day = datetime.datetime.strptime(
            str(when.past(months=leta))[:10] + ' 00:00:00',
            '%Y-%m-%d %H:%M:%S')
        today = datetime.datetime.today()
        if not alert_id:
            return BackstageHTTPResponse(
                code=BackstageHTTPResponse.API_HTTP_CODE_INVILID_PARAMS,
                message=u'预警id不存在').to_response()
        alert_obj = Alert.objects.get(pk=alert_id)
        variety = alert_obj.variety
        price = alert_obj.price
        # source = alert_obj.source
        # exchange = alert_obj.exchange
        contract = alert_obj.contract

        _y = []

        data, date_time = get_history_data(variety,
                                           'CLOSE',
                                           limit=limit,
                                           contract=contract,
                                           start=past_day,
                                           end=today)
        y = list(zip(date_time, data))
        # for i in range(len(data)):
        #     if datetime.datetime.fromtimestamp(date_time[i]/1000) < past_day:
        #         break
        #     y.append([date_time[i], data[i]])
        new_vals = Ship(variety,
                        price,
                        start=None,
                        end=None,
                        limit=None,
                        offset=None,
                        desc=True)
        para_unit = new_vals.unit or ''
        y.reverse()
        res = {
            'y': y,
            'y_max': max(data),
            'y_min': min(data),
            'y_name': '主力合约',
            'y_unit': '元',
            '_y_name': para_name_map[price],
            '_y_unit': para_unit,
            'para_type': price,
            'date_end': str(datetime.datetime.now())[:10],
            'date_start': str(past_day)[:10],
        }

        if price == 'CROSS_STAR':
            para_vals = get_cross_star(variety, past_day, limit)
            res.update({
                '_y': para_vals,
                '_y_max': None,
                '_y_min': None,
            })
        else:
            para_data, para_date_time = get_history_data(variety,
                                                         price,
                                                         limit=limit,
                                                         start=past_day,
                                                         end=today)
            # for i in range(len(para_data)-1):
            #     # print(datetime.datetime.fromtimestamp(para_date_time[i] / 1000))
            #     if datetime.datetime.fromtimestamp(para_date_time[i] / 1000) < past_day:
            #         break
            #     _y.append([para_date_time[i], para_data[i]])
            _y = list(zip(para_date_time, para_data))

            _y.reverse()
            res.update({
                '_y': _y,
                '_y_max': max(para_data),
                '_y_min': min(para_data),
            })

        logger.info('正常返回预警详细数据')
        return BackstageHTTPResponse(data=res,
                                     message=u'正常返回预警详细数据').to_response()
Пример #7
0
import when
datadir = '../data/'
mid_datadir = '../mid_data/'
final_datadir = '../final_data/'
dict_dir = '../dict/'
file_suffix = when.now().strftime("%Y%m%d")
newest_time = when.past(0, 3).strftime("%Y%m%d")

gentime_type = {'year': ['1', '0'], 'newest': ['3', newest_time],
                '2014': ['2', '0'], '2013': ['3', '20130101']}
Пример #8
0
 def month_name_helper(self, i):
     return when.past(months=i).strftime('%Y%m')