alexmojaki/snoop

snoop exist in a for loop after 10 iterations

syedidiflipt opened this issue · 3 comments

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

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}])

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

Extract those few lines to a new function?