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

snoop exist in a for loop after 10 iterations #59

Open
syedidiflipt opened this issue Dec 16, 2022 · 3 comments
Open

snoop exist in a for loop after 10 iterations #59

syedidiflipt opened this issue Dec 16, 2022 · 3 comments

Comments

@syedidiflipt
Copy link

I am using snoop and its very useful

I have a list variable of 20k items. I just want to see the tracing for few iterations only.

@snoop
def mapping(inputlist):
    testing = {}
    for eachDict in inputlist:
        for k, v in eachDict.items():
            testing[k] = v
            # <-------  want to exist snoop after 5 iterations
        # <-------  want to exist snoop after 10 iterations
    return testing

HOw can i achieve the above thing

@alexmojaki
Copy link
Owner

import snoop
import contextlib

def mapping(inputlist):
    for i, eachDict in enumerate(inputlist):
        context = snoop if i < 3 else contextlib.nullcontext()
        with context:
            str(eachDict)


mapping([{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}, {'e': 5}])

or

import snoop


def foo(d):
    return len(d)


foo_snooped = snoop(foo)


def mapping(inputlist):
    for i, eachDict in enumerate(inputlist):
        f = foo_snooped if i < 3 else foo
        f(eachDict)


mapping([{'a': 1}, {'b': 2}, {'c': 3}, {'d': 4}, {'e': 5}])

@syedidiflipt
Copy link
Author

how to avoid snoop for few lines of code within with snoop: or @snoop

@alexmojaki
Copy link
Owner

Extract those few lines to a new function?

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

No branches or pull requests

2 participants