🌐
Python
docs.python.org › 3 › c-api › buffer.html
Buffer Protocol — Python 3.14.8 documentation
Buffers are usually obtained by sending a buffer request to an exporting object via PyObject_GetBuffer(). Since the complexity of the logical structure of the memory can vary drastically, the consumer uses the flags argument to specify the exact buffer type it can handle.
🌐
GitHub
github.com › python › typing › issues › 593
Buffer protocol types · Issue #593 · python/typing
November 20, 2018 - We had several typeshed issues and pull requests lately that try to work around the fact that there is no way to express that a method receives any object following the buffer protocol. The typing documentation mentions BytesType ByteStr...
Author: python
🌐
Adafruit
learn.adafruit.com › creating-and-sharing-a-circuitpython-library › typing-information
Typing Information | Creating and sharing a CircuitPython library | Adafruit Learning System
July 31, 2017 - try: # imports used only for typing from typing import Tuple # <- this line causes the error # any imports below this won't happen if the error gets raised from circuitpython_typing import ReadableBuffer from busio import I2C except ImportError: pass # ignore the error
🌐
GitHub
github.com › python › typeshed › issues › 7601
Accept `ReadableBuffer` instead of `bytes` in `base64` encode functions? · Issue #7601 · python/typeshed
April 6, 2022 - Right now, all of the encode methods in base64 accept bytes but the docs say that they should (and do) accept any bytes-like object. typeshed/stdlib/base64.pyi Line 49 in 536f783 def b64encode(s: bytes, altchars: bytes | None = ...) -> b...
Author: python
🌐
CircuitPython
docs.circuitpython.org › projects › adafruit-circuitpython-typing › en › latest › api.html
circuitpython_typing — Adafruit CircuitPython circuitpython_typing Library 1.0 documentation
circuitpython_typing.FrameBuffer: TypeAlias = ForwardRef('rgbmatrix.RGBMatrix') · Classes that implement the framebuffer protocol. circuitpython_typing.ReadableBuffer · Classes that implement the readable buffer protocol. alias of array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray ·
🌐
GitHub
github.com › python › typing › discussions › 1178
Options for typing buffers · python/typing · Discussion #1178
May 8, 2022 - AnyReadableBuf: Final = TypeVar("AnyReadableBuf", bytearray, array, memoryview, bytes) AnyWritableBuf: Final = TypeVar("AnyWritableBuf", bytearray, array, memoryview)
Author: python
🌐
Python
docs.python.org › 3.11 › c-api › objbuffer.html
Old Buffer Protocol — Python 3.11.16 documentation
Returns a pointer to a read-only memory location containing arbitrary data. The obj argument must support the single-segment readable buffer interface. On success, returns 0, sets buffer to the memory location and buffer_len to the buffer length.
🌐
Python Data Science Handbook
jakevdp.github.io › blog › 2014 › 05 › 05 › introduction-to-the-python-buffer-protocol
An Introduction to the Python Buffer Protocol | Pythonic Perambulations
The buffer protocol in Python is basically structure which defines a very flexible interface to an offset & strided multi-dimensional array. From the CPython source, we can see what it looks like: typedef struct bufferinfo{ void *buf; PyObject *obj; /* owned reference */ Py_ssize_t len; Py_ssize_t itemsize; int readonly; int ndim; char *format; Py_ssize_t *shape; Py_ssize_t *strides; Py_ssize_t *suboffsets; void *internal; }Py_buffer;
Find elsewhere
🌐
Python Morsels
pythonmorsels.com › how-does-file-buffering-work
How does file buffering work? - Python Morsels
October 28, 2022 - Instead, Python stores a buffer of bytes in the BufferedReader object that our text file wraps around, and then our text file keeps track of where it left off within that buffer.
Top answer
1 of 3
2

You can read your data as a text file instead of binary file.

Here's how:

file = open("/dev/rtlightsensor0", "r")
line = file.readline()
data = [int(each) for each in line.split())

Original answer:

You didn't describe your question clearly enough, but I'm guessing that you have four 32-bit (4 bytes) int in your file, which you want to read into your program.

os.read is a binary file read function, so the variable data you got is bytes instead of a list of numbers.

Python can't infer the length your number as it can be four 32-bit numbers, one 128-bit number, or something else. You need to define it explicitly.

There is also the issue of whether your data are big endian or little endian.

If your data doesn't come from a network communication, it's likely to be little endian. If you find the result from the following code is wrong (for example, unbelievably large), you can try to convert 'big' into 'little'.

