[Python-ideas] Python multi-dimensional array constructor
Joseph Jevnik
joejev at gmail.com
Wed Oct 19 15:55:28 EDT 2016
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())
```
Personally I am not sold on replacing `[` and `]` with `|` because I like
that you can visually see where dimensions are closed.
On Wed, Oct 19, 2016 at 3:24 PM, Thomas Nyberg <tomuxiong at gmx.com> wrote:
> Personally I like the way that numpy does it now better (even for
> multidimensional arrays). Being able to index into the different sub
> dimension using just [] iteratively matches naturally with the data
> structure itself in my mind. This may also just be my fear of change
> though...
>
> Here is an example of how it would be used for a 1D array:
>>
>> a = [| 0, 1, 2 |]
>>
>> Compared to the current approach:
>>
>> a = np.ndarray([0, 1, 2])
>>
>
> What would the syntax do if you don't have numpy installed? Is the syntax
> tied to numpy or could other libraries make use of it?
>
> Cheers,
> Thomas
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161019/23c0423c/attachment.html>
More information about the Python-ideas
mailing list