From a146ad3ec43a08c8f622cd2f3e606e596225c2d5 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 27 Aug 2017 15:15:30 -0400 Subject: [PATCH] cleanup predictions Signed-off-by: Kenneth Reitz --- predictions.py | 42 +++--------------------------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/predictions.py b/predictions.py index 8465bcc..1035da2 100644 --- a/predictions.py +++ b/predictions.py @@ -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)