← index #2552Issue #4064
Off-topic · high · value 0.327
QUERY · ISSUE

Add .offset()/.seek() method for memoryview for inplace "slicing"

openby pfalconopened 2016-10-23updated 2018-12-01
enhancementrfc

Here's an idiom to send a large data buffer thru a short-write stream (e.g., non-blocking socket):

        buffer = memoryview(data).cast('b')
        while buffer:
            try:
                nsent = self._socket_send(buffer, flags)
                buffer = buffer[nsent:]

The last statement still produces bunch of object garbage. If we could do

buffer.offset(nsent)

it would be cool. Suggestions for better name are welcome.

CANDIDATE · ISSUE

Missing `memoryview.cast` in MicroPython

openby Pequeopened 2018-08-19updated 2024-10-02
enhancement

It seems memoryview has no cast() method in MicroPython, has it?

Is there another way to create a memoryview out of an array.array('h', array_size) that results in a bytearray-like memory view without making a copy of the buffer?

In code, for better clarity:

import array


size = 64
a = array.array('h', range(size))
m = memoryview(a).cast('B')
assert len(m) == 2 * size

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied