fix: enable all pylint checks by wrapping scripts in main()

- Remove too-few-public-methods and invalid-name from disabled checks
- Wrap module-level code in main() functions:
  - generate_jpeg.py
  - random_digits.py
  - generate_cats.py
  - scrape_comics.py
- Rename download_image -> _download_image (private function)

Pylint score: 10.00/10 with all checks enabled
This commit is contained in:
Krzysztof kuhy Rudnicki 2025-12-01 16:15:03 +01:00
parent 0e73b27d50
commit 81d6dd5315
5 changed files with 90 additions and 70 deletions

View File

@ -149,7 +149,7 @@ ignore-patterns = [".*\\.pyi$"]
[tool.pylint.messages_control]
# Enable all checks by disabling disable
enable = "all"
# Minimal disabled checks - only truly problematic ones
# Minimal disabled checks - pylint internal/meta messages only
disable = [
"raw-checker-failed",
"bad-inline-option",
@ -159,8 +159,6 @@ disable = [
"useless-suppression",
"deprecated-pragma",
"use-symbolic-message-instead",
"too-few-public-methods", # Often false positive for data classes
"invalid-name", # Existing camelCase folder names (downloadCats, etc.)
]
[tool.pylint.format]

View File

@ -40,16 +40,22 @@ def _download_single_image(image_url: str) -> None:
_logger.exception("Failed to download %s", image_url)
requests_send = 0
while requests_send < MAX_REQUESTS:
res = requests.get(
"https://api.thecatapi.com/v1/images/search?limit=100&api_key=",
timeout=REQUEST_TIMEOUT,
)
requests_send += 1
response = json.loads(res.text)
urls = [cat.get("url") for cat in response]
def main() -> None:
"""Download cat images from TheCatAPI."""
requests_sent = 0
while requests_sent < MAX_REQUESTS:
res = requests.get(
"https://api.thecatapi.com/v1/images/search?limit=100&api_key=",
timeout=REQUEST_TIMEOUT,
)
requests_sent += 1
response = json.loads(res.text)
urls = [cat.get("url") for cat in response]
Path("./CATS2").mkdir(parents=True, exist_ok=True)
for url in urls:
_download_single_image(url)
Path("./CATS2").mkdir(parents=True, exist_ok=True)
for url in urls:
_download_single_image(url)
if __name__ == "__main__":
main()

View File

@ -86,7 +86,8 @@ def _save_image(
return str(unique_output_path)
if __name__ == "__main__":
def main() -> None:
"""Generate bloated JPEG images from command line arguments."""
parser = argparse.ArgumentParser(
description="Generate bloated JPEG images with random colors."
)
@ -165,5 +166,9 @@ if __name__ == "__main__":
quality=args.quality,
)
for i in range(1, args.num_images + 1):
output_path = generate_bloated_jpeg(config, i, folder)
_logger.info("Image %s saved to %s", i, Path(output_path).resolve())
result_path = generate_bloated_jpeg(config, i, folder)
_logger.info("Image %s saved to %s", i, Path(result_path).resolve())
if __name__ == "__main__":
main()

View File

@ -70,7 +70,9 @@ def _parse_single_number(num_str: str) -> tuple[float, int] | None:
MIN_ARGS = 2
if __name__ == "__main__":
def main() -> None:
"""Run the number randomizer from command line arguments."""
if len(sys.argv) < MIN_ARGS:
_logger.info(
"Usage: python random_digits.py <number1> <number2> ... "
@ -78,27 +80,27 @@ if __name__ == "__main__":
)
sys.exit(1)
input_string = " ".join(sys.argv[1:])
numbers, decimal_counts = parse_input(input_string)
args_string = " ".join(sys.argv[1:])
numbers, decimal_counts = parse_input(args_string)
if not numbers:
_logger.error("No valid numbers provided.")
sys.exit(1)
min_percentage = DEFAULT_MIN_PERCENTAGE
max_percentage = DEFAULT_MAX_PERCENTAGE
min_pct = DEFAULT_MIN_PERCENTAGE
max_pct = DEFAULT_MAX_PERCENTAGE
try:
if len(sys.argv) > len(numbers) + 1:
with contextlib.suppress(ValueError):
min_percentage = float(sys.argv[len(numbers) + 1])
min_pct = float(sys.argv[len(numbers) + 1])
if len(sys.argv) > len(numbers) + 2:
with contextlib.suppress(ValueError):
max_percentage = float(sys.argv[len(numbers) + 2])
max_pct = float(sys.argv[len(numbers) + 2])
randomized_numbers = randomize_numbers(numbers, min_percentage, max_percentage)
randomized = randomize_numbers(numbers, min_pct, max_pct)
formatted_numbers = []
for i, num in enumerate(randomized_numbers):
for i, num in enumerate(randomized):
format_str = f".{decimal_counts[i]}f"
formatted_numbers.append(float(format(num, format_str)))
@ -108,3 +110,7 @@ if __name__ == "__main__":
_logger.exception("Error processing numbers")
_logger.exception("Please provide valid numbers and percentages.")
sys.exit(1)
if __name__ == "__main__":
main()

View File

@ -18,24 +18,8 @@ _logger = logging.getLogger(__name__)
REQUEST_TIMEOUT = 30 # seconds
# Initialize argument parser to accept the website URL as an argument
parser = argparse.ArgumentParser(description="Download images from a comic website.")
parser.add_argument(
"url", type=str, help="The URL of the website to start downloading images from"
)
args = parser.parse_args()
# Initialize WebDriver (Use the appropriate driver for your browser)
driver = webdriver.Chrome()
# Open the website from the passed argument
url = args.url
_logger.info("Opening the website: %s", url)
driver.get(url)
# A function to download images by URL
def download_image(image_url: str) -> bool:
def _download_image(image_url: str) -> bool:
"""Download an image from a URL and save it locally."""
# Extract image name from URL
image_name = Path(urlparse(image_url).path).name
@ -53,37 +37,58 @@ def download_image(image_url: str) -> bool:
return True
# No need to define a specific number of images now
count = 1
def main() -> None:
"""Download comic images from a website using Selenium."""
# Initialize argument parser to accept the website URL as an argument
parser = argparse.ArgumentParser(
description="Download images from a comic website."
)
parser.add_argument(
"url", type=str, help="The URL of the website to start downloading images from"
)
args = parser.parse_args()
while True:
_logger.info("Processing image %s...", count)
# Initialize WebDriver (Use the appropriate driver for your browser)
driver = webdriver.Chrome()
# Find the image element by its ID
image_element = driver.find_element(By.ID, "cc-comic")
# Open the website from the passed argument
_logger.info("Opening the website: %s", args.url)
driver.get(args.url)
# Get the image URL from the 'src' attribute
current_image_url = image_element.get_attribute("src")
_logger.info("Found image URL: %s", current_image_url)
image_count = 1
# Download the image if it doesn't already exist
if download_image(current_image_url):
count += 1 # Increment count only if the image was downloaded
while True:
_logger.info("Processing image %s...", image_count)
# Try to find the 'Next' button by its class
try:
_logger.info("Clicking the 'Next' button to load the next image...")
next_button = driver.find_element(By.CSS_SELECTOR, "a.cc-next")
# Find the image element by its ID
image_element = driver.find_element(By.ID, "cc-comic")
# Navigate to the URL in the 'href' of the next button
next_button_url = next_button.get_attribute("href")
driver.get(next_button_url)
# Get the image URL from the 'src' attribute
current_image_url = image_element.get_attribute("src")
_logger.info("Found image URL: %s", current_image_url)
except NoSuchElementException:
# If the 'Next' button is not found, it means we've reached the last image
_logger.info("No 'Next' button found. Reached the end of images.")
break
# Download the image if it doesn't already exist
if _download_image(current_image_url):
image_count += 1 # Increment count only if the image was downloaded
# Close the browser
_logger.info("All images processed, closing the browser.")
driver.quit()
# Try to find the 'Next' button by its class
try:
_logger.info("Clicking the 'Next' button to load the next image...")
next_button = driver.find_element(By.CSS_SELECTOR, "a.cc-next")
# Navigate to the URL in the 'href' of the next button
next_button_url = next_button.get_attribute("href")
driver.get(next_button_url)
except NoSuchElementException:
# If the 'Next' button is not found, it means we've reached the last image
_logger.info("No 'Next' button found. Reached the end of images.")
break
# Close the browser
_logger.info("All images processed, closing the browser.")
driver.quit()
if __name__ == "__main__":
main()