Generate a Python code snippet that fulfills the following requirements:
Primary Functionality: [Describe the core purpose of the code in 1-2 sentences, e.g., 'Process data from a CSV file and generate statistical summaries'].
Input Parameters: [Specify inputs, e.g., 'A CSV file path (string), a list of numerical columns (list), and an optional output format (JSON or CSV)'].
Output: [Define the expected output, e.g., 'A dictionary or CSV string containing mean, median, and standard deviation for specified columns'].
Constraints: [List technical/functional limitations, e.g., 'Handle missing values by skipping rows, use Pandas for calculations, and ensure time complexity O(n)'].
Examples:
Sample Input: [Provide example input, e.g., 'data.csv', ['age', 'income'], 'JSON'].
Sample Output: [Provide example output, e.g., {'age': {'mean': 34.5, 'median': 32}, 'income': {'mean': 55000}}].
Error Handling: [Specify edge cases/errors to address, e.g., 'Raise FileNotFoundError if the CSV path is invalid; handle non-numeric columns gracefully'].
Additional Context: [Add extra notes, e.g., 'Optimize for large datasets (1M+ rows); avoid external libraries beyond Pandas and NumPy'].
Example Prompt
Generate a Python code snippet that fulfills the following requirements:
Primary Functionality: Create a function to fetch real-time stock prices from an API and calculate moving averages.
Input Parameters: Ticker symbol (string), API endpoint (string), time window for moving average (integer, default=14 days).
Output: A dictionary with keys current_price (float) and moving_avg (float).
Constraints: Use the requests library for API calls, cache API responses for 5 minutes to avoid rate limits, and handle weekends/holidays (no stock data).
Examples:
Sample Input: 'AAPL', 'https://api.stocks.com/v1/price', 10.
Sample Output: {'current_price': 185.6, 'moving_avg': 182.3}.
Error Handling: Raise ValueError for invalid tickers; return None if the API response is malformed.
Additional Context: Prioritize code readability; include docstrings and type hints.
Tags
pythoncode generationprogramming
About This Prompt
Category: Coding
Added: April 13, 2025
Description:
This prompt is designed to help with
coding tasks.
This prompt ensures the Python Code Generator:
Clarity: Explicitly defines inputs, outputs, and constraints, reducing ambiguity.
Efficiency: Guides the generator to optimize for performance (e.g., caching, time complexity).
Robustness: Forces error handling for real-world edge cases (invalid files, API errors).
Testability: Sample inputs/outputs act as validation checkpoints for the generated code.
Scalability: Additional context (e.g., 'optimize for 1M+ rows') tailors the code to specific environments.
Placeholders like [Ticker symbol] or [API endpoint] let users insert domain-specific details while maintaining structural rigor. This format is ideal for generating production-ready code with minimal iteration.