Skip to content

Commit ba2d8ef

Browse files
VAIBHAVVARORApre-commit-ci[bot]cclauss
authored
Refactor error handling and improve logging (#13000)
* Refactor error handling and improve logging * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * httpx --> httpx2 * updating DIRECTORY.md * Replace httpx with httpx2 for HTTP requests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent feceadf commit ba2d8ef

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@
481481
* [Longest Increasing Subsequence Iterative](dynamic_programming/longest_increasing_subsequence_iterative.py)
482482
* [Longest Increasing Subsequence O Nlogn](dynamic_programming/longest_increasing_subsequence_o_nlogn.py)
483483
* [Longest Palindromic Subsequence](dynamic_programming/longest_palindromic_subsequence.py)
484+
* [Longest Repeating Subsequence](dynamic_programming/longest_repeating_subsequence.py)
484485
* [Matrix Chain Multiplication](dynamic_programming/matrix_chain_multiplication.py)
485486
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
486487
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)

web_programming/covid_stats_via_xpath.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Worldometers archive site using lxml. lxml is chosen over BeautifulSoup
44
for its speed and convenience in Python web projects (such as Django or
55
Flask).
6+
7+
uv run --script web_programming/covid_stats_via_xpath.py
68
"""
79

810
# /// script
@@ -12,7 +14,10 @@
1214
# "lxml",
1315
# ]
1416
# ///
17+
from __future__ import annotations
1518

19+
import argparse
20+
import logging
1621
from typing import NamedTuple
1722

1823
import httpx2
@@ -35,25 +40,47 @@ def covid_stats(
3540
try:
3641
response = httpx2.get(url, timeout=10).raise_for_status()
3742
except httpx2.TimeoutException:
38-
print(
43+
logging.error(
3944
"Request timed out. Please check your network connection "
4045
"or try again later."
4146
)
4247
return CovidData("N/A", "N/A", "N/A")
4348
except httpx2.HTTPStatusError as e:
44-
print(f"HTTP error occurred: {e}")
49+
logging.error(f"HTTP error occurred: {e}")
4550
return CovidData("N/A", "N/A", "N/A")
46-
data = html.fromstring(response.content).xpath(xpath_str)
51+
data: list[str] = html.fromstring(response.content).xpath(xpath_str)
4752
if len(data) != 3:
48-
print("Unexpected data format. The page structure may have changed.")
49-
data = "N/A", "N/A", "N/A"
53+
logging.warning("Unexpected data format. The page structure may have changed.")
54+
return CovidData("N/A", "N/A", "N/A")
55+
5056
return CovidData(*data)
5157

5258

53-
if __name__ == "__main__":
59+
def main() -> None:
60+
"""CLI entry point."""
61+
parser = argparse.ArgumentParser(
62+
description="Fetch COVID-19 statistics from Worldometers (archived)."
63+
)
64+
parser.add_argument(
65+
"--url",
66+
type=str,
67+
default=(
68+
"https://web.archive.org/web/20250825095350/"
69+
"https://www.worldometers.info/coronavirus/"
70+
),
71+
help="Custom archive URL (default: latest snapshot).",
72+
)
73+
# args = parser.parse_args()
74+
args, _ = parser.parse_known_args()
75+
76+
stats = covid_stats(args.url)
5477
fmt = (
5578
"Total COVID-19 cases in the world: {}\n"
5679
"Total deaths due to COVID-19 in the world: {}\n"
5780
"Total COVID-19 patients recovered in the world: {}"
5881
)
59-
print(fmt.format(*covid_stats()))
82+
print(fmt.format(*stats))
83+
84+
85+
if __name__ == "__main__":
86+
main()

0 commit comments

Comments
 (0)