You can finish your code like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev,16)
data = []
data.append(int.from_bytes(raw_data[0:4], 'little'))
data.append(int.from_bytes(raw_data[4:8], 'little'))
data.append(int.from_bytes(raw_data[8:12], 'little'))
data.append(int.from_bytes(raw_data[12:16], 'little'))
print(data)

However, DO NOT do it like that because it's really silly. If you can understand the code above, then you can write it better like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev, 16)
data = [int.from_bytes(raw_data[i * 4: (i + 1) * 4], 'little') for i in range(4)]
print(data)

By the way, it's not wise to read binary data like this. Generally speaking, the package numpy is recommended.

2 of 3
1

It is a byte array. You can convert the byte array to string using decode function

>>> test = b'1 53 -5 1\n'
>>> type(test)
<class 'bytes'>
>>> test[0]
49
>>> test_1 = test.decode("utf-8")
>>> test_1[0]
'1'
🌐
Delft Stack
delftstack.com › home › howto › python › buffer interface in python
Buffer in Python | Delft Stack
March 11, 2025 - The buffer interface in Python provides a way for Python objects to expose their underlying byte data. This is particularly useful for performance-critical applications where raw byte manipulation is necessary. The buffer interface allows objects to share memory without copying data, which can significantly improve the efficiency of your programs. When you work with data types like bytes, bytearray, or even NumPy arrays, you are often interacting with the buffer interface.
🌐
GitHub
github.com › python › typeshed › blob › main › stdlib › _codecs.pyi
typeshed/stdlib/_codecs.pyi at main · python/typeshed
def readbuffer_encode(data: str | ReadableBuffer, errors: str | None = None, /) -> tuple[bytes, int]: ...
Author: python
🌐
Stack Overflow
stackoverflow.com › questions › 76738308 › how-do-you-get-anystr-joiniterableanystr-to-pass-type-checks
python - How do you get AnyStr.join(Iterable[AnyStr]) to pass type checks? - Stack Overflow
July 21, 2023 - Copy/tmp/type_demo.py /tmp/type_demo.py:7:27 - error: Argument of type "Iterable[AnyStr@joiner]" cannot be assigned to parameter "__iterable_of_bytes" of type "Iterable[ReadableBuffer]" in function "join" "Iterable[AnyStr@joiner]" is incompatible with "Iterable[ReadableBuffer]" TypeVar "_T_co@Iterable" is covariant Type "str* | bytes*" cannot be assigned to type "ReadableBuffer" "str*" is incompatible with protocol "ReadableBuffer" "__buffer__" is not present (reportGeneralTypeIssues) /tmp/type_demo.py:9:27 - error: Argument of type "Iterable[AnyStr@joiner]" cannot be assigned to parameter "__
Top answer
1 of 2
5

It's hard to respond without knowing what the enums.DataPoints dtype object looks like, but I'll try to explain where you see that error message.

When you try to set (an element of) an array to some value that doesn't align properly with its dtype, you will see this. Here is an example:

In [133]: data = np.zeros((3,2), dtype="int, int")

In [134]: data
Out[134]: 
array([[(0, 0), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

In [135]: data[0, 0]
Out[135]: (0, 0)

In [136]: data[0, 0] = [1,2]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-399e78675be4> in <module>()
----> 1 data[0, 0] = [1,2]

TypeError: expected a readable buffer object

This gave an error because it can't handle two values being assigned to one element of your array. Your dtype has two values in it, so it seems reasonable to expect, but the array wants one object of type given by the dtype:

In [137]: data[0,0] = (1,2)

In [138]: data
Out[138]: 
array([[(1, 2), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

It's likely that a single set of zeros of the same shape of your array won't align with the dtype.

2 of 2
1

The answer to this was that I was not working under the same version of Numpy that my colleagues were using, as pointed out by @askewchan in the comments to his answer.

  • Failed: numpy 1.6.1
  • Works: numpy 1.7
  • Works: numpy 1.8.0
🌐
CircuitPython
docs.circuitpython.org › projects › adafruit-circuitpython-typing › en › latest
Introduction — Adafruit CircuitPython circuitpython_typing Library 1.0 documentation
ReadableBuffer · WriteableBuffer · circuitpython_typing.socket · CircuitPythonSocketType · CommonCircuitPythonSocketType · CommonCircuitPythonSocketType.connect() CommonSocketType · CommonSocketType.close() CommonSocketType.send() CommonSocketType.settimeout() InterfaceType ·
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-memoryview-and-buffers-work-23c489fbbabb
How Python’s Memoryview and Buffers Work | Medium
August 11, 2024 - In Python, the buffer protocol ... that support the buffer protocol include built-in types like bytes, bytearray, and third-party types like NumPy arrays....