Code Generation
# **Code Generation** Situation 1: Generate a Python function that takes a list of dictionaries and filters them based on a specified key-value condition, with error handling. ——————————————————————————————————————— Prompt 1: You are an expert software engineer specializing in Python function creation. Generate a Python function that takes a list of dictionaries and filters them based on a specified key-value condition. The function should accept three parameters: \- data (list of dicts): the list to filter \- key (str): the dictionary key to filter on \- value: the value to match against The function should return a filtered list of dictionaries. Example usage: data \= \[{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "user"}\] filter\_dicts(data, "role", "admin") \# Returns: \[{"name": "Alice", "role": "admin"}\] Make sure to include error handling for this function, including but not limited to these cases below: \- data is not a list \- items inside the list are not dictionaries \- key does not exist in a dictionary Include type hints on all parameters and the return value. Include a docstring that describes what the function does, its parameters, and return value. ——————————————————————————————————————— Situation 2: Generate a FastAPI endpoint that accepts a POST request with a JSON body, validates the required fields, and returns a structured response. ——————————————————————————————————————— Prompt 2: You are an expert software engineer specializing in FastAPI development. Generate a FastAPI POST endpoint for creating a contact record with the following requirements: Endpoint: POST /contacts Required fields: name (str), email (str) Optional fields: phone (str), company (str), tags (list of str) The endpoint should: \- Validate that all required fields are present \- Store the contact in an in-memory dictionary with an auto-incremented integer ID \- Return the created contact with its assigned ID and HTTP status 201 \- Return a 422 error with a descriptive message if required fields are missing Use Pydantic BaseModel for request validation. Include type hints and a docstring on the endpoint function. Example success response: {"id": 1, "name": "Alice", "email": "alice@example.com", "phone": null, "company": null, "tags": \[\]} ——————————————————————————————————————— Situation 3: Generate a Python function that calls an external REST API with retry logic and exponential backoff on 429 and 500 errors. ——————————————————————————————————————— Prompt 3: You are an expert software engineer specializing in Python and REST API implementation. Generate a Python function that calls an external REST API with retry logic and exponential backoff on 429 and 500 errors. The function should take in the following parameters: \- url (str): the endpoint to call \- method (str): GET, POST, etc. \- payload (dict): request body for POST calls \- max\_retries (int): amount of times to retry before giving up The try/except format should be used for 429 and 500 errors. Specifically: \- On 429: wait 2^attempt before trying \- On 500: same backoff \- After max\_retries exhausted: raise an exception with a descriptive message Example usage: result \= call\_api\_with\_retry(url="https://api.example.com/data", method="GET", max\_retries=3) Parse and return the JSON response using .json(). The return type should be dict | list to reflect that API responses may be either. Include type hints on all parameters and the return value. Include a docstring that describes what the function does, its parameters, and return value.
fill the variables
This prompt has 2 variables. Pro fills them into a ready-to-paste prompt for you — no manual find-and-replace.
{"name": "Alice", "role": "admin"}{"name": "Bob", "role": "user"}
Unlock with Pro →when to use it
Community prompt sourced from the open-source GitHub repo AltusInitiatives/prompt-engineering-toolkit (no explicit license). A "Code Generation" style prompt — adapt the placeholders and specifics to your task. Imported as-is and not independently retested here, so check the output before relying on it.
tags
codingcommunitydeveloper
source
AltusInitiatives/prompt-engineering-toolkit · no explicit license
more in Coding
Coding✓ tested
Senior code review (strict mode)
senior staff engineer running a merciless but fair review
Coding✓ tested
Debug by hypothesis, not by guessing
debugging partner who forms theories before touching code
Coding✓ tested
Generate tests from described behavior
test engineer who writes tests that would actually catch regressions