Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions machine_learning/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# "numpy",
# ]
# ///

import httpx
import httpx2
import numpy as np


Expand All @@ -25,7 +25,7 @@ def collect_dataset():
The dataset contains ADR vs Rating of a Player
:return : dataset obtained from the link, as matrix
"""
response = httpx.get(
response = httpx2.get(
"https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/"
"master/Week1/ADRvsRating.csv",
timeout=10,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"beautifulsoup4>=4.15",
"cython>=3.1.2",
"fake-useragent>=1.5.1",
"httpx>=0.28.1",
"httpx2>=2.0.1",
"imageio>=2.36.1",
"keras>=3.7",
"lxml>=6",
Expand Down Expand Up @@ -50,7 +50,7 @@ cv = [
"opencv-python>=4.10.0.84",
]
euler-validate = [
"httpx>=0.28.1",
"httpx2>=2.0.1",
"numpy>=2.1.3",
]
# qiskit does not yet run on free-threaded CPython: its compiled core re-enables
Expand Down
6 changes: 3 additions & 3 deletions scripts/validate_solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# "pytest",
# ]
# ///
Expand All @@ -15,8 +15,8 @@
import pathlib
from types import ModuleType

import httpx
import httpx2
import pytest

Check failure on line 19 in scripts/validate_solutions.py

View workflow job for this annotation

GitHub Actions / ty

ty (unresolved-import)

scripts/validate_solutions.py:19:8: unresolved-import: Cannot resolve imported module `pytest` info: Searched in the following paths during module resolution: info: 1. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 2. /home/runner/work/_temp/setup-uv-cache/builds-v0/.tmpGxihyi/lib/python3.14/site-packages (site-packages) info: 3. /home/runner/work/_temp/setup-uv-cache/builds-v0/.tmpGxihyi/lib64/python3.14/site-packages (site-packages) info: 4. /home/runner/work/Python/Python/.venv/lib/python3.14/site-packages (site-packages) info: 5. /home/runner/work/Python/Python/.venv/lib64/python3.14/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment

PROJECT_EULER_DIR_PATH = pathlib.Path.cwd().joinpath("project_euler")
PROJECT_EULER_ANSWERS_PATH = pathlib.Path.cwd().joinpath(
Expand All @@ -30,8 +30,8 @@
def convert_path_to_module(file_path: pathlib.Path) -> ModuleType:
"""Converts a file path to a Python module"""
spec = importlib.util.spec_from_file_location(file_path.name, str(file_path))
module = importlib.util.module_from_spec(spec) # type: ignore[arg-type]

Check failure on line 33 in scripts/validate_solutions.py

View workflow job for this annotation

GitHub Actions / ty

ty (invalid-argument-type)

scripts/validate_solutions.py:33:46: invalid-argument-type: Argument to function `module_from_spec` is incorrect: Expected `ModuleSpec`, found `ModuleSpec | None` info: element `None` of union `ModuleSpec | None` is not assignable to `ModuleSpec` stdlib/_frozen_importlib.pyi:45:5: info: Function defined here stdlib/_frozen_importlib.pyi:45:22: Parameter declared here
spec.loader.exec_module(module) # type: ignore[union-attr]

Check failure on line 34 in scripts/validate_solutions.py

View workflow job for this annotation

GitHub Actions / ty

ty (unresolved-attribute)

scripts/validate_solutions.py:34:5: unresolved-attribute: Attribute `loader` is not defined on `None` in union `ModuleSpec | None`

Check failure on line 34 in scripts/validate_solutions.py

View workflow job for this annotation

GitHub Actions / ty

ty (unresolved-attribute)

scripts/validate_solutions.py:34:5: unresolved-attribute: Attribute `exec_module` is not defined on `None` in union `Loader | None`
return module


Expand Down Expand Up @@ -66,7 +66,7 @@
"Accept": "application/vnd.github.v3+json",
"Authorization": "token " + os.environ["GITHUB_TOKEN"],
}
files = httpx.get(get_files_url(), headers=headers, timeout=10).json()
files = httpx2.get(get_files_url(), headers=headers, timeout=10).json()
for file in files:
filepath = pathlib.Path.cwd().joinpath(file["filename"])
if (
Expand Down
8 changes: 4 additions & 4 deletions web_programming/co2_emission.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# ]
# ///

from datetime import date

import httpx
import httpx2

BASE_URL = "https://api.carbonintensity.org.uk/intensity"


# Emission in the last half hour
def fetch_last_half_hour() -> str:
last_half_hour = httpx.get(BASE_URL, timeout=10).json()["data"][0]
last_half_hour = httpx2.get(BASE_URL, timeout=10).json()["data"][0]
return last_half_hour["intensity"]["actual"]


# Emissions in a specific date range
def fetch_from_to(start, end) -> list:
return httpx.get(f"{BASE_URL}/{start}/{end}", timeout=10).json()["data"]
return httpx2.get(f"{BASE_URL}/{start}/{end}", timeout=10).json()["data"]


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions web_programming/covid_stats_via_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# "lxml",
# ]
# ///

from typing import NamedTuple

import httpx
import httpx2
from lxml import html


Expand All @@ -33,14 +33,14 @@ def covid_stats(
) -> CovidData:
xpath_str = '//div[@class = "maincounter-number"]/span/text()'
try:
response = httpx.get(url, timeout=10).raise_for_status()
except httpx.TimeoutException:
response = httpx2.get(url, timeout=10).raise_for_status()
except httpx2.TimeoutException:
print(
"Request timed out. Please check your network connection "
"or try again later."
)
return CovidData("N/A", "N/A", "N/A")
except httpx.HTTPStatusError as e:
except httpx2.HTTPStatusError as e:
print(f"HTTP error occurred: {e}")
return CovidData("N/A", "N/A", "N/A")
data = html.fromstring(response.content).xpath(xpath_str)
Expand Down
6 changes: 3 additions & 3 deletions web_programming/crawl_google_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# dependencies = [
# "beautifulsoup4",
# "fake-useragent",
# "httpx",
# "httpx2",
# ]
# ///

import sys
import webbrowser

import httpx
import httpx2
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

if __name__ == "__main__":
print("Googling.....")
url = "https://www.google.com/search?q=" + " ".join(sys.argv[1:])
res = httpx.get(
res = httpx2.get(
url,
headers={"UserAgent": UserAgent().random},
timeout=10,
Expand All @@ -25,7 +25,7 @@
)
# res.raise_for_status()
with open("project1a.html", "wb") as out_file: # only for knowing the class
for data in res.iter_content(10000):

Check failure on line 28 in web_programming/crawl_google_results.py

View workflow job for this annotation

GitHub Actions / ty

ty (unresolved-attribute)

web_programming/crawl_google_results.py:28:21: unresolved-attribute: Object of type `Response` has no attribute `iter_content`
out_file.write(data)
soup = BeautifulSoup(res.text, "html.parser")
links = list(soup.select(".eZt8xd"))[:5]
Expand All @@ -33,6 +33,6 @@
print(len(links))
for link in links:
if link.text == "Maps":
webbrowser.open(link.get("href"))

Check failure on line 36 in web_programming/crawl_google_results.py

View workflow job for this annotation

GitHub Actions / ty

ty (invalid-argument-type)

web_programming/crawl_google_results.py:36:29: invalid-argument-type: Argument to function `open` is incorrect: Expected `str`, found `str | AttributeValueList | None` info: element `AttributeValueList` of union `str | AttributeValueList | None` is not assignable to `str` stdlib/webbrowser.pyi:21:5: info: Function defined here stdlib/webbrowser.pyi:21:10: Parameter declared here
else:
webbrowser.open(f"https://google.com{link.get('href')}")
6 changes: 3 additions & 3 deletions web_programming/crawl_google_scholar_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "httpx",
# "httpx2",
# ]
# ///

import httpx
import httpx2
from bs4 import BeautifulSoup


Expand All @@ -20,10 +20,10 @@
Return the citation number.
"""
soup = BeautifulSoup(
httpx.get(base_url, params=params, timeout=10).content, "html.parser"
httpx2.get(base_url, params=params, timeout=10).content, "html.parser"
)
div = soup.find("div", attrs={"class": "gs_ri"})
anchors = div.find("div", attrs={"class": "gs_fl"}).find_all("a")

