paleolimbot commented on code in PR #340:
URL: https://github.com/apache/arrow-nanoarrow/pull/340#discussion_r1447983694


##########
python/src/nanoarrow/_lib.pyx:
##########
@@ -890,50 +790,129 @@ cdef class BufferView:
         self._element_size_bits = element_size_bits
         self._strides = self._item_size()
         self._shape = self._ptr.size_bytes // self._strides
+        self._format[0] = 0
+        self._populate_format()
+
+    def _addr(self):
+        return <uintptr_t>self._ptr.data.data
 
+    @property
+    def device_type(self):
+        return self._device.device_type
+
+    @property
+    def device_id(self):
+        return self._device.device_id
+
+    @property
+    def element_size_bits(self):
+        return self._element_size_bits
+
+    @property
+    def size_bytes(self):
+        return self._ptr.size_bytes
+
+    @property
+    def type(self):
+        if self._buffer_type == NANOARROW_BUFFER_TYPE_VALIDITY:
+            return "validity"
+        elif self._buffer_type == NANOARROW_BUFFER_TYPE_TYPE_ID:
+            return "type_id"
+        elif self._buffer_type == NANOARROW_BUFFER_TYPE_UNION_OFFSET:
+            return "union_offset"
+        elif self._buffer_type == NANOARROW_BUFFER_TYPE_DATA_OFFSET:
+            return "data_offset"
+        elif self._buffer_type == NANOARROW_BUFFER_TYPE_DATA:
+            return "data"
+
+    @property
+    def data_type(self):
+        return ArrowTypeString(self._buffer_data_type).decode("UTF-8")
+
+    @property
+    def format(self):
+        return self._format.decode("UTF-8")
+
+    @property
+    def item_size(self):
+        return self._strides
+
+    def __len__(self):
+        return self._shape
+
+    def __getitem__(self, int64_t i):
+        if i < 0 or i >= self._shape:
+            raise IndexError(f"Index {i} out of range")
+        cdef int64_t offset = self._strides * i
+        value = unpack_from(self.format, buffer=self, offset=offset)
+        if len(value) == 1:
+            return value[0]
+        else:
+            return value
+
+    def __iter__(self):
+        for value in iter_unpack(self.format, self):
+            if len(value) == 1:
+                yield value[0]
+            else:
+                yield value

Review Comment:
   I think the memoryview thing would be way better...I'm sure it can be 
workshopped to work most of the time. I used `=` because "standard width" 
sounded appealing but I'm sure we can do some runtime check once to maximize 
the built-in functionlity of the memoryview. (I'll defer improvements to the 
buffer view for a future PR).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to