From d449f3ed91861ef811473584c60f22307641b6ec Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Fri, 3 May 2019 12:18:25 +0300 Subject: [PATCH] Massaging some code --- pysnooper/__init__.py | 1 + pysnooper/variables.py | 7 ++++--- tests/test_pysnooper.py | 22 ++++++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pysnooper/__init__.py b/pysnooper/__init__.py index ff2aa4c..a2c0126 100644 --- a/pysnooper/__init__.py +++ b/pysnooper/__init__.py @@ -2,6 +2,7 @@ # This program is distributed under the MIT license. from .pysnooper import snoop +from .variables import Attrs, Exploded, Indices, Keys import collections __VersionInfo = collections.namedtuple('VersionInfo', diff --git a/pysnooper/variables.py b/pysnooper/variables.py index a5a0a6f..c556cda 100644 --- a/pysnooper/variables.py +++ b/pysnooper/variables.py @@ -1,12 +1,13 @@ import itertools -from abc import ABC, abstractmethod +import abc from collections import Mapping, Sequence from copy import deepcopy from . import utils +from . import pycompat -class BaseVariable(ABC): +class BaseVariable(pycompat.ABC): def __init__(self, source, exclude=()): self.source = source self.exclude = utils.ensure_tuple(exclude) @@ -19,7 +20,7 @@ class BaseVariable(ABC): return () return self._items(main_value) - @abstractmethod + @abc.abstractmethod def _items(self, key): raise NotImplementedError diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index 5e5ac67..40435a5 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -3,13 +3,12 @@ import io import textwrap -from types import SimpleNamespace +import types from python_toolbox import sys_tools, temp_file_tools import pytest import pysnooper -from pysnooper.variables import Attrs, Keys, Indices from .utils import (assert_output, VariableEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry, ReturnValueEntry, ExceptionEntry) @@ -127,10 +126,16 @@ def test_variables(): def test_exploded_variables(): - @pysnooper.snoop(exploded_variables='_d _point lst'.split()) + class Foo: + def __init__(self, x, y): + self.x = x + self.y = y + + + @pysnooper.snoop(exploded_variables=('_d', '_point', 'lst')) def my_function(): _d = {'a': 1, 'b': 2, 'c': 'ignore'} - _point = SimpleNamespace(x=3, y=4) + _point = Foo(x=3, y=4) lst = [7, 8, 9] lst.append(10) @@ -142,6 +147,7 @@ def test_exploded_variables(): assert_output( output, ( + VariableEntry('Foo'), CallEntry('def my_function():'), LineEntry(), VariableEntry("(_d)['a']", '1'), @@ -175,10 +181,10 @@ def test_variables_classes(): self.y = 4 @pysnooper.snoop(variables=( - Keys('_d', exclude='c'), - Attrs('_d'), # doesn't have attributes - Attrs('_s'), - Indices('_lst')[-3:], + 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'}