Check failure on line 26 in web_programming/crawl_google_scholar_citation.py

View workflow job for this annotation

GitHub Actions / ty

ty (unresolved-attribute)

web_programming/crawl_google_scholar_citation.py:26:15: unresolved-attribute: Attribute `find` is not defined on `None` in union `Tag | None`

Check failure on line 26 in web_programming/crawl_google_scholar_citation.py

View workflow job for this annotation

GitHub Actions / ty

ty (unresolved-attribute)

web_programming/crawl_google_scholar_citation.py:26:15: unresolved-attribute: Attribute `find_all` is not defined on `None` in union `Tag | None`
return anchors[2].get_text()


Expand Down
6 changes: 3 additions & 3 deletions web_programming/currency_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# ]
# ///

import os

import httpx
import httpx2

URL_BASE = "https://www.amdoren.com/api/currency.php"

Expand Down Expand Up @@ -183,7 +183,7 @@ def convert_currency(
params = locals()
# from is a reserved keyword
params["from"] = params.pop("from_")
res = httpx.get(URL_BASE, params=params, timeout=10).json()
res = httpx2.get(URL_BASE, params=params, timeout=10).json()
return str(res["amount"]) if res["error"] == 0 else res["error_message"]


Expand Down
6 changes: 3 additions & 3 deletions web_programming/current_stock_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "httpx",
# "httpx2",
# ]
# ///

import httpx
import httpx2
from bs4 import BeautifulSoup

"""
Expand All @@ -28,7 +28,7 @@ def stock_price(symbol: str = "AAPL") -> str:
True
"""
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
yahoo_finance_source = httpx.get(
yahoo_finance_source = httpx2.get(
url, headers={"USER-AGENT": "Mozilla/5.0"}, timeout=10, follow_redirects=True
).text
soup = BeautifulSoup(yahoo_finance_source, "html.parser")
Expand Down
8 changes: 4 additions & 4 deletions web_programming/current_weather.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# ]
# ///

import httpx
import httpx2

# Put your API key(s) here
OPENWEATHERMAP_API_KEY = ""
Expand All @@ -26,13 +26,13 @@ def current_weather(location: str) -> list[dict]:
weather_data = []
if OPENWEATHERMAP_API_KEY:
params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY}
response_openweathermap = httpx.get(
response_openweathermap = httpx2.get(
OPENWEATHERMAP_URL_BASE, params=params_openweathermap, timeout=10
)
weather_data.append({"OpenWeatherMap": response_openweathermap.json()})
if WEATHERSTACK_API_KEY:
params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY}
response_weatherstack = httpx.get(
response_weatherstack = httpx2.get(
WEATHERSTACK_URL_BASE, params=params_weatherstack, timeout=10
)
weather_data.append({"Weatherstack": response_weatherstack.json()})
Expand Down
6 changes: 3 additions & 3 deletions web_programming/daily_horoscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "httpx",
# "httpx2",
# ]
# ///

import httpx
import httpx2
from bs4 import BeautifulSoup


Expand All @@ -15,7 +15,7 @@ def horoscope(zodiac_sign: int, day: str) -> str:
"https://www.horoscope.com/us/horoscopes/general/"
f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}"
)
soup = BeautifulSoup(httpx.get(url, timeout=10).content, "html.parser")
soup = BeautifulSoup(httpx2.get(url, timeout=10).content, "html.parser")
return soup.find("div", class_="main-horoscope").p.text


