### Install pyprof2calltree Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Installation instructions for the tool via pip or system package managers. ```bash # Install using pip pip install pyprof2calltree # On Debian/Ubuntu, install from system packages (includes kcachegrind) sudo apt install kcachegrind pyprof2calltree ``` -------------------------------- ### Launch kcachegrind via CLI Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Commands to profile or convert data and immediately open the results in kcachegrind. ```bash # Profile and launch kcachegrind pyprof2calltree -r myscript.py -k # Convert existing profile and launch kcachegrind pyprof2calltree -i profile_output.stats -k ``` -------------------------------- ### Display command line help for pyprof2calltree Source: https://github.com/pwaller/pyprof2calltree/blob/master/README.rst View available command line arguments and options. ```bash $ pyprof2calltree --help usage: pyprof2calltree [-h] [-o output_file_path] [-i input_file_path] [-k] [-r scriptfile [args ...]] optional arguments: -h, --help show this help message and exit -o output_file_path, --outfile output_file_path Save calltree stats to -i input_file_path, --infile input_file_path Read Python stats from -k, --kcachegrind Run the kcachegrind tool on the converted data -r scriptfile [args ...], --run-script scriptfile [args ...] Name of the Python script to run to collect profiling data -s {s,ms,us,ns}, --scale {s,ms,us,ns} Time scale ``` -------------------------------- ### Python API: visualize() Source: https://context7.com/pwaller/pyprof2calltree/llms.txt The visualize function converts profiling data and automatically launches a graphical viewer like kcachegrind or qcachegrind. ```APIDOC ## visualize(data) ### Description Converts profiling data and immediately launches a visual analysis tool. ### Parameters #### Arguments - **data** (cProfile entries, pstats.Stats, or str) - Required - The profiling data or path to a pstats dump file. ### Request Example ```python from pyprof2calltree import visualize visualize(profiler.getstats()) ``` ``` -------------------------------- ### Visualize Profiling Data with Python API Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Use the visualize() function to convert data and automatically launch a viewer like kcachegrind. ```python from cProfile import Profile from pyprof2calltree import visualize # Profile XML parsing performance from xml.etree import ElementTree xml_content = '\n' + '\t\n'.format(i) * 1000 for i in range(1000)) + '' profiler = Profile() profiler.enable() ElementTree.fromstring(xml_content) profiler.disable() # Launch kcachegrind with the profiling results visualize(profiler.getstats()) # Can also visualize from a saved stats file profiler.dump_stats('xml_profile.stats') visualize('xml_profile.stats') ``` -------------------------------- ### Profile and Convert Scripts via CLI Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Commands to profile a script and convert the output to calltree format. ```bash # Profile a script and save to calltree format pyprof2calltree -r myscript.py arg1 arg2 # Output: myscript.py.log (calltree format file) ``` -------------------------------- ### Profile and visualize code in a Python shell Source: https://github.com/pwaller/pyprof2calltree/blob/master/README.rst Use the cProfile module to collect stats and visualize them with kcachegrind or save them to a file. ```python >>> from xml.etree import ElementTree >>> from cProfile import Profile >>> xml_content = '\n' + '\ttext\n' * 100 + '' >>> profiler = Profile() >>> profiler.runctx( ... "ElementTree.fromstring(xml_content)", ... locals(), globals()) >>> from pyprof2calltree import convert, visualize >>> visualize(profiler.getstats()) # run kcachegrind >>> convert(profiler.getstats(), 'profiling_results.kgrind') # save for later ``` -------------------------------- ### Profile and visualize code in an IPython shell Source: https://github.com/pwaller/pyprof2calltree/blob/master/README.rst Utilize IPython magic commands to profile code and visualize the results. ```python In [1]: %doctest_mode Exception reporting mode: Plain Doctest mode is: ON >>> from xml.etree import ElementTree >>> xml_content = '\n' + '\ttext\n' * 100 + '' >>> %prun -D out.stats ElementTree.fromstring(xml_content) *** Profile stats marshalled to file 'out.stats' >>> from pyprof2calltree import convert, visualize >>> visualize('out.stats') >>> convert('out.stats', 'out.kgrind') >>> results = %prun -r ElementTree.fromstring(xml_content) >>> visualize(results) ``` -------------------------------- ### Configure Time Scale via CLI Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Set the time scale for profiling output using the -s flag. ```bash # Use milliseconds time scale pyprof2calltree -r myscript.py -s ms # Use microseconds time scale pyprof2calltree -i profile.stats -o output.log -s us # Available scales: s (seconds), ms (milliseconds), us (microseconds), ns (nanoseconds, default) ``` -------------------------------- ### Convert Existing Profile Data via CLI Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Convert a previously saved cProfile or pstats dump file to calltree format. ```bash # Convert existing profile data to calltree format pyprof2calltree -i profile_output.stats -o profile.calltree # Output: writing converted data to: profile.calltree ``` -------------------------------- ### IPython Integration with %prun and pyprof2calltree Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Integrates pyprof2calltree with IPython's magic commands for interactive profiling and visualization. Use %doctest_mode first. ```python # In IPython/Jupyter, enable doctest mode first %doctest_mode >>> from xml.etree import ElementTree >>> xml_content = '\n' + '\ttext\n' * 100 + '' # Profile with %prun and save stats >>> %prun -D out.stats ElementTree.fromstring(xml_content) *** Profile stats marshalled to file 'out.stats' # Convert and visualize >>> from pyprof2calltree import convert, visualize >>> visualize('out.stats') # Opens kcachegrind >>> convert('out.stats', 'out.kgrind') # Save for later # Or capture profile results directly >>> results = %prun -r ElementTree.fromstring(xml_content) >>> visualize(results) ``` -------------------------------- ### Integration with pstats.Stats for Analysis Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Profiles application code, creates a pstats.Stats object for analysis (filtering, sorting), and then converts the analyzed stats to calltree format. ```python from cProfile import Profile import pstats from pyprof2calltree import CalltreeConverter, convert def app_function(): # Simulate application work data = list(range(10000)) sorted_data = sorted(data) filtered = [x for x in sorted_data if x % 2 == 0] return sum(filtered) # Profile the application profiler = Profile() profiler.enable() for _ in range(10): app_function() profiler.disable() # Create pstats.Stats for analysis stats = pstats.Stats(profiler) # Use pstats features before conversion stats.strip_dirs() stats.sort_stats('cumulative') stats.print_stats(10) # Print top 10 functions # Convert the analyzed stats to calltree format converter = CalltreeConverter(stats) with open('analyzed_profile.kgrind', 'w') as f: converter.output(f) # Or use the convenience function convert(stats, 'profile_output.kgrind') ``` -------------------------------- ### Python API: convert() Source: https://context7.com/pwaller/pyprof2calltree/llms.txt The convert function processes profiling data from cProfile entries, pstats.Stats objects, or pstats files and writes the result to a specified calltree file. ```APIDOC ## convert(data, output_file) ### Description Converts profiling data to calltree format and writes it to a file. ### Parameters #### Arguments - **data** (cProfile entries, pstats.Stats, or str) - Required - The profiling data or path to a pstats dump file. - **output_file** (str) - Required - The path where the calltree output will be saved. ### Request Example ```python from pyprof2calltree import convert convert('profile.stats', 'results.kgrind') ``` ``` -------------------------------- ### Convert from Saved pstats File Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Saves profiler results to a file and then converts it to calltree format using CalltreeConverter. ```python profiler.dump_stats('saved_profile.stats') converter = CalltreeConverter('saved_profile.stats') with open('converted.kgrind', 'w') as f: converter.output(f) ``` -------------------------------- ### Convert Profiling Data with Python API Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Use the convert() function to transform profiler stats or pstats files into calltree format. ```python from cProfile import Profile from pyprof2calltree import convert # Profile some code def expensive_function(): result = sum(i * i for i in range(100000)) return result profiler = Profile() profiler.enable() expensive_function() profiler.disable() # Convert directly from profiler stats to calltree format convert(profiler.getstats(), 'profiling_results.kgrind') # Or convert from a pstats file profiler.dump_stats('profile.stats') convert('profile.stats', 'profiling_results.kgrind') ``` -------------------------------- ### Use CalltreeConverter Class Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Low-level converter class for custom output handling or in-memory processing. ```python from cProfile import Profile from pyprof2calltree import CalltreeConverter import pstats from io import StringIO # Profile some computation def compute(): data = [i ** 2 for i in range(10000)] sorted_data = sorted(data, reverse=True) return sum(sorted_data[:100]) profiler = Profile() profiler.enable() compute() profiler.disable() # Method 1: Convert from raw cProfile entries entries = profiler.getstats() converter = CalltreeConverter(entries) # Write to file with open('output.calltree', 'w') as f: converter.output(f) # Method 2: Convert from pstats.Stats object stats = pstats.Stats(profiler) converter = CalltreeConverter(stats) # Write to StringIO for in-memory processing output_buffer = StringIO() converter.output(output_buffer) calltree_data = output_buffer.getvalue() ``` -------------------------------- ### Python API: CalltreeConverter Class Source: https://context7.com/pwaller/pyprof2calltree/llms.txt A low-level class for advanced conversion scenarios, allowing custom output handling and in-memory processing. ```APIDOC ## CalltreeConverter(data) ### Description Initializes a converter instance for custom output handling. ### Methods - **output(file_handle)**: Writes the converted calltree data to the provided file-like object. ### Request Example ```python from pyprof2calltree import CalltreeConverter converter = CalltreeConverter(entries) with open('output.calltree', 'w') as f: converter.output(f) ``` ``` -------------------------------- ### CalltreeConverter with Custom Scale Source: https://context7.com/pwaller/pyprof2calltree/llms.txt Configures the time scale for calltree output, supporting 's', 'ms', 'us', 'ns'. Defaults to nanoseconds for maximum precision. ```python from cProfile import Profile from pyprof2calltree import CalltreeConverter from pyprof2calltree import Scale def workload(): return [x ** 0.5 for x in range(50000)] profiler = Profile() profiler.enable() workload() profiler.disable() # Use millisecond scale for human-readable output scale = Scale('ms') # Options: 's', 'ms', 'us', 'ns' converter = CalltreeConverter(profiler.getstats(), scale=scale) with open('output_ms.calltree', 'w') as f: converter.output(f) # Default is nanoseconds for maximum precision converter_ns = CalltreeConverter(profiler.getstats()) # Defaults to 'ns' with open('output_ns.calltree', 'w') as f: converter_ns.output(f) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.