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

Feature/tz cache lazy load #1709

Merged
merged 3 commits into from
Oct 1, 2023
Merged

Feature/tz cache lazy load #1709

merged 3 commits into from
Oct 1, 2023

Conversation

ValueRaider
Copy link
Collaborator

The initial singleton design pattern for database access meant that lazy-loading was broken,
due to structure of '_KV' class. So errors were blocking import.
Fix = use 'peewee' proxy database and initialise when needed.

The initial singleton design pattern for database access meant that lazy-loading was broken,
due to structure of '_KV' class. So errors were blocking import.
Fix = use 'peewee' proxy database and initialise when needed.
@rickturner2001
Copy link
Contributor

Lazy loading is working correctly but execution will fail on read-only file systems. Were you expecting this?

@ValueRaider
Copy link
Collaborator Author

No didn't expect that. Should be fixed now plus added some unit tests.

@rickturner2001
Copy link
Contributor

rickturner2001 commented Oct 1, 2023

There are still some issues with this

import yfinance as yf

# this works
ticker = yf.Ticker("MSFT")

# This returns an empty dataframe when the code is executed in Read only fs
df = yf.download("MSFT")

print(ticker.info)
print(df.tail())

output

[*********************100%%**********************]  1 of 1 completed
1 Failed download:
['MSFT']: _TzCacheException("Error creating TzCache folder: '/root/.cache/py-yfinance' reason: [Errno 30] Read-only file system: '/root/.cache/py-yfinance'")

'address1': 'One Microsoft Way', 'city': 'Redmond', 'state': 'WA', 'zip': '98052-6399', 'country': 'United States', 'phone': '425 882 8080', 'website': 'https://www.microsoft.com', 'industry': 'Software—Infrastructure', 'industryKey':

...

Empty DataFrame
Columns: [Open, High, Low, Close, Adj Close, Volume]
Index: []

@ValueRaider
Copy link
Collaborator Author

I can't reproduce with little effort, so can you re-run after yf.enable_debug_mode()

@rickturner2001
Copy link
Contributor

DEBUG    Entering download()
DEBUG     Disabling multithreading because DEBUG logging enabled
DEBUG     Entering history()
ERROR     
          1 Failed download:
ERROR     ['MSFT']: _TzCacheException("Error creating TzCache folder: '/root/.cache/py-yfinance' reason: [Errno 30] Read-only file system: '/root/.cache/py-yfinance'")
DEBUG     ['MSFT']: Traceback (most recent call last):
            File "/yfinance/utils.py", line 981, in _initialise
              _os.makedirs(cls._cache_dir)
            File "<frozen os>", line 225, in makedirs
          OSError: [Errno 30] Read-only file system: '/root/.cache/py-yfinance'
          
          During handling of the above exception, another exception occurred:
          
          Traceback (most recent call last):
            File "/yfinance/multi.py", line 275, in _download_one
              data = Ticker(ticker, session=session).history(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            File "/yfinance/utils.py", line 114, in wrapper
              result = func(*args, **kwargs)
                       ^^^^^^^^^^^^^^^^^^^^^
            File "/yfinance/base.py", line 148, in history
              tz = self._get_ticker_tz(proxy, timeout)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            File "/yfinance/base.py", line 1645, in _get_ticker_tz
              tz = cache.lookup(self.ticker)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
            File "/yfinance/utils.py", line 1035, in lookup
              self.initialise()
            File "/yfinance/utils.py", line 1028, in initialise
              db = _DBManager.get_database()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
            File "/yfinance/utils.py", line 961, in get_database
              cls._initialise()
            File "/yfinance/utils.py", line 983, in _initialise
              raise _TzCacheException(f"Error creating TzCache folder: '{cls._cache_dir}' reason: {err}")
          yfinance.utils._TzCacheException: Error creating TzCache folder: '/root/.cache/py-yfinance' reason: [Errno 30] Read-only file system: '/root/.cache/py-yfinance'
Reproduce locally

Dockerfile

FROM python:3.11

COPY . .
RUN pip install -r requirements.txt

CMD ["python", "your_test_script.py"]

Build the container

docker build -t yfinance-test .

Run the container in a read-only env

docker run --read-only yfinance-test

@ValueRaider
Copy link
Collaborator Author

ValueRaider commented Oct 1, 2023

Line numbers don't match code. Did you git pull ? It should be impossible for a _TzCacheException to propagate up, because it only has 1 route where it is caught.

@rickturner2001
Copy link
Contributor

Nope, you were right, I wasn't up to date. Everything works perfectly. Can you make a beta release?

@ValueRaider ValueRaider merged commit 38f8ccd into dev Oct 1, 2023
@ValueRaider ValueRaider deleted the feature/tz-cache-lazy-load branch October 1, 2023 21:40
@ValueRaider ValueRaider mentioned this pull request Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants