[Python-ideas] Python multi-dimensional array constructor

Todd toddrjen at gmail.com
Wed Oct 19 16:10:20 EDT 2016


On Wed, Oct 19, 2016 at 3:55 PM, Joseph Jevnik <joejev at gmail.com> wrote:

> You could add or prototype this with quasiquotes (http://quasiquotes.
> readthedocs.io/en/latest/). You just need to be able to parse the body of
> your expression as a string into an array. Here is a quick example with a
> parser that only accepts 2d arrays:
>
> ```
> # coding: quasiquotes
>
> import numpy as np
> from quasiquotes import QuasiQuoter
>
>
> @object.__new__
> class array(QuasiQuoter):
>     def quote_expr(self, expr, frame, col_offset):
>         return np.array([
>             eval('[%s]' % d, frame.f_globals, frame.f_locals)
>             for d in expr.split('||')
>         ])
>
>
> def f():
>     a = 1
>     b = 2
>     c = 3
>     return [$array| a, b, c || 4, 5, 6 |]
>
>
> if __name__ == '__main__':
>     print(f())
> ```
>

Interesting project, thanks!  If there is any actual interest in this that
might be a good way to prototype it.


> Personally I am not sold on replacing `[` and `]` with `|` because I like
> that you can visually see where dimensions are closed.
>
>
Yes, that issue occurred to me.  But assuming a rectangular matrix, I had
trouble coming up with a good example that is clearer than what you could
do with this syntax.  For simple arrays it isn't needed, and complicated
arrays are large so picking out the "[" and "]" becomes visually harder at
least for me.  Do you have a specific example that you think would be
clearer than what is possible with this syntax?

Of course that is more of an issue with jagged arrays, but numpy doesn't
support those and I am not aware of any plans to add them (dynd is another
story).

Also keep in mind that this would supplement the existing approach, it
doesn't replace it.  np.ndarray() would stay around just like list() stays
around for cases where it makes sense.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161019/ced1d496/attachment.html>


More information about the Python-ideas mailing list