implemented new function and adjusted function call sequence

This commit is contained in:
2021-12-03 15:19:21 +01:00
parent a7dbdcf917
commit 8d18e03018
2 changed files with 41 additions and 39 deletions

View File

@ -4,7 +4,7 @@ from typing import Union, List, Dict
from aiohttp.client import ClientSession
from bs4 import BeautifulSoup
from bs4.element import Tag
from webutils import in_async_session
from webutils import in_async_session, gather_in_batches
from . import constants
@ -97,49 +97,49 @@ async def _get_single_company_fin_stmt(statement: str, ticker_symbol: str, quart
async def _get_multi_companies_fin_stmt(statement: str, *ticker_symbols: str, quarterly: bool = False,
concurrent_batch_size: int = constants.DEFAULT_CONCURRENT_BATCH_SIZE,
session: ClientSession = None) -> Union[ResultDict, Dict[str, ResultDict]]:
pass
if len(ticker_symbols) == 1:
return await _get_single_company_fin_stmt(statement, ticker_symbols[0], quarterly, session)
result_list = await gather_in_batches(
concurrent_batch_size,
*(_get_single_company_fin_stmt(statement, symbol, quarterly, session) for symbol in ticker_symbols)
)
return {symbol: data for symbol, data in zip(ticker_symbols, result_list)}
@in_async_session
async def get_balance_sheet(*ticker_symbols: str, quarterly: bool = False,
concurrent_batch_size: int = constants.DEFAULT_CONCURRENT_BATCH_SIZE,
session: ClientSession = None) -> Union[ResultDict, Dict[str, ResultDict]]:
"""
Returns data from the balance sheet of the specified company.
"""
if len(ticker_symbols) == 1:
return await _get_single_company_fin_stmt(constants.BS, ticker_symbols[0], quarterly, session)
return {
sym: await _get_single_company_fin_stmt(constants.BS, sym, quarterly, session)
for sym in ticker_symbols
}
return await _get_multi_companies_fin_stmt(constants.BS, *ticker_symbols,
quarterly=quarterly, concurrent_batch_size=concurrent_batch_size,
session=session)
@in_async_session
async def get_income_statement(*ticker_symbols: str, quarterly: bool = False,
concurrent_batch_size: int = constants.DEFAULT_CONCURRENT_BATCH_SIZE,
session: ClientSession = None) -> Union[ResultDict, Dict[str, ResultDict]]:
"""
Returns data from the income statement of the specified company.
"""
if len(ticker_symbols) == 1:
return await _get_single_company_fin_stmt(constants.IS, ticker_symbols[0], quarterly, session)
return {
sym: await _get_single_company_fin_stmt(constants.IS, sym, quarterly, session)
for sym in ticker_symbols
}
return await _get_multi_companies_fin_stmt(constants.IS, *ticker_symbols,
quarterly=quarterly, concurrent_batch_size=concurrent_batch_size,
session=session)
@in_async_session
async def get_cash_flow_statement(*ticker_symbols: str, quarterly: bool = False,
concurrent_batch_size: int = constants.DEFAULT_CONCURRENT_BATCH_SIZE,
session: ClientSession = None) -> Union[ResultDict, Dict[str, ResultDict]]:
"""
Returns data from the cash flow statement of the specified company.
"""
if len(ticker_symbols) == 1:
return await _get_single_company_fin_stmt(constants.CF, ticker_symbols[0], quarterly, session)
return {
sym: await _get_single_company_fin_stmt(constants.CF, sym, quarterly, session)
for sym in ticker_symbols
}
return await _get_multi_companies_fin_stmt(constants.CF, *ticker_symbols,
quarterly=quarterly, concurrent_batch_size=concurrent_batch_size,
session=session)
@in_async_session