Exemplo n.º 1
0
            def get_torrent_info(self, torrent):
                # Dirty hack - make torrent timeout if we have seen it timeout in the past (cache)
                torrent_timeout_id = '%s_timeout' % torrent.hash
                if get_cache(torrent_timeout_id):
                    from datetime import timedelta
                    log.info('Torrent timeout found in cache for %s', torrent.hash)
                    torrent.last_status_change -= timedelta(weeks=100)
                    torrent.save()

                # torrent_info retrieval/cache
                cached_content = get_cache(torrent.hash)
                if cached_content:
                    log.info('Torrent info found in cache for %s', torrent.hash)
                    return cached_content
                else:
                    torrent_bt = default_get_torrent_info(self, torrent)
                    # Only cache once we've got the metadata
                    if torrent_bt.has_metadata:
                        log.info('Adding torrent info in cache for %s', torrent.hash)
                        set_cache(torrent.hash, torrent_bt)
                    # Or if the torrent is timeout
                    elif torrent.is_timeout(settings.BITTORRENT_METADATA_TIMEOUT):
                        log.info('Recording torrent timeout in cache for %s', torrent.hash)
                        set_cache(torrent_timeout_id, True)
                    return torrent_bt
Exemplo n.º 2
0
 def test_with_game_1(self):
     func = with_game(lambda *a, **k: (a, k))
     # test decorator, tokens are not in cache
     with patch('decorators.send_error') as mock:
         mock.return_value = '_error'
         self.assertEqual(func('1234'), '_error')
         mock.assert_called_once_with('game not found')
     # test decorator, tokens are in cache
     game = Game.create(white='1234', black='qwer')
     self.assertEqual(func('1234')[0][0].model.pk, game.pk)
     self.assertEqual(func('qwer')[0][0].model.pk, game.pk)
     # waited game
     set_cache('wait_5678', (consts.TYPE_FAST, 3600))
     with patch('decorators.send_data') as mock:
         func('5678')
         mock.assert_called_once_with({'type': 'fast', 'limit': 3600})
     # invited game
     set_cache('wait_asdf', (consts.TYPE_FAST, 3600, 'zxcv'))
     with patch('decorators.send_data') as mock:
         func('asdf')
         mock.assert_called_once_with({
             'type': 'fast',
             'limit': 3600,
             'invite': 'zxcv'
         })
Exemplo n.º 3
0
    def bankrisk(innercode, checkdate):
        innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.bankRiskCache)
        #innercodeCalc=innercode
        result = df(result,columns=('innercode', 'es'))

        if len(innercodeCalc) != 0:
            query="""SELECT
                        financedata.bank_fin_prd.INNER_CODE,
                        financeprd.bank_risk.VAR
                    FROM
                        financedata.bank_fin_prd,
                        financeprd.bank_risk
                    WHERE
                        financedata.bank_fin_prd.INNER_CODE IN ( %s )
                    AND
                        financeprd.bank_risk.BANK_ID = financedata.bank_fin_prd.BANK_ID """ % ','.join(innercodeCalc)
            result_new = GetDataFromDB(config, query)
            result_new.columns = ['innercode', 'es']
            innercode_empty=df(columns=['innercode', 'es'])
            innercode_empty['innercode']=list(set(innercodeCalc)-set(result_new['innercode'].astype(str)))
            innercode_empty['es']=''
            innercode_empty.columns=['innercode','es']
            result_new=result_new.append(innercode_empty,ignore_index=1)
            #save cache for new data
            ca.set_cache(result_new=result_new, checkdate=checkdate,cache=ca.bankRiskCache)

            result=result.append(result_new,ignore_index=1)
            result['checkdate'] = checkdate
        else:
            result['checkdate'] = checkdate
        return (result.to_json(orient='index'))
