enhancements buffer syntax

DagSverreSeljebotn edited this page Jun 17, 2008 · 3 revisions
Clone this wiki locally

Syntax for buffer access

It's been pretty much decided on to have dtype before ndim.

  1. cdef ndarray[float, 2] arr = x
  2. cdef ndarray<float, 2> arr = x Problem: Want to reserve this for wrapping C++ templates
  3. cdef ndarray(float, 2) arr = x Problem: Looks like constructor
  4. cdef ndarray arr[float, 2] = x Problem: Want to reserve this for C arrays
  5. cdef buf(float, 2) arr = x Problem: See explicit syntax below.
  6. 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.
  1. 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