From 32284703d806484cfae57dc9e735b5d67021d791 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 23 Jan 2023 09:16:55 -0600 Subject: [PATCH 1/4] add __repr__ --- dpnp/dpnp_array.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 57f057ae7608..6ae848388a9c 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -294,6 +294,8 @@ def __str__(self): return str(self.asnumpy()) + def __repr__(self): + return self._array_obj.__repr__() def __sub__(self, other): return dpnp.subtract(self, other) From 333b4fcbe977b3567bc646bccf01dfa163d12c53 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Mon, 23 Jan 2023 18:07:45 -0600 Subject: [PATCH 2/4] add __str__ --- dpnp/dpnp_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 6ae848388a9c..417664967cde 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -292,7 +292,7 @@ def __str__(self): """ - return str(self.asnumpy()) + return self._array_obj.__str__() def __repr__(self): return self._array_obj.__repr__() From 67d5cb1a41d4813f9eb16563de6c213a14438453 Mon Sep 17 00:00:00 2001 From: Vahid Tavanashad Date: Wed, 1 Feb 2023 13:13:16 -0600 Subject: [PATCH 3/4] reviewer's comments --- dpnp/dpnp_array.py | 8 ++-- tests/test_dparray.py | 107 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 417664967cde..9198b34d4d83 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -247,7 +247,10 @@ def __radd__(self, other): # '__rdivmod__', # '__reduce__', # '__reduce_ex__', - # '__repr__', + + def __repr__(self): + return dpt.usm_ndarray_repr(self._array_obj, prefix="array") + # '__rfloordiv__', # '__rlshift__', @@ -294,9 +297,6 @@ def __str__(self): return self._array_obj.__str__() - def __repr__(self): - return self._array_obj.__repr__() - def __sub__(self, other): return dpnp.subtract(self, other) diff --git a/tests/test_dparray.py b/tests/test_dparray.py index 745884f6a078..4757fc855b4f 100644 --- a/tests/test_dparray.py +++ b/tests/test_dparray.py @@ -68,3 +68,110 @@ def test_flags_strides(dtype, order, strides): assert usm_array.flags == dpnp_array.flags assert numpy_array.flags.c_contiguous == dpnp_array.flags.c_contiguous assert numpy_array.flags.f_contiguous == dpnp_array.flags.f_contiguous + +def test_print_dpnp_int(): + result = repr(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype='i4')) + expected = "array([ 1, 0, 2, -3, -1, 2, 21, -9], dtype=int32)" + assert(result==expected) + + result = str(dpnp.array([1, 0, 2, -3, -1, 2, 21, -9], dtype='i4')) + expected = "[ 1 0 2 -3 -1 2 21 -9]" + assert(result==expected) +# int32 + result = repr(dpnp.array([1, -1, 21], dtype=dpnp.int32)) + expected = "array([ 1, -1, 21], dtype=int32)" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=dpnp.int32)) + expected = "[ 1 -1 21]" + assert(result==expected) +# uint8 + result = repr(dpnp.array([1, 0, 3], dtype=numpy.uint8)) + expected = "array([1, 0, 3], dtype=uint8)" + assert(result==expected) + + result = str(dpnp.array([1, 0, 3], dtype=numpy.uint8)) + expected = "[1 0 3]" + assert(result==expected) + +def test_print_dpnp_float(): + result = repr(dpnp.array([1, -1, 21], dtype=float)) + expected = "array([ 1., -1., 21.])" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=float)) + expected = "[ 1. -1. 21.]" + assert(result==expected) +# float32 + result = repr(dpnp.array([1, -1, 21], dtype=dpnp.float32)) + expected = "array([ 1., -1., 21.], dtype=float32)" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=dpnp.float32)) + expected = "[ 1. -1. 21.]" + assert(result==expected) + +def test_print_dpnp_complex(): + result = repr(dpnp.array([1, -1, 21], dtype=complex)) + expected = "array([ 1.+0.j, -1.+0.j, 21.+0.j])" + assert(result==expected) + + result = str(dpnp.array([1, -1, 21], dtype=complex)) + expected = "[ 1.+0.j -1.+0.j 21.+0.j]" + assert(result==expected) + +def test_print_dpnp_boolean(): + result = repr(dpnp.array([1, 0, 3], dtype=bool)) + expected = "array([ True, False, True])" + assert(result==expected) + + result = str(dpnp.array([1, 0, 3], dtype=bool)) + expected = "[ True False True]" + assert(result==expected) + +def test_print_dpnp_special_character(): +# NaN + result = repr(dpnp.array([1., 0., dpnp.nan, 3.])) + expected = "array([ 1., 0., nan, 3.])" + assert(result==expected) + + result = str(dpnp.array([1., 0., dpnp.nan, 3.])) + expected = "[ 1. 0. nan 3.]" + assert(result==expected) +# inf + result = repr(dpnp.array([1., 0., numpy.inf, 3.])) + expected = "array([ 1., 0., inf, 3.])" + assert(result==expected) + + result = str(dpnp.array([1., 0., numpy.inf, 3.])) + expected = "[ 1. 0. inf 3.]" + assert(result==expected) + +def test_print_dpnp_nd(): +# 1D + result = repr(dpnp.arange(10000, dtype='float32')) + expected = "array([0.000e+00, 1.000e+00, 2.000e+00, ..., 9.997e+03, 9.998e+03,\n 9.999e+03], dtype=float32)" + assert(result==expected) + + result = str(dpnp.arange(10000, dtype='float32')) + expected = "[0.000e+00 1.000e+00 2.000e+00 ... 9.997e+03 9.998e+03 9.999e+03]" + assert(result==expected) + +# 2D + result = repr(dpnp.array([[1, 2], [3, 4]], dtype=float)) + expected = "array([[1., 2.],\n [3., 4.]])" + assert(result==expected) + + result = str(dpnp.array([[1, 2], [3, 4]])) + expected = "[[1 2]\n [3 4]]" + assert(result==expected) + +# 0 shape + result = repr(dpnp.empty( shape=(0, 0) )) + expected = "array([])" + assert(result==expected) + + result = str(dpnp.empty( shape=(0, 0) )) + expected = "[]" + assert(result==expected) + \ No newline at end of file From 30d3ada54c9140910825ec4a47c16b0e13d4be28 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 8 Feb 2023 16:20:53 -0600 Subject: [PATCH 4/4] linter changes applied --- tests/test_dparray.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_dparray.py b/tests/test_dparray.py index 4757fc855b4f..87c23f7b8781 100644 --- a/tests/test_dparray.py +++ b/tests/test_dparray.py @@ -174,4 +174,3 @@ def test_print_dpnp_nd(): result = str(dpnp.empty( shape=(0, 0) )) expected = "[]" assert(result==expected) - \ No newline at end of file