Exemplo n.º 4
0
 def decorator(*args, **kwargs):
     fn = name if name else '.'.join([f.__module__, f.__name__])
     cache_name = get_cache_func_name(fn, *args, **kwargs)
     results = get_cache(cache_name)
     if results is not None:
         return results
     results = f(*args, **kwargs)
     set_cache(cache_name, results, time)
     return results
Exemplo n.º 5
0
 def get_url(url):
     url_id = url.replace('/', '_')
     cached_content = get_cache(url_id)
     if cached_content:
         return cached_content
     else:
         content = default_get_url(url)
         set_cache(url_id, content)
         return content
Exemplo n.º 6
0
 def draw_accept(self, color=None):
     if self.model.ended:
         raise errors.EndGame
     color = self.get_color(color)
     set_cache(self._get_draw_name(color), True)
     if self.check_draw():
         return self.get_info()
     self.send_ws('opponent offered draw', consts.WS_DRAW_REQUEST,
                  invert_color(color))
Exemplo n.º 7
0
 def authenticate(cls, username, password):
     try:
         user = cls.get(username=username,
                        password=encrypt_password(password))
     except cls.DoesNotExist:
         return False
     token = generate_token()
     set_cache(token, user.pk, config.SESSION_TIME)
     return token
Exemplo n.º 8
0
 def test_get_by_token(self):
     # token not exist
     self.assertIsNone(User.get_by_token('asdfgh'))
     # user not exist
     set_cache('asdfgh', 1)
     self.assertIsNone(User.get_by_token('asdfgh'))
     # success
     user = User.add('user1', 'passwd')
     set_cache('asdfgh', user.pk)
     self.assertEqual(User.get_by_token('asdfgh'), user)
Exemplo n.º 9
0
 def _post(data):
     game_type = data['type']
     game_limit = data['limit']
     token = generate_token(True)
     pool = GamePool.create(
         player1=token,
         user1=request.user,
         type_game=game_type,
         time_limit=game_limit,
     )
     set_cache('wait_{}'.format(token), (game_type, game_limit))
     return send_data({'game': token})
Exemplo n.º 10
0
 def test_with_game_2(self, request):
     request.json.get.return_value = 'user_token'
     # create game with white user and try to get it anonymously
     func = authenticated(with_game(lambda *a, **k: (a, k)))
     user = User.create(username='******', password='******')
     game = Game.create(white='1234', black='qwer', player_white=user)
     with patch('decorators.send_error') as mock:
         func('1234')
         mock.assert_called_once_with('wrong user')
     self.assertEqual(func('qwer')[0][0].model.pk, game.pk)
     # set auth and check again
     set_cache('user_token', user.pk)
     self.assertEqual(func('1234')[0][0].model.pk, game.pk)
Exemplo n.º 11
0
 def get_reset(self):
     if not self.email:
         raise AttributeError('your email is not defined')
     if self.date_last_reset:
         seconds = (datetime.now() - self.date_last_reset).total_seconds()
         if seconds < config.RESET_PERIOD:
             raise errors.ResetRequestError(config.RESET_PERIOD -
                                            int(seconds))
     token = generate_token()
     set_cache(token, self.pk, config.RESET_TIME)
     self.date_last_reset = datetime.now()
     self.save()
     return token
Exemplo n.º 12
0
 def get_verification(self):
     if self.verified:
         raise errors.VerifiedError
     if not self.email:
         raise AttributeError('your email is not defined')
     if self.date_verification_token:
         seconds = (datetime.now() -
                    self.date_verification_token).total_seconds()
         if seconds < config.VERIFICATION_PERIOD:
             raise errors.VerificationRequestError(
                 config.VERIFICATION_PERIOD - int(seconds))
     token = generate_token()
     set_cache(token, self.pk, config.VERIFICATION_TIME)
     self.date_verification_token = datetime.now()
     self.save()
     return token
