Massaging some code

This commit is contained in:
Ram Rachum 2019-09-15 21:51:14 +03:00
parent e5fe6986dd
commit 32c86da200
5 changed files with 83 additions and 23 deletions

View file

@ -178,6 +178,16 @@ sum_to_x(10000)
You will get `l = list(size=10000)` for the list, and `a = ndarray(shape=(10, 10), dtype=float64)` for the ndarray.
The `custom_repr` are matched in order, if one condition matches, no further conditions will be checked.
Variables and exceptions get truncated to 100 characters by default. You
can customize that:
```python
@pysnooper.snoop(max_variable_length=200)
```
You can also use `max_variable_length=None` to never truncate them.
# Installation #
You can install **PySnooper** by:

View file

@ -54,5 +54,4 @@ if __name__ == '__main__':
branch = sys.argv[1]
except IndexError:
branch = 'master'
print_authors(branch)

View file

@ -24,7 +24,8 @@ ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
def get_local_reprs(frame, watch=(), custom_repr=(), max_length=None):
code = frame.f_code
vars_order = code.co_varnames + code.co_cellvars + code.co_freevars + tuple(frame.f_locals.keys())
vars_order = (code.co_varnames + code.co_cellvars + code.co_freevars +
tuple(frame.f_locals.keys()))
result_items = [(key, utils.get_shortish_repr(value, custom_repr,
max_length))
@ -187,25 +188,20 @@ class Tracer:
Customize how values are represented as strings::
@pysnooper.snoop(custom_repr=((type1, custom_repr_func1), (condition2, custom_repr_func2), ...))
@pysnooper.snoop(custom_repr=((type1, custom_repr_func1),
(condition2, custom_repr_func2), ...))
Customize the length of truncated result::
Variables and exceptions get truncated to 100 characters by default. You
can customize that:
@pysnooper.snoop(max_variable_length=100)
@pysnooper.snoop(max_variable_length=200)
You can also use `max_variable_length=None` to never truncate them.
'''
def __init__(
self,
output=None,
watch=(),
watch_explode=(),
depth=1,
prefix='',
overwrite=False,
thread_info=False,
custom_repr=(),
max_variable_length=None,
):
def __init__(self, output=None, watch=(), watch_explode=(), depth=1,
prefix='', overwrite=False, thread_info=False, custom_repr=(),
max_variable_length=100):
self._write = get_write_function(output, overwrite)
self.watch = [

View file

@ -68,11 +68,12 @@ def get_shortish_repr(item, custom_repr=(), max_length=None):
def truncate(string, max_length):
if len(string) > max_length:
if (max_length is None) or (len(string) <= max_length):
return string
else:
left = (max_length - 3) // 2
right = max_length - 3 - left
string = u'{}...{}'.format(string[:left], string[-right:])
return string
return u'{}...{}'.format(string[:left], string[-right:])
def ensure_tuple(x):

View file

@ -385,8 +385,8 @@ def test_single_watch_no_comma():
)
def test_long_variable_with_truncate():
@pysnooper.snoop(max_variable_length=100)
def test_long_variable():
@pysnooper.snoop()
def my_function():
foo = list(range(1000))
return foo
@ -396,7 +396,7 @@ def test_long_variable_with_truncate():
result = my_function()
assert result == list(range(1000))
output = output_capturer.string_io.getvalue()
regex = r'^\[0, 1, 2, .*\.\.\..*, 997, 998, 999\]$'
regex = r'^(?=.{100}$)\[0, 1, 2, .*\.\.\..*, 997, 998, 999\]$'
assert_output(
output,
(
@ -411,6 +411,60 @@ def test_long_variable_with_truncate():
)
def test_long_variable_with_custom_max_variable_length():
@pysnooper.snoop(max_variable_length=200)
def my_function():
foo = list(range(1000))
return foo
with mini_toolbox.OutputCapturer(stdout=False,
stderr=True) as output_capturer:
result = my_function()
assert result == list(range(1000))
output = output_capturer.string_io.getvalue()
regex = r'^(?=.{200}$)\[0, 1, 2, .*\.\.\..*, 997, 998, 999\]$'
assert_output(
output,
(
SourcePathEntry(),
CallEntry('def my_function():'),
LineEntry('foo = list(range(1000))'),
VariableEntry('foo', value_regex=regex),
LineEntry(),
ReturnEntry(),
ReturnValueEntry(value_regex=regex)
)
)
def test_long_variable_with_infinite_max_variable_length():
@pysnooper.snoop(max_variable_length=None)
def my_function():
foo = list(range(1000))
return foo
with mini_toolbox.OutputCapturer(stdout=False,
stderr=True) as output_capturer:
result = my_function()
assert result == list(range(1000))
output = output_capturer.string_io.getvalue()
regex = r'^(?=.{1000,100000}$)\[0, 1, 2, [^.]+ 997, 998, 999\]$'
assert_output(
output,
(
SourcePathEntry(),
CallEntry('def my_function():'),
LineEntry('foo = list(range(1000))'),
VariableEntry('foo', value_regex=regex),
LineEntry(),
ReturnEntry(),
ReturnValueEntry(value_regex=regex)
)
)
def test_repr_exception():
class Bad(object):
def __repr__(self):