浏览代码

calcex.Figure.__sum__: accept int 0 as universal neutral element

Fabian Peter Hammerle 7 年之前
父节点
当前提交
7e1d9a565f
共有 3 个文件被更改,包括 11 次插入4 次删除
  1. 4 1
      ioex/calcex.py
  2. 2 2
      setup.py
  3. 5 1
      tests/calcex/test_figure.py

+ 4 - 1
ioex/calcex.py

@@ -108,7 +108,10 @@ class Figure(object):
         return not (self == other)
 
     def __add__(self, other):
-        if not isinstance(self, type(other)):
+        """ allows self + sum([]) """
+        if isinstance(other, type(EMPTY_SUM)) and other == EMPTY_SUM:
+            return copy.deepcopy(self)
+        elif not isinstance(self, type(other)):
             raise NotImplementedError('{!r} + {!r}'.format(self, other))
         assert not self.value is None
         assert not other.value is None

+ 2 - 2
setup.py

@@ -5,12 +5,12 @@ import glob
 setup(
     name = 'ioex',
     packages = ['ioex'],
-    version = '0.16.0',
+    version = '0.17.0',
     description = 'extension for python\'s build-in input / output interface',
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',
     url = 'https://github.com/fphammerle/ioex',
-    download_url = 'https://github.com/fphammerle/ioex/tarball/0.13.0',
+    download_url = 'https://github.com/fphammerle/ioex/tarball/0.17.0',
     keywords = [],
     classifiers = [],
     scripts = glob.glob('scripts/*'),

+ 5 - 1
tests/calcex/test_figure.py

@@ -161,7 +161,7 @@ def test_add_sum():
 
 
 @pytest.mark.parametrize(('a', 'b'), [
-    [Figure(1, 'm'), 0],
+    [Figure(1, 'm'), 0.0],
     [Figure(1, 'm'), 'test'],
     [1, Figure(1, 'm')],
 ])
@@ -170,6 +170,10 @@ def test_add_not_implemented(a, b):
         a + b
 
 
+def test_add_null_summand():
+    assert Figure(1, ['m']) == Figure(1, ['m']) + sum([])
+
+
 @pytest.mark.parametrize(('a', 'b', 'expected_diff'), [
     [Figure(1, 'm'), Figure(2, 'm'), Figure(-1, 'm')],
     [Figure(-2, 'l'), Figure(-4, 'l'), Figure(2, 'l')],