Exemplo n.º 13
0
 def test_authenticated(self, request):
     func = authenticated(lambda *a, **k: 'success')
     request.json.get.return_value = '1234'
     # test decorator, token is not in cache
     self.assertEqual(func(), 'success')
     self.assertIsNone(request.user)
     self.assertIsNone(request.auth)
     # test decorator, token is in cache, no user
     set_cache('1234', 1)
     self.assertEqual(func(), 'success')
     self.assertIsNone(request.user)
     self.assertIsNone(request.auth)
     # test decorator success
     user = User.create(username='******', password='******')
     set_cache('1234', user.pk)
     self.assertEqual(func(), 'success')
     self.assertEqual(request.user.pk, user.pk)
     self.assertEqual(request.auth, '1234')
Exemplo n.º 14
0
    def bankrisk(innercode, checkdate):
        innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.bankRiskCache)
        if len(innercodeCalc) != 0:
            ro.r.assign("innercode", innercode)
            ro.r("innercode=as.character(innercode)")

            ro.r("res=bankRisk(innercode)")

            innercode = list(ro.r("as.character(res$innercode)"))
            es = list(ro.r("as.character(res$es)"))
            # result = [innercode, es]
            ca.set_cache(innercode=innercode, checkdate=checkdate, es=es, cache=ca.bankRiskCache)

            result[0] = result[0] + innercode
            result[1] = result[1] + es
        else:
            pass

        return result
Exemplo n.º 15
0
def get_word(value):
    '''Return the entire feed of a word from cache or API.'''
    wd = get_dictionary()

    filename = 'dictionary_word_{}.pyf'.format(value)
    success, val = get_cache(filename)
    if success is True:
        print('returning cache word')
        return val

    val = dict(
        value=value,
        meaning=wd.meaning(value),
        synonym=wd.synonym(value),
        antonym=wd.antonym(value),
    )

    set_cache(filename, val)
    return val
Exemplo n.º 16
0
 def downloadData(self, airport_icao, flight_number=None):
   infra = Infraero.Harvester()
   if flight_number:
       key = airport_icao+str(int(flight_number))
       flight = get_cache(key)
       if not flight:
           flights = infra.request_flight(airport_icao, flight_number)
           set_cache(key, flights[0], 60*5)
           flights[0].append( ('from_cache','False') )
       else:
           flights = [ flight ]
           flights[0].append( ('from_cache','True') )
   else:
       key = airport_icao
       flights = get_cache(key)
        
       if not flights:
           flights = infra.request_airport(airport_icao)
           set_cache(key, flights, 60*5)
           for flight in flights:
               set_cache(key+str(int(flight[1][1])), flight, 60*5)
               flight.append( ('from_cache','False') )
       else:
           for flight in flights:
               flight.append( ('from_cache','True') )
   return flights
Exemplo n.º 17
0
def invite(data):
    game_type = data['type']
    game_limit = data['limit']
    if game_type != consts.TYPE_NOLIMIT and not game_limit:
        return send_error('game limit must be set for no limit game')
    token_game = generate_token(True)
    token_invite = generate_token(True)
    set_cache('invite_{}'.format(token_invite),
              (token_game, game_type, game_limit))
    if request.user:
        set_cache('user_{}'.format(token_game), request.user.pk, 3600)
    set_cache('wait_{}'.format(token_game),
              (game_type, game_limit, token_invite))
    return send_data({
        'game': token_game,
        'invite': token_invite,
    })
Exemplo n.º 18
0
 def post(self):
     game_type = self.data['type']
     game_limit = self.data['limit']
     if game_type != consts.TYPE_NOLIMIT and not game_limit:
         raise errors.APIException(
             'game limit must be set for no limit game')
     token_game = generate_token(True)
     token_invite = generate_token(True)
     set_cache('invite_{}'.format(token_invite),
               (token_game, game_type, game_limit))
     if request.user:
         set_cache('user_{}'.format(token_game), request.user.pk, 3600)
     set_cache('wait_{}'.format(token_game),
               (game_type, game_limit, token_invite))
     return {
         'game': token_game,
         'invite': token_invite,
     }
Exemplo n.º 19
0
 def test_cache_1(self):
     self.assertEqual(get_cache('nokey'), None)
     set_cache('key', 'data')
     self.assertEqual(get_cache('key'), 'data')
     delete_cache('key')
     self.assertEqual(get_cache('key'), None)
Exemplo n.º 20
0
 def _set_cache(self, query):
     if AWS_ADMIN:
         set_cache(self.caller_name, query, self._temp_results,
                   self._cache_result)
Exemplo n.º 21
0
 def test_cache_2(self):
     set_cache('key', 'data', 1)
     self.assertEqual(get_cache('key'), 'data')
     time.sleep(1)
     self.assertEqual(get_cache('key'), None)
Exemplo n.º 22
0
 def test_cache_3(self):
     set_cache('key', (1, True, 'test'))
     self.assertEqual(get_cache('key'), (1, True, 'test'))
     set_cache('key', {'k1': 'v1', 'k2': True})
     self.assertEqual(get_cache('key'), {'k1': 'v1', 'k2': True})
Exemplo n.º 23
0
 def expired(cls, key):
     set_cache()
Exemplo n.º 24
0
from config.database.expired_callback import ExpiredCallback
from settings import PORT, HOST
from config.database import app, db
from cache import set_cache
from views import home, good, category, store, group, user, other, admin

app.register_blueprint(home)
app.register_blueprint(good)
app.register_blueprint(category)
app.register_blueprint(store)
app.register_blueprint(group)
app.register_blueprint(user)
app.register_blueprint(other)
app.register_blueprint(admin)

if __name__ == '__main__':
    db.create_all()
    set_cache()
    ExpiredCallback.monitor()
    app.run(host=HOST, port=PORT, debug=True)
Exemplo n.º 25
0
    def fundrisk(innercode, checkdate):
        """
        innercode from url:param innercode:
        checkdate from url:param checkdate:
        reult fundrisk with json type:return:
        """
        innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.fundRiskCache)
        """
        check the cache for the expected short fall already existed
        """
        #innercodeCalc=innercode
        result = df(result,columns=('innercode', 'es'))
        if len(innercodeCalc) != 0:
            startdate = datetime.strptime(checkdate, '%Y-%m-%d') - timedelta(days=730)
            query = """select
              financedata.ana_fnd_nav_calc.TRADEDATE,
              financedata.ana_fnd_nav_calc.fac_unit_net,
              financedata.ana_fnd_nav_calc.INNER_CODE
              from
              financedata.ana_fnd_nav_calc
              where
              financedata.ana_fnd_nav_calc.inner_code in ( %s )
              and
              financedata.ana_fnd_nav_calc.tradedate<= '%s'
              and
              financedata.ana_fnd_nav_calc.tradedate>= '%s'
              """ % (','.join(innercodeCalc), checkdate, startdate.strftime('%Y-%m-%d'))
            """
            asseemble the query
            """

            indexPrice = GetDataFromDB(config, query)
            indexPrice.columns = ['tradedate', 'close', 'innercode']
            """
            get date from db
            """
            grouped = indexPrice.groupby('innercode')
            result_new = df(columns=('innercode', 'es'))


            for i in innercodeCalc:
                try:
                    dataForCalc = grouped.get_group(int(i))
                    es = cr.es(dataForCalc['tradedate'], dataForCalc['close'])
                    result_new.loc[len(result_new.index)]=[i,es]
                except KeyError:
                    es = ''
                    result_new.loc[len(result_new.index)]=[i,es]

            """
            constract the result maxtrix which innercode cannot be found in database
            with empty es
            """


            #save cache for new data
            ca.set_cache(result_new=result_new, checkdate=checkdate,cache=ca.fundRiskCache)
            """
            save the result in cache
            """
            result = result.append(result_new,ignore_index=1)
            result['checkdate'] = checkdate
        else:
            result['checkdate'] = checkdate

        return (result.to_json(orient='index'))