cool-RR / PySnooper

Never use print for debugging again
MIT License
16.39k stars 954 forks source link

Help wanted: Advanced Usage 'exclude variable' #139

Closed rickgeorges closed 5 years ago

rickgeorges commented 5 years ago

The 'Advanced Usage' section in the docs are not that clear to me. Sorry if this if easy to figure out but I'm not sure how to use the classes in variables.py How can I achieve this:

@pysnooper.snoop(exclude='token')
def sum_to_x(x):
    token = 'secret'
    l = list(range(x))
    a = numpy.zeros((10,10))
    return sum(l)

Thanks

alexmojaki commented 5 years ago

There is no way to do this right now. You're probably thinking of something like snoop(watch=Keys('thing', exclude=['token'])), which would show all the keys/values of the mapping thing except for the key token. There's nothing for excluding entire variables.

What's your use case here? Are you using this in production and want to keep secrets out of logs? Do you want to do this across many functions? Is the variable name always the same, or does it at least always contain the word token?

rickgeorges commented 5 years ago

I'm using this in production and want to keep the token out of the logs for our user's with read access. This would be applied across multiple functions. Here snoop(watch=Keys('thing', exclude=['token'])) 'thing' would have to be a list, set, or tuple? I keep getting TypeError: 'Keys' object is not iterable when I try to use it. For example

from pysnooper.variables import *
from pysnooper import *
import pysnooper

@pysnooper.snoop(watch=Keys('beta', exclude='age'))
def foo():
  alpha=['a', 'b', 'c']
  beta={'name':'Jack', 'age': 26}
  for char in alpha:
    print(char)

Traceback (most recent call last):
  File "app.py", line 12, in <module>
    @pysnooper.snoop(watch=Keys('beta', exclude='age'))
  File "/Users/rgeorg229/Library/Python/2.7/lib/python/site-packages/pysnooper/tracer.py", line 202, in __init__
    for v in utils.ensure_tuple(watch)
  File "/Users/rgeorg229/Library/Python/2.7/lib/python/site-packages/pysnooper/utils.py", line 83, in ensure_tuple
    return tuple(x)
TypeError: 'Keys' object is not iterable
alexmojaki commented 5 years ago

@cool-RR update ensure_tuple to use if not isinstance(x, (list, set, tuple)):

@rickgeorges watch=[Keys('beta', exclude='age')] should work. But it doesn't answer your original question, I was just explaining where exclude is relevant.

Various features could be added to try to keep secrets out of the output. But there's always going to be a risk of them appearing anyway, e.g. as a part of some bigger variable with an innocent name that you forgot about. For that reason I don't think it's worth adding these features. Why is the output of snoop readable by people who shouldn't have access to these secrets? Can you not direct it somewhere else with the first argument?

cool-RR commented 5 years ago

@alexmojaki Thanks for the tip about ensure_tuple, I'm fixing that.

Otherwise I agree it's not worth adding these features.