From 5f4df3b8cfd94c7b9c25959a8973b3ace9d97ce6 Mon Sep 17 00:00:00 2001 From: sanskarmodi8 Date: Wed, 1 Oct 2025 07:09:42 +0530 Subject: [PATCH 1/5] add basic crypto price converter in accordance with #13008 --- web_programming/crypto_price.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 web_programming/crypto_price.py diff --git a/web_programming/crypto_price.py b/web_programming/crypto_price.py new file mode 100644 index 000000000000..1116f878a8dd --- /dev/null +++ b/web_programming/crypto_price.py @@ -0,0 +1,30 @@ +""" +Convert ETH to USD using real-time price data from CoinGecko. +""" + +# /// script +# requires-python = ">=3.10" +# dependencies = [ +# "httpx", +# ] +# /// + +import httpx + +COINGECKO_URL = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" + +def get_eth_price_usd() -> float: + """Fetch the current ETH price in USD.""" + response = httpx.get(COINGECKO_URL, timeout=10) + response.raise_for_status() + data = response.json() + return data["ethereum"]["usd"] + +def eth_to_usd(eth_amount: float) -> float: + """Convert ETH amount to USD.""" + return eth_amount * get_eth_price_usd() + +if __name__ == "__main__": + eth_amount = float(input("Enter ETH amount: ")) + usd_value = eth_to_usd(eth_amount) + print(f"{eth_amount} ETH = ${usd_value:.2f} USD") From ad13a490ced008664070b6e211015e61dcc49040 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Oct 2025 01:41:50 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/crypto_price.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/web_programming/crypto_price.py b/web_programming/crypto_price.py index 1116f878a8dd..9eddcfdc1ba0 100644 --- a/web_programming/crypto_price.py +++ b/web_programming/crypto_price.py @@ -11,7 +11,10 @@ import httpx -COINGECKO_URL = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" +COINGECKO_URL = ( + "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" +) + def get_eth_price_usd() -> float: """Fetch the current ETH price in USD.""" @@ -20,10 +23,12 @@ def get_eth_price_usd() -> float: data = response.json() return data["ethereum"]["usd"] + def eth_to_usd(eth_amount: float) -> float: """Convert ETH amount to USD.""" return eth_amount * get_eth_price_usd() + if __name__ == "__main__": eth_amount = float(input("Enter ETH amount: ")) usd_value = eth_to_usd(eth_amount) From a3aff6646421bfcea140d15ca5240426ccd1e4f1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 16 Sep 2026 10:02:03 +0200 Subject: [PATCH 3/5] Switch from httpx to httpx2 for API requests Updated the import statement and modified the get_eth_price_usd function to use httpx2 instead of httpx. --- web_programming/crypto_price.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/web_programming/crypto_price.py b/web_programming/crypto_price.py index 9eddcfdc1ba0..b10409079dc9 100644 --- a/web_programming/crypto_price.py +++ b/web_programming/crypto_price.py @@ -5,11 +5,11 @@ # /// script # requires-python = ">=3.10" # dependencies = [ -# "httpx", +# "httpx2", # ] # /// -import httpx +import httpx2 COINGECKO_URL = ( "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" @@ -18,10 +18,7 @@ def get_eth_price_usd() -> float: """Fetch the current ETH price in USD.""" - response = httpx.get(COINGECKO_URL, timeout=10) - response.raise_for_status() - data = response.json() - return data["ethereum"]["usd"] + return httpx2.get(COINGECKO_URL, timeout=10).raise_for_status().json()["ethereum"]["usd"] def eth_to_usd(eth_amount: float) -> float: From ef2a48cfcc16c8b2776b9a35aebc6beb0d755a03 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Sep 2026 08:02:15 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/crypto_price.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/web_programming/crypto_price.py b/web_programming/crypto_price.py index b10409079dc9..8e6b7ea89e80 100644 --- a/web_programming/crypto_price.py +++ b/web_programming/crypto_price.py @@ -18,7 +18,11 @@ def get_eth_price_usd() -> float: """Fetch the current ETH price in USD.""" - return httpx2.get(COINGECKO_URL, timeout=10).raise_for_status().json()["ethereum"]["usd"] + return ( + httpx2.get(COINGECKO_URL, timeout=10) + .raise_for_status() + .json()["ethereum"]["usd"] + ) def eth_to_usd(eth_amount: float) -> float: From ad58cdc52793d7178db1805b6898f5fcdbac0d71 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 16 Sep 2026 10:04:13 +0200 Subject: [PATCH 5/5] Refactor ETH price fetching to use direct import --- web_programming/crypto_price.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/web_programming/crypto_price.py b/web_programming/crypto_price.py index 8e6b7ea89e80..a3188932256c 100644 --- a/web_programming/crypto_price.py +++ b/web_programming/crypto_price.py @@ -9,7 +9,7 @@ # ] # /// -import httpx2 +from httpx2 import get COINGECKO_URL = ( "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" @@ -18,11 +18,7 @@ def get_eth_price_usd() -> float: """Fetch the current ETH price in USD.""" - return ( - httpx2.get(COINGECKO_URL, timeout=10) - .raise_for_status() - .json()["ethereum"]["usd"] - ) + return get(COINGECKO_URL, timeout=10).raise_for_status().json()["ethereum"]["usd"] def eth_to_usd(eth_amount: float) -> float: