Here's a complete example that runs in about 2 seconds for 1,000,000 doubles:
the python code builds an array of doubles, makes a binary string of bytes, and writes the bytes to stdout. You could use a file if you like, just make sure it is opened in binary mode (wb). The ">" at the beginning of the pack format means "big endian".
"generate.py"
import array
import struct
import math
import sys
# windows python messes up binary newlines...unless...
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
bigmat = array.array("d")
for x in range(0,1000000):
bigmat.append(math.sqrt(x))
# %s is replaced by "1000000" ... d is for double precision 8 bytes each
binary = struct.pack(">%sd" % len(bigmat), *bigmat)
# print len(binary) # 8000000 bytes
sys.stdout.write(binary)
the JSL uses runProgram to run the python program, and reads a blob back from the stdout. BlobToMatrix specifies "big" to match the big endian data. You could use LoadTextFile( ...BLOB ) instead of runProgram if you make the python program run separately and write a file. You'd still need blobToMatrix.
"fetch.jsl"
x = runprogram(executable("C:\Python27\python.exe"),
options("C:\Users\User\Desktop\pythonExample\generate.py"),
readfunction("blob"));
xx = blobToMatrix(x,"float",8,"big");
// verify...
ok="good";
for(i=0,i<nrows(xx),i++,
if( xx[i+1] != sqrt(i), ok="bad")
);
show(ok);
ok = "good";
http://stackoverflow.com/questions/2374427/python-2-x-write-binary-output-to-stdout had the answer for the binary newline issue.
Craige