Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: strip prefix from filenames #86

Open
robin-wayve opened this issue Aug 1, 2021 · 7 comments
Open

Feature request: strip prefix from filenames #86

robin-wayve opened this issue Aug 1, 2021 · 7 comments
Assignees

Comments

@robin-wayve
Copy link

I tried using yappi on some code that is executed through Bazel and it was difficult to read the stats because the paths all begin with an identical, deep prefix. Something like this:

/home/user/.cache/bazel/_bazel_user/a1901621c7221d386cf4d8fa1b80a433/sandbox/sandboxfs/5/code/bazel-out/k8-opt/bin/path/in/project/target.runfiles/<the bit I actually care about starts here>

A --prefix or --strip-prefix option that would allow me to drop the predictable and repeated parts of file paths would be a help to be able to focus on the more interesting part at the end.

@sumerc
Copy link
Owner

sumerc commented Aug 9, 2021

Good idea!

Yappi is already showing the rightmost part of the string but I assume this is not enough for your case, right?

One idea might be to strip off part of the string that contains a path in sys.path. An example:

/home/XXXX/.pyenv/versions/3.9.6-debug/lib/python3.9/site-packages/mock/__init__.py

becomes

mock/__init__.py

because /home/XXXX/.pyenv/versions/3.9.6-debug/lib/python3.9/site-packages/ is in sys.path. WDYT, does it solve then noise problem on your end?

@sumerc sumerc self-assigned this Aug 9, 2021
@robin-wayve
Copy link
Author

I probably should have mentioned: the context in which I found this difficult was feeding callgrind results to gprof2dot and then visualizing with graphviz (which just shows the entire path). I worked around this by manually editing my resulting dot file to strip the prefix.

I think stripping sys.path would not be desirable: check out the resulting visualisation I got in pylint-dev/astroid#1115 -- it's useful to see which calls were inside Python vs libraries.

@sumerc
Copy link
Owner

sumerc commented Aug 9, 2021

I see.

I agree that a bit of flexibility to filter some prefixes on the output would be useful. Will implement this in the next version!
I still think --omit-sys-path might be a good improvement for some cases as well which will not be on by default(of course). Maybe I will implement both.

Again: Thanks for the idea!

@robin-wayve
Copy link
Author

Yep, makes sense. Thanks for an awesome tool!

@zanieb
Copy link

zanieb commented Mar 3, 2022

I've got a rough patch for this fwiw

YappiYFuncStat = yappi.YFuncStat


class YFuncStat(YappiYFuncStat):
    def __setattr__(self, name, value):
        if isinstance(value, str) and value.startswith("/"):
            value = trim_sys_paths(value)
        super().__setattr__(name, value)


yappi.YFuncStat = YFuncStat


def trim_sys_paths(path_string):
    trim_length = 0
    for path in sys.path:
        if path_string.startswith(path) and len(path) > trim_length:
            trim_length = len(path)

    return path_string[trim_length:].lstrip("/")

@sumerc
Copy link
Owner

sumerc commented Mar 4, 2022

Thanks @madkinsz, I would happily merge this as a PR if you can:

  • integrate it with command line
  • and add some tests for it.

@zanieb
Copy link

zanieb commented Apr 20, 2022

I'd be happy to contribute, but the methods here for detecting a path and trimming values seems sloppy. I imagine you wouldn't want this implemented on __setattr__ as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants