question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Filter stats by package

See original GitHub issue

Hi there,

It would be lovely to filter stats by package. Because you’re using PyObject_RichCompareBool as part of the filter it’s already possible to achieve this with a helper object. Would you be interested in integrating this as a feature with a stable API?

My current implementation is:

import dataclasses
import importlib
import os

@dataclasses.dataclass
class PackageModule:
    package: str
    
    def __post_init__(self):
        mod = importlib.import_module(self.package)
        self.fn = mod.__file__
        if self.fn.endswith("__init__.py"):
            self.fn = os.path.dirname(self.fn)
    
    def __eq__(self, other):
        return other.startswith(self.fn)
        

yappi.get_func_stats(filter={"modname": PackageModule("apd.aggregation")}).print_all()

There are caveats to this, mainly that it requires the module to be importable, and not undesirable to import (such as potential import-time side-effects), but I think it improves the usability a fair bit.

What do you think? If you’re interested I’m happy to put together a PR.

Matt

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sumerccommented, Apr 8, 2020

We have a new API param for this filter_callback in get_func_stats().

Here is an example from the docs:

import package_a
import yappi
import sys

def a():
    pass

def b():
    pass

yappi.start()
a()
b()
package_a.a()
yappi.stop()

# filter by module object
current_module = sys.modules[__name__]
stats = yappi.get_func_stats(
    filter_callback=lambda x: yappi.module_matches(x, [current_module])
)  # x is a yappi.YFuncStat object
stats.sort("name", "desc").print_all()
'''
Clock type: CPU
Ordered by: name, desc

name                                  ncall  tsub      ttot      tavg
doc2.py:10 b                          1      0.000001  0.000001  0.000001
doc2.py:6 a                           1      0.000001  0.000001  0.000001
'''

# filter by function object
stats = yappi.get_func_stats(
    filter_callback=lambda x: yappi.func_matches(x, [a, b])
).print_all()
'''
name                                  ncall  tsub      ttot      tavg
doc2.py:6 a                           1      0.000001  0.000001  0.000001
doc2.py:10 b                          1      0.000001  0.000001  0.000001
'''

# filter by module name
stats = yappi.get_func_stats(filter_callback=lambda x: 'package_a' in x.module
                             ).print_all()
'''
name                                  ncall  tsub      ttot      tavg
package_a/__init__.py:1 a             1      0.000001  0.000001  0.000001
'''

# filter by function name
stats = yappi.get_func_stats(filter_callback=lambda x: 'a' in x.name
                             ).print_all()
'''
name                                  ncall  tsub      ttot      tavg
doc2.py:6 a                           1      0.000001  0.000001  0.000001
package_a/__init__.py:1 a             1      0.000001  0.000001  0.000001
'''
0reactions
MatthewWilkescommented, Apr 8, 2020

Thanks, I’ll update the example code and docs I wrote.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow to filter environment in Organization Stats · Issue #9141 ...
My product, Sleuth, integrates with Sentry to pull down error counts, but because this feature is missing both here and for project stats,...
Read more >
Filtering Stats iQ Data - Qualtrics
From the dropdown menu, choose the variable that you would like to base the filter on. Variable dropdown list in filter; Select an...
Read more >
Inverse statistics? (maybe feature request) :: Dyson Sphere ...
The most efficient way that I know of to find them is to go into the statistics panel, select them as a favorite,...
Read more >
Filter Feature Requests by Customer Attributes - Savio
With Savio, you can filter your feature requests by these attributes. This is an extremely powerful feature. Because you know that not all...
Read more >
Video: Sort, filter, summarize, and calculate your PivotTable data
Training: You can analyze PivotTable data in many ways, including sorting to quickly see trends. We'll also look at how to filter, summarize...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found