You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DagSverreSeljebotn edited this page Jun 17, 2008
·
3 revisions
Syntax for buffer access
It's been pretty much decided on to have dtype before ndim.
cdef ndarray[float, 2] arr = x
cdef ndarray<float, 2> arr = x Problem: Want to reserve this for wrapping C++ templates
cdef ndarray(float, 2) arr = x Problem: Looks like constructor
cdef ndarray arr[float, 2] = x Problem: Want to reserve this for C arrays
cdef buf(float, 2) arr = x Problem: See explicit syntax below.
A with statement without assigning to variable, like this:
with buffer(arr, float, 2):
arr[10, 10] = 3.2
A bit verbose for many typical users though.
A with statement with assignment to a variable. See "explicit syntax" below.
Comments:
None are liked...
The problem with explicit syntax
A natural syntax would be 5. above or perhaps something like
#!python
with get_buffer(arr, float, 2) as buf:
buf[2, 3] = 10
The main problem with these kind of syntax choices is that one would often like to use the buffer interface in conjunction with higher-level operations. NumPy example:
#!python
cdef ndarray[float, 2] arr = zeros((10, 10), dtype=float)
arr += 10.3
for i in range(10): arr[i, i] = 0
arr *= 10
for i in range(10): arr[10-i, 10-i] = 0
and so on.
Having to switch between "arr" and "buf" depending on the usecase is not going to be user-friendly:
#!python
cdef ndarray arr = zeros((10, 10), dtype=float)
with get_buffer(arr, float, 2) as buf:
arr += 10.3
for i in range(10): buf[i, i] = 0
arr *= 10
for i in range(10): buf[10-i, 10-i] = 0