Expand Down
6 changes: 3 additions & 3 deletions web_programming/download_images_from_google_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "httpx",
# "httpx2",
# ]
# ///

Expand All @@ -12,7 +12,7 @@
import sys
import urllib.request

import httpx
import httpx2
from bs4 import BeautifulSoup

headers = {
Expand Down Expand Up @@ -47,7 +47,7 @@ def download_images_from_google_query(query: str = "dhaka", max_images: int = 5)
"ijn": "0",
}

html = httpx.get(
html = httpx2.get(
"https://www.google.com/search", params=params, headers=headers, timeout=10
)
soup = BeautifulSoup(html.text, "html.parser")
Expand Down
8 changes: 4 additions & 4 deletions web_programming/emails_from_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# "httpx2",
# ]
# ///

Expand All @@ -20,7 +20,7 @@
from html.parser import HTMLParser
from urllib import parse

import httpx
import httpx2


class Parser(HTMLParser):
Expand Down Expand Up @@ -79,7 +79,7 @@ def emails_from_url(url: str = "https://github.com") -> list[str]:

try:
# Open URL
r = httpx.get(url, timeout=10, follow_redirects=True)
r = httpx2.get(url, timeout=10, follow_redirects=True)

# pass the raw HTML to the parser to get links
parser.feed(r.text)
Expand All @@ -96,7 +96,7 @@ def emails_from_url(url: str = "https://github.com") -> list[str]:
else:
link = parse.urljoin(f"https://{domain}", link)
try:
read = httpx.get(link, timeout=10, follow_redirects=True)
read = httpx2.get(link, timeout=10, follow_redirects=True)
# Get the valid email.
emails = re.findall("[a-zA-Z0-9]+@" + domain, read.text)
# If not in list then append it.
Expand Down
10 changes: 5 additions & 5 deletions web_programming/fetch_anime_and_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# dependencies = [
# "beautifulsoup4",
# "fake-useragent",
# "httpx",
# "httpx2",
# ]
# ///

import httpx
import httpx2
from bs4 import BeautifulSoup, NavigableString, Tag
from fake_useragent import UserAgent

Expand Down Expand Up @@ -36,7 +36,7 @@ def search_scraper(anime_name: str) -> list:
# concat the name to form the search url.
search_url = f"{BASE_URL}/search?keyword={anime_name}"

response = httpx.get(
response = httpx2.get(
search_url, headers={"UserAgent": UserAgent().chrome}, timeout=10
) # request the url.

Expand Down Expand Up @@ -91,7 +91,7 @@ def search_anime_episode_list(episode_endpoint: str) -> list:

request_url = f"{BASE_URL}{episode_endpoint}"

response = httpx.get(
response = httpx2.get(
url=request_url, headers={"UserAgent": UserAgent().chrome}, timeout=10
)
response.raise_for_status()
Expand Down Expand Up @@ -142,7 +142,7 @@ def get_anime_episode(episode_endpoint: str) -> list:

episode_page_url = f"{BASE_URL}{episode_endpoint}"

response = httpx.get(
response = httpx2.get(
url=episode_page_url, headers={"User-Agent": UserAgent().chrome}, timeout=10
)
response.raise_for_status()
Expand Down
Loading
Loading