コード例 #1
0
def test_ensure_tuple():
    x1 = ('foo', ('foo', ), ['foo'], {'foo'})
    assert set(map(ensure_tuple, x1)) == {('foo', )}

    x2 = (pysnooper.Keys('foo'), (pysnooper.Keys('foo'), ),
          [pysnooper.Keys('foo')], {pysnooper.Keys('foo')})

    assert set(map(ensure_tuple, x2)) == {(pysnooper.Keys('foo'), )}
コード例 #2
0
def test_variables_classes():
    class WithSlots(object):
        __slots__ = ('x', 'y')

        def __init__(self):
            self.x = 3
            self.y = 4

    @pysnooper.snoop(watch=(
        pysnooper.Keys('_d', exclude='c'),
        pysnooper.Attrs('_d'),  # doesn't have attributes
        pysnooper.Attrs('_s'),
        pysnooper.Indices('_lst')[-3:],
    ))
    def my_function():
        _d = {'a': 1, 'b': 2, 'c': 'ignore'}
        _s = WithSlots()
        _lst = list(range(1000))

    with mini_toolbox.OutputCapturer(stdout=False,
                                     stderr=True) as output_capturer:
        result = my_function()
    assert result is None
    output = output_capturer.string_io.getvalue()
    assert_output(
        output,
        (VariableEntry('WithSlots'), CallEntry('def my_function():'),
         LineEntry(), VariableEntry('_d'), VariableEntry("_d['a']", '1'),
         VariableEntry("_d['b']", '2'), LineEntry(), VariableEntry('_s'),
         VariableEntry('_s.x', '3'), VariableEntry('_s.y', '4'), LineEntry(),
         VariableEntry('_lst'), VariableEntry('_lst[997]', '997'),
         VariableEntry('_lst[998]', '998'), VariableEntry(
             '_lst[999]', '999'), ReturnEntry(), ReturnValueEntry('None')))
コード例 #3
0
# You can also pass a stream or a callable instead, and they'll be used.
# See values of some expressions that aren't local variables:
@pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))

# Expand values to see all their attributes or items of lists/dictionaries:
@pysnooper.snoop(watch_explode=('foo', 'self'))
# This will output lines like:
'''
Modified var:.. foo[2] = 'whatever'
New var:....... self.baz = 8
'''
# (see Advanced Usage for more control):
import pysnooper
@pysnooper.snoop(watch=(
    pysnooper.Attrs('x'),    # attributes
    pysnooper.Keys('y'),     # mapping (e.g. dict) items
    pysnooper.Indices('z'),  # sequence (e.g. list/tuple) items
))

# 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 ')
On multi-threaded apps identify which thread are snooped in output:

@pysnooper.snoop(thread_info=True)
PySnooper supports decorating generators.

You can also customize the repr of an object: