Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache error on read only system #1108

Merged
merged 3 commits into from Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion yfinance/__init__.py
Expand Up @@ -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"
Expand All @@ -42,4 +43,4 @@ def pdr_override():
pass


__all__ = ['download', 'Ticker', 'Tickers', 'pdr_override']
__all__ = ['download', 'Ticker', 'Tickers', 'pdr_override', 'set_tz_cache_location']
6 changes: 5 additions & 1 deletion yfinance/base.py
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion yfinance/utils.py
Expand Up @@ -316,8 +316,17 @@ 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")
return dp
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):
Expand Down