From 1a7cfb71b6a0b4be320dbf17120978b56a880c77 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 15 Apr 2020 06:47:05 -0700 Subject: [PATCH] caching symbol search results --- functions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/functions.py b/functions.py index f83e53e..f48718c 100644 --- a/functions.py +++ b/functions.py @@ -15,6 +15,7 @@ class Symbol: SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})" LIST_URL = "http://oatsreportable.finra.org/OATSReportableSecurities-SOD.txt" + searched_symbols = {} def __init__(self, IEX_TOKEN: str): self.IEX_TOKEN = IEX_TOKEN @@ -48,6 +49,10 @@ class Symbol: Returns: List of Tuples -- A list tuples of every stock sorted in order of how well they match. Each tuple contains: (Symbol, Issue Name). """ + try: # https://stackoverflow.com/a/3845776/8774114 + return self.searched_symbols[search] + except KeyError: + pass if self.symbol_ts - datetime.now() > timedelta(hours=3): self.symbol_list, self.symbol_ts = self.get_symbol_list() @@ -65,7 +70,9 @@ class Symbol: symbols.sort_values(by="Match", ascending=False, inplace=True) symbols = symbols.head(10) - return list(zip(list(symbols["Symbol"]), list(symbols["Description"]))) + symbol_list = list(zip(list(symbols["Symbol"]), list(symbols["Description"]))) + self.searched_symbols[search] = symbol_list + return symbol_list def find_symbols(self, text: str): """