Skip to content

Instantly share code, notes, and snippets.

@freelancing-solutions
Created March 24, 2023 14:24
Show Gist options
  • Save freelancing-solutions/78932c70c737dfc9950fb9808675ebff to your computer and use it in GitHub Desktop.
Save freelancing-solutions/78932c70c737dfc9950fb9808675ebff to your computer and use it in GitHub Desktop.
code snippet
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)
@freelancing-solutions
Copy link
Author

This part does the downloading

data = yf.download(_stock.code, start=period[0], end=period[1], interval=self._interval.interval,
                                    ignore_tz=True, repair=True, timeout=15)

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)

    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)

`

@bonosa
Copy link

bonosa commented Mar 24, 2023

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