NumPy dtype API improvement suggestion

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

NumPy dtype API improvement suggestion

Eyal Kutz
I am interested in suggesting an API improvement for NumPy.
I wish to make it so that the following code:
@np.dtype
class Point:
    x: np.int16
    y: np.int16
would be equivalent to the following code:
Point = np.dtype([('x', np.int16), ('y', np.int16)])

I am willing to submit the code changes required to make this happen.

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: NumPy dtype API improvement suggestion

bashtage
Better would be to have an object like NamedTuple in typing that would allow

class Point(DType):
    x: np.int16
    y: np.int16



On Sun, Jul 26, 2020 at 3:22 PM Eyal Kutz <[hidden email]> wrote:
I am interested in suggesting an API improvement for NumPy.
I wish to make it so that the following code:
@np.dtype
class Point:
    x: np.int16
    y: np.int16
would be equivalent to the following code:
Point = np.dtype([('x', np.int16), ('y', np.int16)])

I am willing to submit the code changes required to make this happen.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: NumPy dtype API improvement suggestion

Eyal Kutz
In reply to this post by Eyal Kutz
Kevin Sheppard, I agree with you but I don't know how to do this. do you?

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: NumPy dtype API improvement suggestion

Feng Yu
In reply to this post by bashtage
Hi,

Would it be possible to also allow a byte offset for the field? e.g.,

class Point(np.struct):
  x: np.field('i4', offset=8)
  y: np.field('<f8', offset=32)

Best,

Yu

On Sun, Jul 26, 2020 at 9:32 AM Kevin Sheppard <[hidden email]> wrote:
Better would be to have an object like NamedTuple in typing that would allow

class Point(DType):
    x: np.int16
    y: np.int16



On Sun, Jul 26, 2020 at 3:22 PM Eyal Kutz <[hidden email]> wrote:
I am interested in suggesting an API improvement for NumPy.
I wish to make it so that the following code:
@np.dtype
class Point:
    x: np.int16
    y: np.int16
would be equivalent to the following code:
Point = np.dtype([('x', np.int16), ('y', np.int16)])

I am willing to submit the code changes required to make this happen.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: NumPy dtype API improvement suggestion

Sebastian Berg
In reply to this post by bashtage
On Sun, 2020-07-26 at 17:31 +0100, Kevin Sheppard wrote:
> Better would be to have an object like NamedTuple in typing that
> would allow
>
> class Point(DType):
>     x: np.int16
>     y: np.int16
>

I agree with this type of use case (whatever the syntax is). But I
think there are too many small issues around it currently to do that in
NumPy.  For that the new DTypes need to move along a bit further, so
that we do not lock-in nice syntax with features that have lots of
quirks. See also:
https://numpy.org/neps/nep-0041-improved-dtype-support.html
https://numpy.org/neps/nep-0042-new-dtypes.html

I think you could do much of such syntax now, but I would suggest to do
it outside of NumPy proper (at least the experimentation) at this time
for that reason. Hopefully, it won't be too long until we can think of
creating such API for good inside NumPy.

Cheers,

Sebastian


>
> On Sun, Jul 26, 2020 at 3:22 PM Eyal Kutz <[hidden email]>
> wrote:
>
> > I am interested in suggesting an API improvement for NumPy.
> > I wish to make it so that the following code:
> > @np.dtype
> > class Point:
> >     x: np.int16
> >     y: np.int16
> > would be equivalent to the following code:
> > Point = np.dtype([('x', np.int16), ('y', np.int16)])
> >
> > I am willing to submit the code changes required to make this
> > happen.
> > _______________________________________________
> > NumPy-Discussion mailing list
> > [hidden email]
> > https://mail.python.org/mailman/listinfo/numpy-discussion
> >
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (849 bytes) Download Attachment