22
22
23
23
def stock_price (symbol : str = "AAPL" ) -> str :
24
24
"""
25
- >>> stock_price("EEEE")
26
- 'No <fin-streamer> tag with the specified data-testid attribute found.'
27
- >>> isinstance(float(stock_price("GOOG")),float)
28
- True
25
+ Get current stock price from Yahoo Finance.
26
+
27
+ Args:
28
+ symbol: Stock ticker symbol (e.g., 'AAPL', 'GOOG')
29
+
30
+ Returns:
31
+ Current stock price as string, or error message if not found
32
+
33
+ Examples:
34
+ >>> stock_price("EEEE") # Invalid symbol
35
+ 'No <fin-streamer> tag with the specified data-testid attribute found.'
36
+ >>> isinstance(float(stock_price("GOOG")), float) # Valid symbol
37
+ True
38
+ >>> stock_price("") # Empty symbol
39
+ 'No <fin-streamer> tag with the specified data-testid attribute found.'
40
+ >>> stock_price("INVALID_SYMBOL_123") # Another invalid symbol
41
+ 'No <fin-streamer> tag with the specified data-testid attribute found.'
42
+ >>> # Test output format for valid symbols
43
+ >>> result = stock_price("AAPL")
44
+ >>> result.replace(".", "").replace(",", "").isdigit() or "No <fin-streamer>" in result
45
+ True
29
46
"""
30
47
url = f"https://finance.yahoo.com/quote/{ symbol } ?p={ symbol } "
31
48
yahoo_finance_source = httpx .get (
@@ -45,4 +62,4 @@ def stock_price(symbol: str = "AAPL") -> str:
45
62
testmod ()
46
63
47
64
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL" .split ():
48
- print (f"Current { symbol :<4} stock price is { stock_price (symbol ):>8} " )
65
+ print (f"Current { symbol :<4} stock price is { stock_price (symbol ):>8} " )
0 commit comments