cool-RR / PySnooper

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

Add support for custom repr #126

Closed zasdfgbnm closed 5 years ago

zasdfgbnm commented 5 years ago

This PR add an API to allow users to customize how values are printed.

To customize the print of value, the user need to pass an iterable of pairs (condition, action), where the condition will be checked in order to determine which action to take. The condition must be a callable thar return bools, and the action must return string which is the desired print of values.

zasdfgbnm commented 5 years ago

One use case is: https://github.com/zasdfgbnm/TorchSnooper

This is a tool that I just start writing to allow PySnooper work with PyTorch to help debugging PyTorch models.

cool-RR commented 5 years ago

Okay, this is nice. I wasn't sure I wanted to merge it because it adds complexity, but this could solve several problems, so let's do it.

I want you to add the following:

  1. The user could give a class instead of a callable, and isinstance will be used. (Lots of people want to customize how numpy.ndarray is displayed.) To check whether each entry is a class, do isinstance(entry, type).
  2. Add tests for a complex case, including one where a variable fits more than one condition, and ensure that it's always the first condition/class that wins.
  3. Refactor get_shortish_repr, create a function get_repr_function(item, custom_repr) that does the logic of figuring out which repr function to use. By the way, nice touch with the for..else :)
  4. Add a couple of lines about the new argument to the docstring of the Tracer class.
zasdfgbnm commented 5 years ago

@cool-RR I've made the changes according to your review. Hope it looks good this time. I am not sure if you are OK with adding numpy as a test dependency, but this makes the test more like a real use case.

zasdfgbnm commented 5 years ago

Oops,,,, seems that pypy does not support numpy? I will change the test and remove numpy...

cool-RR commented 5 years ago

Good job.

Given the problem with Pypy, I agree with not including numpy as a test dependency. Just test it on classes that you create that have a __repr__ and ensure that that __repr__ wasn't called, just the custom repr function that the user specified.

zasdfgbnm commented 5 years ago

@cool-RR Sorry I didn't see your message. Does the way I am testing it using dict work?

cool-RR commented 5 years ago

Your test looks good, yes.

cool-RR commented 5 years ago

Good job! I'll make a release in 1-2 hours.

Notice that on your second commit, you used a different committer name, try to avoid that because it makes the AUTHORS script show your name twice.