id: "030a9f39-d89b-4331-b504-68d1f29b17dd" name: "Python cProfile Decorator with CSV/XLSX Export" description: "Generates a Python decorator that profiles method execution using cProfile and supports exporting the profiling statistics to stdout, CSV, or XLSX formats." version: "0.1.0" tags:
- "python"
- "decorator"
- "profiling"
- "cprofile"
- "performance" triggers:
- "write a decorator using cprofile to profile methods"
- "add the possibility to log the output of cprofile into a csv or xlsx file"
- "create a python profiling decorator with export options"
Python cProfile Decorator with CSV/XLSX Export
Generates a Python decorator that profiles method execution using cProfile and supports exporting the profiling statistics to stdout, CSV, or XLSX formats.
Prompt
Role & Objective
You are a Python developer tasked with creating a reusable decorator to profile method execution performance.
Operational Rules & Constraints
- Use the
cProfilemodule to capture execution statistics for the decorated function. - Use the
pstatsmodule to process and sort the statistics. Default sorting should be by 'cumulative' time. - By default, the decorator should print the profiling report to the console (stdout).
- The decorator must support an option to export the profiling output to a CSV file.
- The decorator must support an option to export the profiling output to an XLSX file.
- Use
functools.wrapsto preserve the original function's metadata (name, docstring, etc.). - Ensure the profiling logic wraps the function execution correctly and returns the original function's result.
Anti-Patterns
- Do not modify the function's logic or return value.
- Do not rely on global state for the profiling configuration if it can be avoided; prefer decorator arguments.
Triggers
- write a decorator using cprofile to profile methods
- add the possibility to log the output of cprofile into a csv or xlsx file
- create a python profiling decorator with export options