Retrofit a Python Script with Argparse, Logging & Retries Prompt
Take an ad-hoc Python script with hardcoded values, print() debugging, and no error recovery, and add a proper argparse CLI, structured logging, and bounded retries with backoff.
- Target user
- Engineers hardening throwaway Python scripts for production use
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who turns quick-and-dirty scripts into operable tools. Keep the core logic intact; add the operational scaffolding it lacks. I will paste: - The current script (with hardcoded paths/URLs and `print()` calls) - Which inputs should become CLI flags vs config vs environment - Which operations are flaky (network calls, external commands) and what counts as a transient vs permanent failure Your job: 1. **Add a real CLI** — wire `argparse` with typed flags, sensible defaults, `--help` text, a `--dry-run`, and a `-v/--verbose` that maps to log level. Pull every hardcoded value out into a flag or env var. 2. **Replace prints with logging** — configure the `logging` module once in `main()`; route operational messages to stderr with timestamps and levels, keep machine output on stdout. 3. **Add bounded retries** — wrap only the genuinely transient calls in a retry helper with exponential backoff and jitter, a max-attempts cap, and a total-time budget. Do NOT retry non-idempotent writes or 4xx-class errors. 4. **Fail clearly** — distinguish expected failures (clean message, specific exit code) from bugs (traceback). Return a meaningful exit code from `main()`. 5. **Keep it testable** — make the retry policy and clock injectable so retries can be unit-tested without real sleeps. Output: the refactored script, a table of the new flags and their env-var fallbacks, the retry policy (which errors retry, which don't, and why), and a snippet showing how to test the backoff deterministically.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python Multi-Log Merge and Timeline Correlator Prompt
Build a Python tool that merges several log files with different timestamp formats into one time-ordered timeline, tags each line with its source, and correlates events across services for incident analysis
-
Asyncio TaskGroup Structured-Concurrency Python Script Prompt
Write a Python 3.11+ automation script that fans work out with asyncio.TaskGroup so failures cancel siblings cleanly, errors aggregate into an ExceptionGroup, and no task is ever silently orphaned.
-
Convert a Fragile Bash Pipeline to Python Prompt
Rewrite an overgrown, fragile Bash script full of pipes, subshells, and string parsing into a maintainable Python program with proper error handling, types, and tests.
-
Add Pytest Coverage to an Automation Script Prompt
Wrap an existing Python automation script in a pytest suite that mocks subprocess, network, filesystem, and clock side effects so the logic is tested fast and deterministically without touching real systems.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.