Skip to content

Commit

Permalink
Resolve Incompatible types in assignment in linter
Browse files Browse the repository at this point in the history
  • Loading branch information
edoaltamura committed Jun 10, 2024
1 parent d7530e1 commit ef53f9e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 70 deletions.
50 changes: 23 additions & 27 deletions qiskit_finance/data_providers/data_on_demand_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,36 @@ def __init__(
- If a string is provided, it can be a single ticker symbol or multiple symbols
separated by semicolons or newlines.
- If a list of strings is provided, each string should be a single ticker symbol.
Default is None.
Default is :code:`None`.
start (datetime.datetime): Start date of the data.
Defaults to January 1st, 2016.
end (datetime.datetime): End date of the data.
Defaults to January 30th, 2016.
verify (str | bool | None): If verify is None, certify certificates
will be used (default);
if this is False, no certificates will be checked; if this is a string,
verify (str | bool | None): If verify is `None`, runs the certificate verification (default);
if this is :code:`False`, no certificates will be checked; if this is a :code:`str`,
it should be pointing
to a certificate for the HTTPS connection to NASDAQ (`dataondemand.nasdaq.com`),
either in the
form of a CA_BUNDLE file or a directory wherein to look.
to a certificate for the HTTPS connection to NASDAQ (:code:`dataondemand.nasdaq.com`),
either in the form of a :code:`CA_BUNDLE` file or a directory wherein to look.
"""
super().__init__()

self._tickers = None
tickers = tickers if tickers is not None else []
if isinstance(tickers, list):
self._tickers = tickers
else:
self._tickers = tickers.replace("\n", ";").split(";")
self._n = len(self._tickers)
if tickers is None:
tickers = []
if isinstance(tickers, str):
tickers = tickers.replace("\n", ";").split(";")

self._tickers = tickers
self._n = len(tickers)
self._token = token
self._start = start
self._end = end
self._verify = verify

def run(self) -> None:
"""
Loads data, thus enabling get_similarity_matrix and get_covariance_matrix
Loads data, thus enabling :code:`get_similarity_matrix` and :code:`get_covariance_matrix`
methods in the base class.
"""

http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED", ca_certs=certifi.where())
url = "https://dataondemand.nasdaq.com/api/v1/quotes?"
self._data = []
Expand All @@ -96,23 +92,23 @@ def run(self) -> None:
"end": self._end.strftime("%Y-%m-%d'T'%H:%M:%S.%f'Z'"),
"next_cursor": 0,
}
encoded = url + urlencode(values)
encoded_url = f"{url}{urlencode(values)}"
if self._verify is None:
response = http.request(
"POST", encoded
) # this runs certificate verification, as per the set-up of the urllib3
# Runs certificate verification, as per the set-up of the urllib3
response = http.request("POST", encoded_url)
else:
# this disables certificate verification (False)
# Disables certificate verification (False)
# or forces the certificate path (str)
response = http.request("POST", encoded, verify=self._verify)
response = http.request("POST", encoded_url, verify=self._verify)

if response.status != 200:
logger.debug(response.data.decode("utf-8"))
error_message = response.data.decode("utf-8")
logger.debug(f"Error fetching data for {ticker}: {error_message}")
stocks_error.append(ticker)
continue
quotes = json.loads(response.data.decode("utf-8"))["quotes"]
price_evolution = []
for q in quotes:
price_evolution.append(q["ask_price"])

quotes = json.loads(response.data.decode("utf-8")).get("quotes", [])
price_evolution = [q["ask_price"] for q in quotes]
self._data.append(price_evolution)
finally:
http.clear()
Expand Down
37 changes: 19 additions & 18 deletions qiskit_finance/data_providers/exchange_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

logger = logging.getLogger(__name__)

VALID_STOCKMARKETS = [
StockMarket.LONDON,
StockMarket.EURONEXT,
StockMarket.SINGAPORE,
]


class ExchangeDataProvider(BaseDataProvider):
"""Exchange data provider.
Expand All @@ -44,9 +50,9 @@ def __init__(
token (str): Nasdaq Data Link access token.
tickers (str | list[str] | None): Tickers for the data provider.
- If a string is provided, it can be a single ticker symbol or multiple symbols
separated by semicolons or newlines.
separated by semicolons or new-lines.
- If a list of strings is provided, each string should be a single ticker symbol.
Default is None.
Default is :code:`None`.
stockmarket (StockMarket): LONDON (default), EURONEXT, or SINGAPORE
start (datetime.datetime): Start date of the data.
Defaults to January 1st, 2016.
Expand All @@ -57,22 +63,17 @@ def __init__(
QiskitFinanceError: provider doesn't support given stock market
"""
super().__init__()
self._tickers = None
tickers = tickers if tickers is not None else []
if isinstance(tickers, list):
self._tickers = tickers
else:
self._tickers = tickers.replace("\n", ";").split(";")
self._n = len(self._tickers)

if stockmarket not in [
StockMarket.LONDON,
StockMarket.EURONEXT,
StockMarket.SINGAPORE,
]:
msg = "ExchangeDataProvider does not support "
msg += stockmarket.value
msg += " as a stock market."

if tickers is None:
tickers = []
if isinstance(tickers, str):
tickers = tickers.replace("\n", ";").split(";")

self._tickers = tickers
self._n = len(tickers)

if stockmarket not in VALID_STOCKMARKETS:
msg = f"ExchangeDataProvider does not support {stockmarket.value} as a stock market."
raise QiskitFinanceError(msg)

# This is to aid serialization; string is ok to serialize
Expand Down
15 changes: 7 additions & 8 deletions qiskit_finance/data_providers/random_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,22 @@ def __init__(
- If a string is provided, it can be a single ticker symbol or multiple symbols
separated by semicolons or newlines.
- If a list of strings is provided, each string should be a single ticker symbol.
Default is None, using ["TICKER1", "TICKER2"] if not provided.
Default is :code:`None`, using :code:`["TICKER1", "TICKER2"]` if not provided.
start (datetime.datetime): Start date of the data.
Defaults to January 1st, 2016.
end (datetime.datetime): End date of the data.
Defaults to January 30th, 2016.
seed (int | None): Random seed for reproducibility.
"""
super().__init__()
self._tickers = None
tickers = tickers if tickers is not None else ["TICKER1", "TICKER2"]

if isinstance(tickers, list):
self._tickers = tickers
else:
self._tickers = tickers.replace("\n", ";").split(";")
if tickers is None:
tickers = ["TICKER1", "TICKER2"]
if isinstance(tickers, str):
tickers = tickers.replace("\n", ";").split(";")

self._n = len(self._tickers)
self._tickers = tickers
self._n = len(tickers)
self._start = start
self._end = end
self._seed = seed
Expand Down
15 changes: 7 additions & 8 deletions qiskit_finance/data_providers/wikipedia_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ def __init__(
Default is January 30th, 2016.
"""
super().__init__()
self._tickers = None
tickers = tickers if tickers is not None else []
if isinstance(tickers, list):
self._tickers = tickers
else:
self._tickers = tickers.replace("\n", ";").split(";")
self._n = len(self._tickers)

self._token = token
if tickers is None:
tickers = []
if isinstance(tickers, str):
tickers = tickers.replace("\n", ";").split(";")

self._tickers = tickers
self._n = len(tickers)
self._token = token
self._start = start.strftime("%Y-%m-%d")
self._end = end.strftime("%Y-%m-%d")
self._data = []
Expand Down
16 changes: 7 additions & 9 deletions qiskit_finance/data_providers/yahoo_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,21 @@ def __init__(
- If a string is provided, it can be a single ticker symbol or multiple symbols
separated by semicolons or newlines.
- If a list of strings is provided, each string should be a single ticker symbol.
Default is None, meaning no tickers provided.
Default is :code:`None`, meaning no tickers provided.
start (datetime.datetime): Start date of the data.
Default is January 1st, 2016.
end (datetime.datetime): End date of the data.
Default is January 30th, 2016.
"""
super().__init__()

self._tickers = None
tickers = tickers if tickers is not None else []
if isinstance(tickers, list):
self._tickers = tickers
else:
self._tickers = tickers.replace("\n", ";").split(";")

self._n = len(self._tickers)
if tickers is None:
tickers = []
if isinstance(tickers, str):
tickers = tickers.replace("\n", ";").split(";")

self._tickers = tickers
self._n = len(tickers)
self._start = start.strftime("%Y-%m-%d")
self._end = end.strftime("%Y-%m-%d")
self._data = []
Expand Down

0 comments on commit ef53f9e

Please sign in to comment.