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 077a31cb54
commit 3a477dd868
3 changed files with 81 additions and 64 deletions

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:
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_send += 1
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)
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,11 +37,28 @@ 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()
# Open the website from the passed argument
_logger.info("Opening the website: %s", args.url)
driver.get(args.url)
image_count = 1
while True:
_logger.info("Processing image %s...", image_count)
# Find the image element by its ID
image_element = driver.find_element(By.ID, "cc-comic")
@ -67,8 +68,8 @@ while True:
_logger.info("Found image URL: %s", current_image_url)
# 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
if _download_image(current_image_url):
image_count += 1 # Increment count only if the image was downloaded
# Try to find the 'Next' button by its class
try:
@ -84,6 +85,10 @@ while True:
_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()
# Close the browser
_logger.info("All images processed, closing the browser.")
driver.quit()
if __name__ == "__main__":
main()