From ef12c8b600cd9a16e36610a8ab9903bd00b50efd Mon Sep 17 00:00:00 2001 From: ValueRaider Date: Sun, 23 Oct 2022 19:29:54 +0100 Subject: [PATCH 1/3] Catch read-only exceptions during cache write --- yfinance/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/yfinance/base.py b/yfinance/base.py index 62fcfaa36..a0563417a 100644 --- a/yfinance/base.py +++ b/yfinance/base.py @@ -339,7 +339,11 @@ def _get_ticker_tz(self): if tkr_tz is None: tkr_tz = self.info["exchangeTimezoneName"] # info fetch is relatively slow so cache timezone - utils.cache_store_tkr_tz(self.ticker, tkr_tz) + try: + utils.cache_store_tkr_tz(self.ticker, tkr_tz) + except PermissionError: + # System probably read-only, so cannot cache + pass self._tz = tkr_tz return tkr_tz From f525ee2f5e8824b3a14e42f3fcfb4b1cc9fd9c2e Mon Sep 17 00:00:00 2001 From: ValueRaider Date: Sun, 23 Oct 2022 19:47:22 +0100 Subject: [PATCH 2/3] Add README section on tz-cache ; Add set_tz_cache_location() --- README.md | 14 ++++++++++++-- yfinance/__init__.py | 3 ++- yfinance/utils.py | 10 +++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 639a340b4..7c16e9810 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,6 @@ Yahoo! finance API is intended for personal use only.** The `Ticker` module, which allows you to access ticker data in a more Pythonic way: -Note: yahoo finance datetimes are received as UTC. - ```python import yfinance as yf @@ -214,6 +212,18 @@ data = yf.download( # or pdr.get_data_yahoo(... ) ``` +### Timezone cache store + +When fetching price data, all dates are localized to stock exchange timezone. +But timezone retrieval is relatively slow, so yfinance attemps to cache them +in your users cache folder. +You can direct cache to use a different location with `set_tz_cache_location()`: +```python +import yfinance as yf +yf.set_tz_cache_location("custom/cache/location") +... +``` + ### Managing Multi-Level Columns The following answer on Stack Overflow is for [How to deal with diff --git a/yfinance/__init__.py b/yfinance/__init__.py index 5e87a98e3..737104531 100644 --- a/yfinance/__init__.py +++ b/yfinance/__init__.py @@ -23,6 +23,7 @@ from .ticker import Ticker from .tickers import Tickers from .multi import download +from .utils import set_tz_cache_location __version__ = version.version __author__ = "Ran Aroussi" @@ -42,4 +43,4 @@ def pdr_override(): pass -__all__ = ['download', 'Ticker', 'Tickers', 'pdr_override'] +__all__ = ['download', 'Ticker', 'Tickers', 'pdr_override', 'set_tz_cache_location'] diff --git a/yfinance/utils.py b/yfinance/utils.py index d611575c3..f85e5a823 100644 --- a/yfinance/utils.py +++ b/yfinance/utils.py @@ -316,8 +316,16 @@ def __str__(self): # Simple file cache of ticker->timezone: +_cache_dp = None def get_cache_dirpath(): - return _os.path.join(_ad.user_cache_dir(), "py-yfinance") + if _cache_dp is None: + dp = _os.path.join(_ad.user_cache_dir(), "py-yfinance") + else: + dp = _os.path.join(_cache_dp, "py-yfinance") +def set_tz_cache_location(dp): + global _cache_dp + _cache_dp = dp + def cache_lookup_tkr_tz(tkr): fp = _os.path.join(get_cache_dirpath(), "tkr-tz.csv") if not _os.path.isfile(fp): From 90e00a71caf934ae17ed103afe2d9ef5eb63a89d Mon Sep 17 00:00:00 2001 From: ValueRaider Date: Sun, 23 Oct 2022 19:51:09 +0100 Subject: [PATCH 3/3] Fix missing 'return' --- yfinance/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/yfinance/utils.py b/yfinance/utils.py index f85e5a823..131c0df66 100644 --- a/yfinance/utils.py +++ b/yfinance/utils.py @@ -322,6 +322,7 @@ def get_cache_dirpath(): dp = _os.path.join(_ad.user_cache_dir(), "py-yfinance") else: dp = _os.path.join(_cache_dp, "py-yfinance") + return dp def set_tz_cache_location(dp): global _cache_dp _cache_dp = dp