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

Conveniences for tracing entire classes or files #2

Open
alexmojaki opened this issue Jun 30, 2019 · 1 comment
Open

Conveniences for tracing entire classes or files #2

alexmojaki opened this issue Jun 30, 2019 · 1 comment
Labels
discussion Need to hear people's opinions

Comments

@alexmojaki
Copy link
Owner

Sometimes you may want to trace large chunks of your project without having to stick a decorator on each one and without setting a high depth which would trace lots of functions you don't care about. Below are some possible APIs. Which would you like? Any alternative ideas?

  1. Decorate a class to trace all of its methods. Simply put @snoop at the top of the class. The question is, what would this do?

    1. Decorate all functions defined directly in the class by parsing the file and looking at the locations of function definitions. Involves some magic but is very doable.
    2. Decorate all functions in the class __dict__, ignoring functions in base classes, but including functions that have been set as attributes rather than defined directly in the class.

    I like option (i) because it can bypass decorators on the methods and access the original underlying functions. Someone decorating a class may not consider that function decorators can get in the way and be surprised when a function doesn't get snooped - they might even think it's not being called. On the other hand it would surprise experienced Python developers who would intuitively expect something like (ii). It would also require leaving the tracing function on at all times which would hurt performance and get in the way of other debuggers.

    I could also provide both options and document them, but I'd prefer to keep the API lean and these concepts are not easy to explain concisely.

  2. Call a function like snoop.this_file() which snoops all methods in the file. Again there are options similar to (i) and (ii) above, either tracing the functions defined statically in the file or dynamically looking at the global variables, descending into classes. The first option would again require keeping the tracer on at all times.

  3. Call a function like snoop.files(lambda filename: some_condition), e.g. snoop.files(lambda filename: "my_package" in filename), to trace all files satisfying the condition. Hypothetically this could again have options like (i) and (ii), but it would almost certainly be (i).

@ChillarAnand
Copy link

ChillarAnand commented Feb 21, 2023

For tracing entire files, if we can have a -m module flag to run files directly without modifying the code, that would be great. Many tools provide this convenient flag to run the scripts.

python -m snoop run my_script.py

Another related issue: #36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Need to hear people's opinions
Projects
None yet
Development

No branches or pull requests

2 participants