Re: [Numpy-discussion] Custom __array_interface__ error

2015-03-13 Thread Nathaniel Smith
On Mar 13, 2015 7:22 AM, Daniel Smith dgasm...@icloud.com wrote:

 Greetings everyone,
 I have a new project that deals with core and disk tensors wrapped into a
single object so that the expressions are transparent to the user after the
tensor is formed. I would like to add __array_interface__ to the core
tensor and provide a reasonable error message if someone tries to call the
__array_interface__ for a disk tensor. I may be missing something, but I do
not see an obvious way to do this in the python layer.

Just define your class so that attempting to access __array_interface__
raises an error directly:

class DiskTensor(object):
@property
def __array_interface__(self):
raise TypeError(...)

-n
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Custom __array_interface__ error

2015-03-13 Thread Daniel Smith
Greetings everyone,
I have a new project that deals with core and disk tensors wrapped into a 
single object so that the expressions are transparent to the user after the 
tensor is formed. I would like to add __array_interface__ to the core tensor 
and provide a reasonable error message if someone tries to call the 
__array_interface__ for a disk tensor. I may be missing something, but I do not 
see an obvious way to do this in the python layer.

Currently I do something like:

if ttype == “Core:
self.__array_interface__ = self.tensor.ndarray_interface()
else:
self.__array_interface__ = {'typestr’: 'Only Core tensor types 
are supported.’}

Which provides at least a readable error message if it is not a core tensor:
TypeError: data type Only Core tensor types are supported. not understood

A easy solution I see is to change numpy C side __array_interface__ error 
message to throw custom strings.

In numpy/core/src/multiarray/ctors.c:2100 we have the __array_interface__ 
conversion:

if (!PyDict_Check(iface)) {
Py_DECREF(iface);
PyErr_SetString(PyExc_ValueError,
Invalid __array_interface__ value, must be a dict);
return NULL;
}

It could simply be changed to:

if (!PyDict_Check(iface)) {
if (PyString_Check(iface)){
PyErr_SetString(PyExc_ValueError, iface);
}
else{
PyErr_SetString(PyExc_ValueError,
Invalid __array_interface__ value, must be a dict”);
}
Py_DECREF(iface);
return NULL;
}

Thoughts?

Cheers,
-Daniel Smith
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Custom __array_interface__ error

2015-03-13 Thread Stephan Hoyer
In my experience writing ndarray-like objects, you likely want to implement
__array__ instead of __array_interface__. The former gives you full control
to create the ndarray yourself.

On Fri, Mar 13, 2015 at 7:22 AM, Daniel Smith dgasm...@icloud.com wrote:

 Greetings everyone,
 I have a new project that deals with core and disk tensors wrapped into a
 single object so that the expressions are transparent to the user after the
 tensor is formed. I would like to add __array_interface__ to the core
 tensor and provide a reasonable error message if someone tries to call the
 __array_interface__ for a disk tensor. I may be missing something, but I do
 not see an obvious way to do this in the python layer.

 Currently I do something like:

 if ttype == “Core:
 self.__array_interface__ = self.tensor.ndarray_interface()
 else:
 self.__array_interface__ = {'typestr’: 'Only Core tensor
 types are supported.’}

 Which provides at least a readable error message if it is not a core
 tensor:
 TypeError: data type Only Core tensor types are supported. not understood

 A easy solution I see is to change numpy C side __array_interface__ error
 message to throw custom strings.

 In numpy/core/src/multiarray/ctors.c:2100 we have the __array_interface__
 conversion:

 if (!PyDict_Check(iface)) {
 Py_DECREF(iface);
 PyErr_SetString(PyExc_ValueError,
 Invalid __array_interface__ value, must be a dict);
 return NULL;
 }

 It could simply be changed to:

 if (!PyDict_Check(iface)) {
 if (PyString_Check(iface)){
 PyErr_SetString(PyExc_ValueError, iface);
 }
 else{
 PyErr_SetString(PyExc_ValueError,
 Invalid __array_interface__ value, must be a dict”);
 }
 Py_DECREF(iface);
 return NULL;
 }

 Thoughts?

 Cheers,
 -Daniel Smith
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion