cleanup predictions

Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
This commit is contained in:
2017-08-27 15:15:30 -04:00
parent 1ff4ffadeb
commit a146ad3ec4
+3 -39
View File
@@ -1,49 +1,14 @@
import time
import records
import os
import maya
import numpy as np
import pandas as pd
import requests
from fbprophet import Prophet
from scraper import Coin
class MWT(object):
"""Memoize With Timeout"""
_caches = {}
_timeouts = {}
def __init__(self, timeout=2):
self.timeout = timeout
def collect(self):
"""Clear cache of results which have timed out"""
for func in self._caches:
cache = {}
for key in self._caches[func]:
if (time.time() - self._caches[func][key][1]) < self._timeouts[func]:
cache[key] = self._caches[func][key]
self._caches[func] = cache
def __call__(self, f):
self.cache = self._caches[f] = {}
self._timeouts[f] = self.timeout
def func(*args, **kwargs):
kw = sorted(kwargs.items())
key = (args, tuple(kw))
try:
v = self.cache[key]
if (time.time() - v[1]) > self.timeout:
raise KeyError
except KeyError:
v = self.cache[key] = f(*args, **kwargs), time.time()
return v[0]
func.func_name = f.__name__
return func
from scraper import Coin, MWT
@MWT(timeout=300)
@@ -52,7 +17,6 @@ def get_predictions(coin):
c = Coin(coin.lower())
q = "SELECT date as ds, value as y from api_coin WHERE name=:coin"
usd = requests.get('https://coinbin.org/btc').json()['coin']['usd']
pro_db = records.Database(os.environ['HEROKU_POSTGRESQL_TEAL_URL'])
rows = pro_db.query(q, coin=c.name)