This is an automated email from the ASF dual-hosted git repository.

uwe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 51e117d  ARROW-2154: [Python] Implement equality on buffers
51e117d is described below

commit 51e117d31ef4e3bf26abf3c4af71ca723de13d5c
Author: Antoine Pitrou <anto...@python.org>
AuthorDate: Tue Mar 6 20:20:33 2018 +0100

    ARROW-2154: [Python] Implement equality on buffers
    
    And also on tensors.
    
    Author: Antoine Pitrou <anto...@python.org>
    
    Closes #1716 from pitrou/ARROW-2154-buffer-equality and squashes the 
following commits:
    
    b568123 <Antoine Pitrou> ARROW-2154:  Implement equality on buffers
---
 python/pyarrow/array.pxi            | 10 ++++++++--
 python/pyarrow/io.pxi               |  6 ++++++
 python/pyarrow/tests/test_io.py     | 26 +++++++++++++++++++++-----
 python/pyarrow/tests/test_tensor.py | 24 ++++++++++++++++++++----
 4 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index d4e53ec..7899d9d 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -541,6 +541,12 @@ strides: {0.strides}""".format(self)
         self._validate()
         return self.tp.Equals(deref(other.tp))
 
+    def __eq__(self, other):
+        if isinstance(other, Tensor):
+            return self.equals(other)
+        else:
+            return NotImplemented
+
     @property
     def is_mutable(self):
         self._validate()
@@ -565,12 +571,12 @@ strides: {0.strides}""".format(self)
     def shape(self):
         # Cython knows how to convert a vector[T] to a Python list
         self._validate()
-        return self.tp.shape()
+        return tuple(self.tp.shape())
 
     @property
     def strides(self):
         self._validate()
-        return self.tp.strides()
+        return tuple(self.tp.strides())
 
 
 cdef wrap_array_output(PyObject* output):
diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi
index 5c8411b..611c8a8 100644
--- a/python/pyarrow/io.pxi
+++ b/python/pyarrow/io.pxi
@@ -671,6 +671,12 @@ cdef class Buffer:
             result = self.buffer.get().Equals(deref(other.buffer.get()))
         return result
 
+    def __eq__(self, other):
+        if isinstance(other, Buffer):
+            return self.equals(other)
+        else:
+            return NotImplemented
+
     def to_pybytes(self):
         self._check_nullptr()
         return cp.PyBytes_FromStringAndSize(
diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py
index 17aca43..e42914f 100644
--- a/python/pyarrow/tests/test_io.py
+++ b/python/pyarrow/tests/test_io.py
@@ -237,6 +237,16 @@ def test_buffer_from_numpy():
 
 def test_buffer_equals():
     # Buffer.equals() returns true iff the buffers have the same contents
+    def eq(a, b):
+        assert a.equals(b)
+        assert a == b
+        assert not (a != b)
+
+    def ne(a, b):
+        assert not a.equals(b)
+        assert not (a == b)
+        assert a != b
+
     b1 = b'some data!'
     b2 = bytearray(b1)
     b3 = bytearray(b1)
@@ -246,12 +256,18 @@ def test_buffer_equals():
     buf3 = pa.frombuffer(b2)
     buf4 = pa.frombuffer(b3)
     buf5 = pa.frombuffer(np.frombuffer(b2, dtype=np.int16))
-    assert buf1.equals(buf1)
-    assert buf1.equals(buf2)
-    assert buf2.equals(buf3)
-    assert not buf2.equals(buf4)
+    eq(buf1, buf1)
+    eq(buf1, buf2)
+    eq(buf2, buf3)
+    ne(buf2, buf4)
     # Data type is indifferent
-    assert buf2.equals(buf5)
+    eq(buf2, buf5)
+
+
+def test_buffer_hashing():
+    # Buffers are unhashable
+    with pytest.raises(TypeError, match="unhashable"):
+        hash(pa.frombuffer(b'123'))
 
 
 def test_foreign_buffer():
diff --git a/python/pyarrow/tests/test_tensor.py 
b/python/pyarrow/tests/test_tensor.py
index 1d45dc7..093bc86 100644
--- a/python/pyarrow/tests/test_tensor.py
+++ b/python/pyarrow/tests/test_tensor.py
@@ -30,8 +30,8 @@ def test_tensor_attrs():
 
     assert tensor.ndim == 2
     assert tensor.size == 40
-    assert tensor.shape == list(data.shape)
-    assert tensor.strides == list(data.strides)
+    assert tensor.shape == data.shape
+    assert tensor.strides == data.strides
 
     assert tensor.is_contiguous
     assert tensor.is_mutable
@@ -121,14 +121,30 @@ def test_tensor_ipc_strided(tmpdir):
 
 
 def test_tensor_equals():
+    def eq(a, b):
+        assert a.equals(b)
+        assert a == b
+        assert not (a != b)
+
+    def ne(a, b):
+        assert not a.equals(b)
+        assert not (a == b)
+        assert a != b
+
     data = np.random.randn(10, 6, 4)[::, ::2, ::]
     tensor1 = pa.Tensor.from_numpy(data)
     tensor2 = pa.Tensor.from_numpy(np.ascontiguousarray(data))
-    assert tensor1.equals(tensor2)
+    eq(tensor1, tensor2)
     data = data.copy()
     data[9, 0, 0] = 1.0
     tensor2 = pa.Tensor.from_numpy(np.ascontiguousarray(data))
-    assert not tensor1.equals(tensor2)
+    ne(tensor1, tensor2)
+
+
+def test_tensor_hashing():
+    # Tensors are unhashable
+    with pytest.raises(TypeError, match="unhashable"):
+        hash(pa.Tensor.from_numpy(np.arange(10)))
 
 
 def test_tensor_size():

-- 
To stop receiving notification emails like this one, please contact
u...@apache.org.

Reply via email to