Home > Enterprise >  how can I copy the bytes from numpy.save to stdout?
how can I copy the bytes from numpy.save to stdout?

Time:11-11

I have some python that needs to send a pickled numpy array to stdout. If I try to

numpy.save(sys.stdout.buffer, data, allow_pickle=False)

I get the following error

Traceback (most recent call last):
  File "/home/thoth/work/TandbergLabs/2022/iot-rendezvous/picture-server/src/main/resources/com/ericsson/duluthMadScience/pictureServer/histogram.py", line 29, in <module>
    numpy.save(sys.stdout.buffer, data, allow_pickle=False)
  File "<__array_function__ internals>", line 180, in save
  File "/usr/lib/python3.10/site-packages/numpy/lib/npyio.py", line 502, in save
    format.write_array(fid, arr, allow_pickle=allow_pickle,
  File "/usr/lib/python3.10/site-packages/numpy/lib/format.py", line 689, in write_array
    array.tofile(fp)
OSError: obtaining file position failed

What writable file-like object can I pass to numpy.save that I can later fetch bytes from?

CodePudding user response:

You can pass any file-like object if the file has been opened in binary mode it seems.

You can see this in examples such as this one from the numpy.save documentation:

import numpy as np

with open('test.npy', 'wb') as f:
    np.save(f, np.array([1, 2]))
    np.save(f, np.array([1, 3]))
with open('test.npy', 'rb') as f:
    a = np.load(f)
    b = np.load(f)
print(a, b)

Notices that both when opening and reading the file, it passes the character 'b' to second function parameter:

with open('test.npy', 'wb') as f:
....
with open('test.npy', 'rb') as f:

This signals to the open function to open the file in binary mode, you can see more details about this in the documentation

Hope that was helpful!

CodePudding user response:

you can write to io.BytesIO buffer then write it to stdout or even transfer it over network.

import numpy as np
import io
buffer = io.BytesIO()
arr = np.array([1, 2, 3])
np.save(buffer, arr, allow_pickle=False)
bytes_value = buffer.getvalue()
print(bytes_value)
sys.stdout.buffer.write(bytes_value)  # prints it in non-human readable binary
  • Related