From 3c22fe4d5c4c6b5df462e963d35e5f054e67aec0 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 10 Apr 2020 03:13:30 -0700 Subject: [PATCH] added documentation for more confusing functions --- bot.py | 7 +++---- functions.py | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 8521b8e..1b824b9 100644 --- a/bot.py +++ b/bot.py @@ -68,7 +68,7 @@ def dividend(update, context): if symbols: context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - for reply in s.symbol_name(symbols).items(): + for reply in s.dividend_reply(symbols).items(): update.message.reply_text( text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN @@ -87,7 +87,6 @@ def news(update, context): context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) for reply in s.news_reply(symbols).items(): - update.message.reply_text( text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN ) @@ -105,7 +104,6 @@ def info(update, context): context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) for reply in s.info_reply(symbols).items(): - update.message.reply_text( text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN ) @@ -113,7 +111,7 @@ def info(update, context): def inline_query(update, context): """ - Handles inline query. + Handles inline query. Does a fuzzy search on input and returns stocks that are close. """ print(update.inline_query.query) @@ -132,6 +130,7 @@ def inline_query(update, context): ) ) except TypeError: + logging.warning(str(match)) pass if len(results) == 5: diff --git a/functions.py b/functions.py index 6f3169a..08ec9fa 100644 --- a/functions.py +++ b/functions.py @@ -9,6 +9,10 @@ from fuzzywuzzy import fuzz class Symbol: + """ + Functions for finding stock market information about symbols. + """ + SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})" LIST_URL = "http://oatsreportable.finra.org/OATSReportableSecurities-SOD.txt" @@ -17,6 +21,13 @@ class Symbol: self.symbol_list, self.symbol_ts = self.get_symbol_list() def get_symbol_list(self): + """ + Fetches a list of stock market symbols from FINRA + + Returns: + pd.DataFrame -- [DataFrame with columns: Symbol | Issue_Name | Primary_Listing_Mkt + datetime -- The time when the list of symbols was fetched. The Symbol list is updated every open and close of every trading day. + """ raw_symbols = r.get(self.LIST_URL).text symbols = pd.DataFrame( [line.split("|") for line in raw_symbols.split("\n")][:-1] @@ -28,6 +39,15 @@ class Symbol: return symbols, datetime.now() def search_symbols(self, search: str): + """ + Performs a fuzzy search to find stock symbols closest to a search term. + + Arguments: + search {str} -- String used to search, could be a company name or something close to the companies stock ticker. + + Returns: + List of Tuples -- A list tuples of every stock sorted in order of how well they match. Each tuple contains: (Symbol, Issue Name). + """ if self.symbol_ts - datetime.now() > timedelta(hours=12): self.symbol_list, self.symbol_ts = self.get_symbol_list() @@ -48,14 +68,26 @@ class Symbol: def find_symbols(self, text: str): """ - Takes a blob of text and returns a list of symbols without any repeats. + Finds stock tickers starting with a dollar sign in a blob of text and returns them in a list. Only returns each match once. Example: Whats the price of $tsla? -> ['tsla'] + + Arguments: + text {str} -- Blob of text that might contain tickers with the format: $TICKER + + Returns: + list -- List of every found match without the dollar sign. """ return list(set(re.findall(self.SYMBOL_REGEX, text))) def price_reply(self, symbols: list): """ - Takes a list of symbols and returns a dictionary of strings with information about the symbol. + Takes a list of symbols and replies with Markdown formatted text about the symbols price change for the day. + + Arguments: + symbols {list} -- List of stock market symbols. + + Returns: + dict -- Dictionary with keys of symbols and values of markdown formatted text example: {'tsla': 'The current stock price of Tesla Motors is $**420$$, the stock price is currently **up 42%**} """ dataMessages = {} for symbol in symbols: @@ -80,7 +112,7 @@ class Symbol: return dataMessages - def symbol_name(self, symbols: list): + def dividend_reply(self, symbols: list): divMessages = {} for symbol in symbols: