Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4f0e6c

Browse files
committed
Add entry for io.BytesIO.getbuffer().
1 parent 122539e commit f4f0e6c

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Doc/whatsnew/3.2.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,35 @@ implemented::
992992

993993
(Patch submitted by Daniel Urban; :issue:`5867`.)
994994

995+
io
996+
--
997+
998+
The :class:`io.BytesIO` has a new method, :meth:`~io.BytesIO.getbuffer`, which
999+
provides functionality similar to :func:`memoryview`. It creates an editable
1000+
view of the data without making a copy. The buffer's random access and support
1001+
for slice notation are well-suited to in-place editing::
1002+
1003+
import io
1004+
1005+
REC_LEN, LOC_START, LOC_LEN = 34, 7, 11
1006+
1007+
def change_location(buffer, record_number, location):
1008+
start = record_number * REC_LEN + LOC_START
1009+
buffer[start: start+LOC_LEN] = location
1010+
1011+
>>> byte_stream = io.BytesIO(
1012+
b'G3805 storeroom Main chassis '
1013+
b'X7899 shipping Reserve cog '
1014+
b'L6988 receiving Primary sprocket'
1015+
)
1016+
>>> buffer = byte_stream.getbuffer()
1017+
>>> change_location(buffer, 1, b'warehouse ')
1018+
>>> change_location(buffer, 0, b'showroom ')
1019+
>>> print(byte_stream.getvalue())
1020+
b'G3805 showroom Main chassis ' ->
1021+
b'X7899 warehouse Reserve cog ' ->
1022+
b'L6988 receiving Primary sprocket'
1023+
9951024
reprlib
9961025
-------
9971026

0 commit comments

Comments
 (0)