This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from sMAshdot/develop
Specify Custom Groups
- Loading branch information
Showing
10 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .example_with_submodules import main | ||
|
||
__all__ = [main] |
13 changes: 13 additions & 0 deletions
13
examples/graphviz/example_with_submodules/example_with_submodules.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from submodule_one import SubmoduleOne | ||
from submodule_two import SubmoduleTwo | ||
|
||
|
||
def main(): | ||
s1 = SubmoduleOne() | ||
s1.report() | ||
|
||
s2 = SubmoduleTwo() | ||
s2.report() | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def helper(something): | ||
return something |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class SubmoduleOne(object): | ||
def __init__(self): | ||
self.one = 1 | ||
|
||
def report(self): | ||
return self.one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from helpers import helper | ||
|
||
|
||
class SubmoduleTwo(object): | ||
def __init__(self): | ||
self.two = 2 | ||
|
||
def report(self): | ||
return helper(self.two) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env python | ||
''' | ||
This example demonstrates the use of grouping. | ||
''' | ||
from pycallgraph import PyCallGraph | ||
from pycallgraph import Config | ||
from pycallgraph import GlobbingFilter | ||
from pycallgraph import Grouper | ||
from pycallgraph.output import GraphvizOutput | ||
import example_with_submodules | ||
|
||
|
||
def run(name, trace_grouper=None, config=None, comment=None): | ||
if not config: | ||
config = Config() | ||
|
||
config.trace_filter = GlobbingFilter() | ||
|
||
if trace_grouper is not None: | ||
config.trace_grouper = trace_grouper | ||
|
||
graphviz = GraphvizOutput() | ||
graphviz.output_file = 'grouper-{}.png'.format(name) | ||
if comment: | ||
graphviz.graph_attributes['graph']['label'] = comment | ||
|
||
with PyCallGraph(config=config, output=graphviz): | ||
example_with_submodules.main() | ||
|
||
|
||
def group_none(): | ||
run( | ||
'without', | ||
comment='Default grouping.' | ||
) | ||
|
||
|
||
def group_some(): | ||
trace_grouper = Grouper(groups=[ | ||
'example_with_submodules.submodule_one.*', | ||
'example_with_submodules.submodule_two.*', | ||
'example_with_submodules.helpers.*', | ||
]) | ||
|
||
run( | ||
'with', | ||
trace_grouper=trace_grouper, | ||
comment='Should assign groups to the two submodules.', | ||
) | ||
|
||
|
||
def group_methods(): | ||
trace_grouper = Grouper(groups=[ | ||
'example_with_submodules.*.report', | ||
]) | ||
|
||
run( | ||
'methods', | ||
trace_grouper=trace_grouper, | ||
comment='Should assign a group to the report methods.', | ||
) | ||
|
||
|
||
def main(): | ||
group_none() | ||
group_some() | ||
group_methods() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from fnmatch import fnmatch | ||
|
||
|
||
class Grouper(object): | ||
'''Group module names. | ||
By default, objects are grouped by their top-level module name. Additional | ||
groups can be specified with the groups list and all objects will be | ||
matched against it. | ||
''' | ||
|
||
def __init__(self, groups=None): | ||
if groups is None: | ||
groups = [] | ||
|
||
self.groups = groups | ||
|
||
def __call__(self, full_name=None): | ||
for pattern in self.groups: | ||
if fnmatch(full_name, pattern): | ||
if pattern[-2:] == ".*": | ||
# a wilcard in the middle is probably meaningful, while at | ||
# the end, it's only noise and can be removed | ||
return pattern[:-2] | ||
return pattern | ||
return full_name.split('.', 1)[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters