From e793e18dc14df857e55a131728093749f25c2d07 Mon Sep 17 00:00:00 2001 From: Tommaso Baiocchi Date: Mon, 29 Sep 2025 22:35:05 +0200 Subject: [PATCH 1/4] test: Add comprehensive doctests for stock_price function --- web_programming/current_stock_price.py | 27 +++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 531da949ea50..7c760dcc803e 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -22,10 +22,27 @@ def stock_price(symbol: str = "AAPL") -> str: """ - >>> stock_price("EEEE") - 'No tag with the specified data-testid attribute found.' - >>> isinstance(float(stock_price("GOOG")),float) - True + Get current stock price from Yahoo Finance. + + Args: + symbol: Stock ticker symbol (e.g., 'AAPL', 'GOOG') + + Returns: + Current stock price as string, or error message if not found + + Examples: + >>> stock_price("EEEE") # Invalid symbol + 'No tag with the specified data-testid attribute found.' + >>> isinstance(float(stock_price("GOOG")), float) # Valid symbol + True + >>> stock_price("") # Empty symbol + 'No tag with the specified data-testid attribute found.' + >>> stock_price("INVALID_SYMBOL_123") # Another invalid symbol + 'No tag with the specified data-testid attribute found.' + >>> # Test output format for valid symbols + >>> result = stock_price("AAPL") + >>> result.replace(".", "").replace(",", "").isdigit() or "No " in result + True """ url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" yahoo_finance_source = httpx.get( @@ -45,4 +62,4 @@ def stock_price(symbol: str = "AAPL") -> str: testmod() for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): - print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") + print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") \ No newline at end of file From cf50aec438e98288a51b51d4828879ff9c5d94a1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Sep 2025 20:44:13 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/current_stock_price.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 7c760dcc803e..249c0364cc29 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -23,13 +23,13 @@ def stock_price(symbol: str = "AAPL") -> str: """ Get current stock price from Yahoo Finance. - + Args: symbol: Stock ticker symbol (e.g., 'AAPL', 'GOOG') - + Returns: Current stock price as string, or error message if not found - + Examples: >>> stock_price("EEEE") # Invalid symbol 'No tag with the specified data-testid attribute found.' @@ -62,4 +62,4 @@ def stock_price(symbol: str = "AAPL") -> str: testmod() for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): - print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") \ No newline at end of file + print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") From 6581c4ed1f5af3a42f6fa444ef580517c157aefa Mon Sep 17 00:00:00 2001 From: Tommaso Baiocchi Date: Tue, 30 Sep 2025 22:33:37 +0200 Subject: [PATCH 3/4] test: Add comprehensive doctests for ohms_law function --- electronics/ohms_law.py | 61 +++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 66e737c1f909..7a4c76cff86a 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -4,23 +4,48 @@ def ohms_law(voltage: float, current: float, resistance: float) -> dict[str, float]: """ - Apply Ohm's Law, on any two given electrical values, which can be voltage, current, - and resistance, and then in a Python dict return name/value pair of the zero value. - - >>> ohms_law(voltage=10, resistance=5, current=0) - {'current': 2.0} - >>> ohms_law(voltage=0, current=0, resistance=10) - Traceback (most recent call last): - ... - ValueError: One and only one argument must be 0 - >>> ohms_law(voltage=0, current=1, resistance=-2) - Traceback (most recent call last): - ... - ValueError: Resistance cannot be negative - >>> ohms_law(resistance=0, voltage=-10, current=1) - {'resistance': -10.0} - >>> ohms_law(voltage=0, current=-1.5, resistance=2) - {'voltage': -3.0} + Apply Ohm's Law to calculate the missing electrical value. + + Ohm's Law: V = I × R, where V=voltage, I=current, R=resistance + + Args: + voltage: Electrical potential difference in volts (0 if unknown) + current: Electrical current in amperes (0 if unknown) + resistance: Electrical resistance in ohms (0 if unknown) + + Returns: + Dictionary with the calculated value: {'voltage': V}, {'current': I}, or {'resistance': R} + + Raises: + ValueError: If not exactly one value is 0, or if resistance is negative + + Examples: + >>> ohms_law(voltage=10, resistance=5, current=0) + {'current': 2.0} + >>> ohms_law(voltage=0, current=2, resistance=5) + {'voltage': 10.0} + >>> ohms_law(voltage=10, current=2, resistance=0) + {'resistance': 5.0} + >>> ohms_law(voltage=0, current=0, resistance=10) + Traceback (most recent call last): + ... + ValueError: One and only one argument must be 0 + >>> ohms_law(voltage=0, current=1, resistance=-2) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be negative + >>> ohms_law(resistance=0, voltage=-10, current=1) + {'resistance': -10.0} + >>> ohms_law(voltage=0, current=-1.5, resistance=2) + {'voltage': -3.0} + >>> ohms_law(voltage=12, current=3, resistance=0) + {'resistance': 4.0} + >>> ohms_law(voltage=-9, current=3, resistance=0) + {'resistance': -3.0} + >>> ohms_law(voltage=0, current=2.5, resistance=4) + {'voltage': 10.0} + >>> ohms_law(voltage=7.5, current=0, resistance=3) + {'current': 2.5} """ if (voltage, current, resistance).count(0) != 1: raise ValueError("One and only one argument must be 0") @@ -39,4 +64,4 @@ def ohms_law(voltage: float, current: float, resistance: float) -> dict[str, flo if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file From b39c7b8dbf95290766e93b6901af5fe72cf90e0e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 18:18:56 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- electronics/ohms_law.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 7a4c76cff86a..490f774ae3da 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -5,20 +5,20 @@ def ohms_law(voltage: float, current: float, resistance: float) -> dict[str, float]: """ Apply Ohm's Law to calculate the missing electrical value. - + Ohm's Law: V = I × R, where V=voltage, I=current, R=resistance - + Args: voltage: Electrical potential difference in volts (0 if unknown) - current: Electrical current in amperes (0 if unknown) + current: Electrical current in amperes (0 if unknown) resistance: Electrical resistance in ohms (0 if unknown) - + Returns: Dictionary with the calculated value: {'voltage': V}, {'current': I}, or {'resistance': R} - + Raises: ValueError: If not exactly one value is 0, or if resistance is negative - + Examples: >>> ohms_law(voltage=10, resistance=5, current=0) {'current': 2.0} @@ -64,4 +64,4 @@ def ohms_law(voltage: float, current: float, resistance: float) -> dict[str, flo if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod()