Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ef52f0

Browse files
unbornchikkenpavanky
authored andcommitted
get/set_display_dims_limit
1 parent b643f55 commit 2ef52f0

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

arrayfire/array.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
import inspect
15+
import os
1516
from .library import *
1617
from .util import *
1718
from .util import _is_number
@@ -20,6 +21,63 @@
2021
from .index import *
2122
from .index import _Index4
2223

24+
_is_running_in_py_charm = "PYCHARM_HOSTED" in os.environ
25+
26+
_display_dims_limit = None
27+
28+
def set_display_dims_limit(*dims):
29+
"""
30+
Sets the dimension limit after which array's data won't get
31+
presented to the result of str(arr).
32+
33+
Default is None, which means there is no limit.
34+
35+
Parameters
36+
----------
37+
*dims : dimension limit args
38+
39+
Example
40+
-------
41+
set_display_dims_limit(10, 10, 10, 10)
42+
43+
"""
44+
global _display_dims_limit
45+
_display_dims_limit = dims
46+
47+
def get_display_dims_limit():
48+
"""
49+
Gets the dimension limit after which array's data won't get
50+
presented to the result of str(arr).
51+
52+
Default is None, which means there is no limit.
53+
54+
Returns
55+
-----------
56+
- tuple of the current limit
57+
- None is there is no limit
58+
59+
Example
60+
-------
61+
get_display_dims_limit()
62+
# None
63+
set_display_dims_limit(10, 10, 10, 10)
64+
get_display_dims_limit()
65+
# (10, 10, 10, 10)
66+
67+
"""
68+
return _display_dims_limit
69+
70+
def _in_display_dims_limit(dims):
71+
if _is_running_in_py_charm:
72+
return False
73+
print(_display_dims_limit)
74+
if _display_dims_limit is not None:
75+
min_dim_len = min(len(_display_dims_limit), len(dims))
76+
for i in range(min_dim_len):
77+
if dims[i] > _display_dims_limit[i]:
78+
return False
79+
return True
80+
2381
def _create_array(buf, numdims, idims, dtype, is_device):
2482
out_arr = c_void_ptr_t(0)
2583
c_dims = dim4(idims[0], idims[1], idims[2], idims[3])
@@ -1185,7 +1243,7 @@ def to_list(self, row_major=False):
11851243
ct_array, shape = self.to_ctype(row_major, True)
11861244
return _ctype_to_lists(ct_array, len(shape) - 1, shape)
11871245

1188-
def to_string(self):
1246+
def __str__(self):
11891247
"""
11901248
Converts the arrayfire array to string showing its meta data and contents.
11911249
@@ -1194,6 +1252,9 @@ def to_string(self):
11941252
You can also use af.display(a, pres) to display the contents of the array with better precision.
11951253
"""
11961254

1255+
if not _in_display_dims_limit(self.dims()):
1256+
return self.__repr__();
1257+
11971258
arr_str = c_char_ptr_t(0)
11981259
be = backend.get()
11991260
safe_call(be.af_array_to_string(c_pointer(arr_str), "", self.arr, 4, True))

0 commit comments

Comments
 (0)