Created
March 24, 2023 14:24
-
-
Save freelancing-solutions/78932c70c737dfc9950fb9808675ebff to your computer and use it in GitHub Desktop.
code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def download_eod_and_store(self, _stock, period, session): | |
""" | |
**download_and_store** | |
download eod data and then store | |
:param _stock: | |
:param period: | |
:param session: | |
:return: | |
""" | |
_data = None | |
with self.lock: | |
try: | |
_data = yf.download(_stock.code, start=period[0], end=period[1], interval=self._interval.interval, | |
ignore_tz=True, repair=True, timeout=15) | |
except (KeyError, RuntimeError) as e: | |
message: str = f""" | |
Runtime or KeyError | |
Unable to download eod data for {_stock.name}, | |
method : download_eod_and_store | |
debug: {str(e)} | |
stock_code: {_stock.code} | |
""" | |
self._logger.error(message) | |
return | |
except (ReadTimeout, JSONDecodeError, ConnectionError) as e: | |
message: str = f""" | |
Remote or Response Error | |
Unable to connect or getting bad response for {_stock.name}, | |
method : download_eod_and_store | |
debug: {str(e)} | |
stock_code: {_stock.code} | |
""" | |
self._logger.error(message) | |
return | |
_data['date'] = pd.to_datetime(_data.index).date | |
map_partial = functools.partial(self.map_eod_data, stock=_stock) | |
eod_instances_list = [map_partial(line=_line) for _line in _data.itertuples()] | |
_eod_instances = await asyncio.gather(*eod_instances_list) | |
_put_tasks = [] | |
eod_instances = list(_eod_instances) | |
for i in range(0, len(eod_instances), self._limit): | |
short_eod_instance_list: list[EODData] = eod_instances[i: i + self._limit] | |
_put_tasks.append(EODData.save_all(instance_list=short_eod_instance_list, session=session)) | |
self._count += len(short_eod_instance_list) | |
await asyncio.gather(*_put_tasks) | |
mess: str = f"Added a total of : {self._count} EOD Data for the period of {period}" | |
config_instance().DEBUG and self._logger.info(mess) |
it made new functions for each operation. Did it speed it up any? How do you run the function?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This part does the downloading
So what ChatGPT could not figure out is that download is already separate from this method
all this is just processing
`
_data['date'] = pd.to_datetime(_data.index).date
map_partial = functools.partial(self.map_eod_data, stock=_stock)
`