|
| 1 | +#!/usr/bin/env python |
| 2 | +''' |
| 3 | +This example demonstrates the use of grouping. |
| 4 | +''' |
| 5 | +from pycallgraph import PyCallGraph |
| 6 | +from pycallgraph import Config |
| 7 | +from pycallgraph import GlobbingFilter |
| 8 | +from pycallgraph import Grouper |
| 9 | +from pycallgraph.output import GraphvizOutput |
| 10 | +import example_with_submodules |
| 11 | + |
| 12 | + |
| 13 | +def run(name, trace_grouper=None, config=None, comment=None): |
| 14 | + if not config: |
| 15 | + config = Config() |
| 16 | + |
| 17 | + config.trace_filter = GlobbingFilter() |
| 18 | + |
| 19 | + if trace_grouper is not None: |
| 20 | + config.trace_grouper = trace_grouper |
| 21 | + |
| 22 | + graphviz = GraphvizOutput() |
| 23 | + graphviz.output_file = 'grouper-{}.png'.format(name) |
| 24 | + if comment: |
| 25 | + graphviz.graph_attributes['graph']['label'] = comment |
| 26 | + |
| 27 | + with PyCallGraph(config=config, output=graphviz): |
| 28 | + example_with_submodules.main() |
| 29 | + |
| 30 | + |
| 31 | +def group_none(): |
| 32 | + run( |
| 33 | + 'without', |
| 34 | + comment='Default grouping.' |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def group_some(): |
| 39 | + trace_grouper = Grouper(groups=[ |
| 40 | + 'example_with_submodules.submodule_one.*', |
| 41 | + 'example_with_submodules.submodule_two.*', |
| 42 | + 'example_with_submodules.helpers.*', |
| 43 | + ]) |
| 44 | + |
| 45 | + run( |
| 46 | + 'with', |
| 47 | + trace_grouper=trace_grouper, |
| 48 | + comment='Should assign groups to the two submodules.', |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def group_methods(): |
| 53 | + trace_grouper = Grouper(groups=[ |
| 54 | + 'example_with_submodules.*.report', |
| 55 | + ]) |
| 56 | + |
| 57 | + run( |
| 58 | + 'methods', |
| 59 | + trace_grouper=trace_grouper, |
| 60 | + comment='Should assign a group to the report methods.', |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def main(): |
| 65 | + group_none() |
| 66 | + group_some() |
| 67 | + group_methods() |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + main() |
0 commit comments