Coder Social home page Coder Social logo

Comments (6)

sumerc avatar sumerc commented on September 23, 2024 2

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
'''

from yappi.

sumerc avatar sumerc commented on September 23, 2024

Hi,

Well. I know for sure that the current filtering implementation is too simple and does not cover too many cases. The reason behind this is: when I do not see the use cases for a specific feature entirely, I do a minimum implementation and let some time to decide.

In that sense: I am all into a better implementation of filtering API(like something in Django queries?) but your current cases seemed a bit too specific to modname. So, please correct me if I am wrong but if we somehow have a regex support in modname, your problem is already solved, or maybe I am missing something?

Examples:

yappi.get_func_stats(modname_istartswith='django.db').print_all()
yappi.get_func_stats(modname_icontains='django.db').print_all()

from yappi.

MatthewWilkes avatar MatthewWilkes commented on September 23, 2024

Well, modname, at least for me, is the full path to the .py file. I'm testing this under Windows (I'm writing about Python profiling and using Windows to force myself not to write POSIX-specific things) and I see filenames. If I could do package/module identifier like django.db that'd be a great step up, and starts with would be sufficient.

from yappi.

sumerc avatar sumerc commented on September 23, 2024

Nice to know.

Ok. After thinking through this again and again, it turns out that it would be better to add new functionality rather than modifying the current behavior as it probably will break code.

What I am thinking very roughly is something like a filter_func param which will be called per stats and we will simply filter based on that.

Pros:

  • Implementation/Testing will be very simple,
  • Will not break existing code,
  • Highly customizable as it is a custom filter function,

Cons:

  • It is ugly to have two params for filtering? Or maybe not?

Example:

yappi.get_func_stats(filter={"name"}, filter_callback=my_filter_callback)

from yappi.

sumerc avatar sumerc commented on September 23, 2024

Closing this as the same behavior can be accomplished by following code(instead of filter_callback). I could not see any benefit in having another filter param when we can get a YFuncStat object and apply filtering on its properties.

stats = yappi.get_func_stats()
for stat in stats:
    if stat.module == PackageModule("apd.aggregation"):
        # do something

Here is a more detailed answer.

from yappi.

MatthewWilkes avatar MatthewWilkes commented on September 23, 2024

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

from yappi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.