From 32c86da200f8b61933fa5a3c790e75c4ede42604 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Sun, 15 Sep 2019 21:51:14 +0300 Subject: [PATCH] Massaging some code --- README.md | 10 +++++++ misc/generate_authors.py | 1 - pysnooper/tracer.py | 28 ++++++++----------- pysnooper/utils.py | 7 +++-- tests/test_pysnooper.py | 60 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 6112552..ac82704 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/misc/generate_authors.py b/misc/generate_authors.py index 582e077..5836dd7 100644 --- a/misc/generate_authors.py +++ b/misc/generate_authors.py @@ -54,5 +54,4 @@ if __name__ == '__main__': branch = sys.argv[1] except IndexError: branch = 'master' - print_authors(branch) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index f64f749..3ea6580 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -24,7 +24,8 @@ ipython_filename_pattern = re.compile('^$') 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 = [ diff --git a/pysnooper/utils.py b/pysnooper/utils.py index a675a09..15dbc3b 100644 --- a/pysnooper/utils.py +++ b/pysnooper/utils.py @@ -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): diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 9f190d1..500c959 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -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):