A poor man's debugger for Python.
Project description
PySnooper - Never use print for debugging again
PySnooper is a poor man's debugger.
You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a full-fledged debugger with breakpoints and watches, but you can't be bothered to set one up right now.
You want to know which lines are running and which aren't, and what the values of the local variables are.
Most people would use print
lines, in strategic locations, some of them showing the values of variables.
PySnooper lets you do the same, except instead of carefully crafting the right print
lines, you just add one decorator line to the function you're interested in. You'll get a play-by-play log of your function, including which lines ran and when, and exactly when local variables were changed.
What makes PySnooper stand out from all other code intelligence tools? You can use it in your shitty, sprawling enterprise codebase without having to do any setup. Just slap the decorator on, as shown below, and redirect the output to a dedicated log file by specifying its path as the first argument.
Example
We're writing a function that converts a number to binary, by returing a list of bits. Let's snoop on it by adding the @pysnooper.snoop()
decorator:
import pysnooper
@pysnooper.snoop()
def number_to_bits(number):
if number:
bits = []
while number:
number, remainder = divmod(number, 2)
bits.insert(0, remainder)
return bits
else:
return [0]
number_to_bits(6)
The output to stderr is:
Starting var:.. number = 6
21:14:32.099769 call 3 @pysnooper.snoop()
21:14:32.099769 line 5 if number:
21:14:32.099769 line 6 bits = []
New var:....... bits = []
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
New var:....... remainder = 0
Modified var:.. number = 3
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
Modified var:.. number = 1
Modified var:.. remainder = 1
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [1, 0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 8 number, remainder = divmod(number, 2)
Modified var:.. number = 0
21:14:32.099769 line 9 bits.insert(0, remainder)
Modified var:.. bits = [1, 1, 0]
21:14:32.099769 line 7 while number:
21:14:32.099769 line 10 return bits
21:14:32.099769 return 10 return bits
Features
If stderr is not easily accessible for you, you can redirect the output to a file easily:
@pysnooper.snoop('/my/log/file.log')
See values of some variables that aren't local variables:
@pysnooper.snoop(variables=('foo.bar', 'self.whatever'))
Show snoop lines for functions that your function calls:
@pysnooper.snoop(depth=2)
Start all snoop lines with a prefix, to grep for them easily:
@pysnooper.snoop(prefix='ZZZ ')
Installation
Use pip
:
pip install pysnooper
Copyright
Copyright (c) 2019 Ram Rachum, released under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file PySnooper-0.0.4.tar.gz
.
File metadata
- Download URL: PySnooper-0.0.4.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b0f169da435f87221f1db14304149d928b976d47d4590eac58fae20ecb72a1f |
|
MD5 | ca1e2a898f629d0e369bfe95a2d662d3 |
|
BLAKE2b-256 | 94fc7b08fe383fbe0dc4cfb189f887c2f829de5eb847b223e53a43b04eccefae |
File details
Details for the file PySnooper-0.0.4-py2.py3-none-any.whl
.
File metadata
- Download URL: PySnooper-0.0.4-py2.py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c609e3f5e3b57c564925af0e510ed4cd3f9876efd8d4aeadeee24b8a5248405b |
|
MD5 | 57df3a5457472d7e3f589fc77c0e79a0 |
|
BLAKE2b-256 | 54278f3b2c49501cf6e5500a5d7c0cfcecc38e1ab9a836e911603e1502c25e16 |