Python Code Generator

Coding Added on April 13, 2025
Python Code Generator

Prompt

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

python code generation programming

Recently Added Prompts