Skip to content

Commit 89e9689

Browse files
committedFeb 19, 2014
Merge pull request gak#124 from sMAshdot/develop
Specify Custom Groups
2 parents 35d6465 + 5c51c51 commit 89e9689

File tree

10 files changed

+137
-5
lines changed

10 files changed

+137
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .example_with_submodules import main
2+
3+
__all__ = [main]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from submodule_one import SubmoduleOne
2+
from submodule_two import SubmoduleTwo
3+
4+
5+
def main():
6+
s1 = SubmoduleOne()
7+
s1.report()
8+
9+
s2 = SubmoduleTwo()
10+
s2.report()
11+
12+
if __name__ == "__main__":
13+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def helper(something):
2+
return something
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class SubmoduleOne(object):
2+
def __init__(self):
3+
self.one = 1
4+
5+
def report(self):
6+
return self.one
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from helpers import helper
2+
3+
4+
class SubmoduleTwo(object):
5+
def __init__(self):
6+
self.two = 2
7+
8+
def report(self):
9+
return helper(self.two)

‎examples/graphviz/grouper.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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()

‎pycallgraph/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .exceptions import PyCallGraphException
1717
from .config import Config
1818
from .globbing_filter import GlobbingFilter
19+
from .grouper import Grouper
1920
from .util import Util
2021
from .color import Color
2122
from .color import ColorException

‎pycallgraph/config.py

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .output import outputters
55
from .globbing_filter import GlobbingFilter
6+
from .grouper import Grouper
67

78

89
class Config(object):
@@ -31,6 +32,9 @@ def __init__(self, **kwargs):
3132
include=['*'],
3233
)
3334

35+
# Grouping
36+
self.trace_grouper = Grouper()
37+
3438
self.did_init = True
3539

3640
# Update the defaults with anything from kwargs

‎pycallgraph/grouper.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from fnmatch import fnmatch
2+
3+
4+
class Grouper(object):
5+
'''Group module names.
6+
7+
By default, objects are grouped by their top-level module name. Additional
8+
groups can be specified with the groups list and all objects will be
9+
matched against it.
10+
'''
11+
12+
def __init__(self, groups=None):
13+
if groups is None:
14+
groups = []
15+
16+
self.groups = groups
17+
18+
def __call__(self, full_name=None):
19+
for pattern in self.groups:
20+
if fnmatch(full_name, pattern):
21+
if pattern[-2:] == ".*":
22+
# a wilcard in the middle is probably meaningful, while at
23+
# the end, it's only noise and can be removed
24+
return pattern[:-2]
25+
return pattern
26+
return full_name.split('.', 1)[0]

‎pycallgraph/tracer.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,17 @@ def __getstate__(self):
290290

291291
return odict
292292

293-
def group(self, name):
294-
return name.split('.', 1)[0]
295-
296293
def groups(self):
297294
grp = defaultdict(list)
298295
for node in self.nodes():
299-
grp[self.group(node.name)].append(node)
296+
grp[node.group].append(node)
300297
for g in grp.iteritems():
301298
yield g
302299

303300
def stat_group_from_func(self, func, calls):
304301
stat_group = StatGroup()
305302
stat_group.name = func
306-
stat_group.group = self.group(func)
303+
stat_group.group = self.config.trace_grouper(func)
307304
stat_group.calls = Stat(calls, self.func_count_max)
308305
stat_group.time = Stat(self.func_time.get(func, 0), self.func_time_max)
309306
stat_group.memory_in = Stat(

0 commit comments

Comments
 (0)
Please sign in to comment.