Skip to content

Commit 3e2fd93

Browse files
authored
Merge branch 'master' into feature/apriori-association-rules
2 parents 469ce25 + c0ad5bb commit 3e2fd93

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
- id: auto-walrus
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.13.0
19+
rev: v0.13.1
2020
hooks:
2121
- id: ruff-check
2222
- id: ruff-format
@@ -47,7 +47,7 @@ repos:
4747
- id: validate-pyproject
4848

4949
- repo: https://github.com/pre-commit/mirrors-mypy
50-
rev: v1.18.1
50+
rev: v1.18.2
5151
hooks:
5252
- id: mypy
5353
args:

data_structures/arrays/sudoku_solver.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
def cross(items_a, items_b):
1212
"""
1313
Cross product of elements in A and elements in B.
14+
15+
>>> cross('AB', '12')
16+
['A1', 'A2', 'B1', 'B2']
17+
>>> cross('ABC', '123')
18+
['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']
19+
>>> cross('ABC', '1234')
20+
['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'B4', 'C1', 'C2', 'C3', 'C4']
21+
>>> cross('', '12')
22+
[]
23+
>>> cross('A', '')
24+
[]
25+
>>> cross('', '')
26+
[]
1427
"""
1528
return [a + b for a in items_a for b in items_b]
1629

strings/edit_distance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ def edit_distance(source: str, target: str) -> int:
1414
1515
>>> edit_distance("GATTIC", "GALTIC")
1616
1
17+
>>> edit_distance("NUM3", "HUM2")
18+
2
19+
>>> edit_distance("cap", "CAP")
20+
3
21+
>>> edit_distance("Cat", "")
22+
3
23+
>>> edit_distance("cat", "cat")
24+
0
25+
>>> edit_distance("", "123456789")
26+
9
27+
>>> edit_distance("Be@uty", "Beautyyyy!")
28+
5
29+
>>> edit_distance("lstring", "lsstring")
30+
1
1731
"""
1832
if len(source) == 0:
1933
return len(target)

web_programming/covid_stats_via_xpath.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
2-
This is to show simple COVID19 info fetching from worldometers archive site using lxml
3-
* The main motivation to use lxml in place of bs4 is that it is faster and therefore
4-
more convenient to use in Python web projects (e.g. Django or Flask-based)
2+
This script demonstrates fetching simple COVID-19 statistics from the
3+
Worldometers archive site using lxml. lxml is chosen over BeautifulSoup
4+
for its speed and convenience in Python web projects (such as Django or
5+
Flask).
56
"""
67

78
# /// script
@@ -25,15 +26,34 @@ class CovidData(NamedTuple):
2526

2627

2728
def covid_stats(
28-
url: str = "https://web.archive.org/web/20250825095350/https://www.worldometers.info/coronavirus/",
29+
url: str = (
30+
"https://web.archive.org/web/20250825095350/"
31+
"https://www.worldometers.info/coronavirus/"
32+
),
2933
) -> CovidData:
3034
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
31-
return CovidData(
32-
*html.fromstring(httpx.get(url, timeout=10).content).xpath(xpath_str)
35+
try:
36+
response = httpx.get(url, timeout=10).raise_for_status()
37+
except httpx.TimeoutException:
38+
print(
39+
"Request timed out. Please check your network connection "
40+
"or try again later."
41+
)
42+
return CovidData("N/A", "N/A", "N/A")
43+
except httpx.HTTPStatusError as e:
44+
print(f"HTTP error occurred: {e}")
45+
return CovidData("N/A", "N/A", "N/A")
46+
data = html.fromstring(response.content).xpath(xpath_str)
47+
if len(data) != 3:
48+
print("Unexpected data format. The page structure may have changed.")
49+
data = "N/A", "N/A", "N/A"
50+
return CovidData(*data)
51+
52+
53+
if __name__ == "__main__":
54+
fmt = (
55+
"Total COVID-19 cases in the world: {}\n"
56+
"Total deaths due to COVID-19 in the world: {}\n"
57+
"Total COVID-19 patients recovered in the world: {}"
3358
)
34-
35-
36-
fmt = """Total COVID-19 cases in the world: {}
37-
Total deaths due to COVID-19 in the world: {}
38-
Total COVID-19 patients recovered in the world: {}"""
39-
print(fmt.format(*covid_stats()))
59+
print(fmt.format(*covid_stats()))

0 commit comments

Comments
 (0)