Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #124 from sMAshdot/develop
Browse files Browse the repository at this point in the history
Specify Custom Groups
  • Loading branch information
gak committed Feb 19, 2014
2 parents 35d6465 + 5c51c51 commit 89e9689
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/graphviz/example_with_submodules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .example_with_submodules import main

__all__ = [main]
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()
2 changes: 2 additions & 0 deletions examples/graphviz/example_with_submodules/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def helper(something):
return something
6 changes: 6 additions & 0 deletions examples/graphviz/example_with_submodules/submodule_one.py
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
9 changes: 9 additions & 0 deletions examples/graphviz/example_with_submodules/submodule_two.py
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)
71 changes: 71 additions & 0 deletions examples/graphviz/grouper.py
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()
1 change: 1 addition & 0 deletions pycallgraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .exceptions import PyCallGraphException
from .config import Config
from .globbing_filter import GlobbingFilter
from .grouper import Grouper
from .util import Util
from .color import Color
from .color import ColorException
4 changes: 4 additions & 0 deletions pycallgraph/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .output import outputters
from .globbing_filter import GlobbingFilter
from .grouper import Grouper


class Config(object):
Expand Down Expand Up @@ -31,6 +32,9 @@ def __init__(self, **kwargs):
include=['*'],
)

# Grouping
self.trace_grouper = Grouper()

self.did_init = True

# Update the defaults with anything from kwargs
Expand Down
26 changes: 26 additions & 0 deletions pycallgraph/grouper.py
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]
7 changes: 2 additions & 5 deletions pycallgraph/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,17 @@ def __getstate__(self):

return odict

def group(self, name):
return name.split('.', 1)[0]

def groups(self):
grp = defaultdict(list)
for node in self.nodes():
grp[self.group(node.name)].append(node)
grp[node.group].append(node)
for g in grp.iteritems():
yield g

def stat_group_from_func(self, func, calls):
stat_group = StatGroup()
stat_group.name = func
stat_group.group = self.group(func)
stat_group.group = self.config.trace_grouper(func)
stat_group.calls = Stat(calls, self.func_count_max)
stat_group.time = Stat(self.func_time.get(func, 0), self.func_time_max)
stat_group.memory_in = Stat(
Expand Down

0 comments on commit 89e9689

Please sign in to comment.