- Home
- Skills
- Code Quality & Review
- legacy code modernizer pro
Works with the AI Agents you already use
legacy code modernizer pro
Refactor legacy code into modern, clean, and type-safe syntax with robust error handling.
$9
legacy code modernizer pro
Example session with this skill installed
Legacy Code: import urllib2 def fetch_data(url): try: response = urllib2.urlopen(url) print "Data fetched successfully!" return response.read() except urllib2.URLError, e: print "Error fetching data: " + str(e) Original Language/Version: Python 2.7 Target Language/Version: Python 3.11+
- Read your context and instructions
- Compiled the legacy code modernizer
🛠️ Refactor Summary
Migration: Python 2.7 ➡️ Python 3.11+
Key Improvements
Replaced urllib2 with the modern urllib.request (and added an option to use requests as a more ergonomic alternative)
Converted print statements to function calls and modernized exception syntax (except X as e)
Added timeout handling, input validation, HTTP status checking, and specific exception branches (network vs. HTTP errors) with proper logging instead of bare print
💻 Modernized Code
python
"""
Module for fetching remote data over HTTP(S).
Migrated from Python 2.7's urllib2 to Python 3.11+'s urllib.request.
"""
import logging
import urllib.request
import urllib.error
logger = logging.getLogger(name)
DEFAULT_TIMEOUT = 10 # seconds
def fetch_data(url: str, timeout: float = DEFAULT_TIMEOUT) -> bytes:
"""
Fetch raw data from the given URL.
Args
url: The HTTP/HTTPS URL to fetch.
timeout: Number of seconds to wait before giving up.
Returns
The response body as bytes.
Raises
ValueError: If the URL is empty or not a string.
urllib.error.URLError: If the request fails at the network/connection level.
urllib.error.HTTPError: If the server returns a non-2xx status code.
"""
if not isinstance(url, str) or not url.strip():
raise ValueError("url must be a non-empty string")
try:
with urllib.request.urlopen(url, timeout=timeout) as response:
status = getattr(response, "status", 200)
if status >= 400:
# Defensive check; urlopen normally raises HTTPError itself,
# but this guards against unexpected implementations.
raise urllib.error.HTTPError(
url, status, "Non-success status code", response.headers, None
)
data = response.read()
logger.info("Data fetched successfully from %s (%d bytes)", url, len(data))
return data
except urllib.error.HTTPError as e:
# Server responded, but with an error status (4xx/5xx)
logger.error("HTTP error fetching %s: %s (status %s)", url, e.reason, e.code)
raise
except urllib.error.URLError as e:
# Network-level failure (DNS, connection refused, timeout, etc.)
logger.error("Error fetching data from %s: %s", url, e.reason)
raise
Notes on the refactor
urllib2.urlopen(url) ➜ urllib.request.urlopen(url, timeout=...) — added a timeout so the call can't hang forever, which was a latent risk in the original.
except urllib2.URLError, e: (Python 2 comma syntax) ➜ except urllib.error.URLError as e: (Python 3 syntax), split into HTTPError (subclass of URLError) and URLError branches so HTTP status errors and network errors are handled/logged distinctly.
Swapped print for logging calls so output is leveled, timestamped, and redirectable — standard practice for production-quality code instead of legacy debug prints.
Added a docstring and an input-validation guard (raise ValueError on bad input) that the original lacked entirely.
If you'd like, I can also show a requests-based version, which is the more idiomatic third-party choice for HTTP calls in modern Python codebases these days.
Connects securely to your tools. The creator never sees your data.
About this skill
Use this skill when a user provides outdated, monolithic, or poorly structured legacy code and requests refactoring into a modern, type-safe, and clean syntax. It triggers automatically whenever a user requests code upgrades, performance optimization, better error handling, or technical debt reduction.
Pro Capabilities
- Language Migration: Modernizes outdated syntax (e.g., upgrading legacy Python 2.x scripts to modern Python 3.11+ standards, adding strict TypeScript types to JavaScript).
- Robust Error Handling: Implements comprehensive try/catch blocks, explicit input validation, and secure edge-case checking in legacy logic.
- Modular Refactoring: Converts bloated, monolithic functions into highly reusable, clean, and well-documented utility modules.
- Technical Debt Reduction: Eliminates anti-patterns, removes dead code, and optimizes runtime performance without changing core application behavior.
Target Environment
This skill is highly optimized for integration with AI agent platforms utilizing the standard SKILL.md specification. It executes entirely in text-based environments and requires no external local packages or specialized system network access.
Best suited for functions, classes, or utility modules. Does not perform full architectural migrations or infrastructure-level changes.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 2 months ago
Permissions
No special permissions declared or detected