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 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] [tool.pylint.messages_control]
# Enable all checks by disabling disable # Enable all checks by disabling disable
enable = "all" enable = "all"
# Minimal disabled checks - only truly problematic ones # Minimal disabled checks - pylint internal/meta messages only
disable = [ disable = [
"raw-checker-failed", "raw-checker-failed",
"bad-inline-option", "bad-inline-option",
@ -159,8 +159,6 @@ disable = [
"useless-suppression", "useless-suppression",
"deprecated-pragma", "deprecated-pragma",
"use-symbolic-message-instead", "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] [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) _logger.exception("Failed to download %s", image_url)
requests_send = 0 def main() -> None:
while requests_send < MAX_REQUESTS: """Download cat images from TheCatAPI."""
res = requests.get( requests_sent = 0
"https://api.thecatapi.com/v1/images/search?limit=100&api_key=", while requests_sent < MAX_REQUESTS:
timeout=REQUEST_TIMEOUT, res = requests.get(
) "https://api.thecatapi.com/v1/images/search?limit=100&api_key=",
requests_send += 1 timeout=REQUEST_TIMEOUT,
response = json.loads(res.text) )
urls = [cat.get("url") for cat in response] requests_sent += 1
response = json.loads(res.text)
urls = [cat.get("url") for cat in response]
Path("./CATS2").mkdir(parents=True, exist_ok=True) Path("./CATS2").mkdir(parents=True, exist_ok=True)
for url in urls: for url in urls:
_download_single_image(url) _download_single_image(url)
if __name__ == "__main__":
main()

View File

@ -86,7 +86,8 @@ def _save_image(
return str(unique_output_path) return str(unique_output_path)
if __name__ == "__main__": def main() -> None:
"""Generate bloated JPEG images from command line arguments."""
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Generate bloated JPEG images with random colors." description="Generate bloated JPEG images with random colors."
) )
@ -165,5 +166,9 @@ if __name__ == "__main__":
quality=args.quality, quality=args.quality,
) )
for i in range(1, args.num_images + 1): for i in range(1, args.num_images + 1):
output_path = generate_bloated_jpeg(config, i, folder) result_path = generate_bloated_jpeg(config, i, folder)
_logger.info("Image %s saved to %s", i, Path(output_path).resolve()) _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 MIN_ARGS = 2
if __name__ == "__main__":
def main() -> None:
"""Run the number randomizer from command line arguments."""
if len(sys.argv) < MIN_ARGS: if len(sys.argv) < MIN_ARGS:
_logger.info( _logger.info(
"Usage: python random_digits.py <number1> <number2> ... " "Usage: python random_digits.py <number1> <number2> ... "
@ -78,27 +80,27 @@ if __name__ == "__main__":
) )
sys.exit(1) sys.exit(1)
input_string = " ".join(sys.argv[1:]) args_string = " ".join(sys.argv[1:])
numbers, decimal_counts = parse_input(input_string) numbers, decimal_counts = parse_input(args_string)
if not numbers: if not numbers:
_logger.error("No valid numbers provided.") _logger.error("No valid numbers provided.")
sys.exit(1) sys.exit(1)
min_percentage = DEFAULT_MIN_PERCENTAGE min_pct = DEFAULT_MIN_PERCENTAGE
max_percentage = DEFAULT_MAX_PERCENTAGE max_pct = DEFAULT_MAX_PERCENTAGE
try: try:
if len(sys.argv) > len(numbers) + 1: if len(sys.argv) > len(numbers) + 1:
with contextlib.suppress(ValueError): 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: if len(sys.argv) > len(numbers) + 2:
with contextlib.suppress(ValueError): 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 = [] formatted_numbers = []
for i, num in enumerate(randomized_numbers): for i, num in enumerate(randomized):
format_str = f".{decimal_counts[i]}f" format_str = f".{decimal_counts[i]}f"
formatted_numbers.append(float(format(num, format_str))) formatted_numbers.append(float(format(num, format_str)))
@ -108,3 +110,7 @@ if __name__ == "__main__":
_logger.exception("Error processing numbers") _logger.exception("Error processing numbers")
_logger.exception("Please provide valid numbers and percentages.") _logger.exception("Please provide valid numbers and percentages.")
sys.exit(1) sys.exit(1)
if __name__ == "__main__":
main()

View File

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