N00b
N00b

Reputation: 103

Calling J from Python

I have some J code that I'd like to run on an array variable from my Python script. The Python array is simply a variable with 200 floating point numbers. I am aware of memory mapped files, but this seems very low level and technical.

Is there a simple way to call a J function or script from Python without dropping out to the shell, as in:

import subprocess
subprocess.check_output(["echo function.ijs | ijconsole"])

Using this method, I first need to save out my variable into a temporary file, and the J program needs to load that file. Is there a more elegant way?

Upvotes: 3

Views: 279

Answers (1)

ephemient
ephemient

Reputation: 204768

If you have a single string to pass data to a subprocess's input, and want to read its output all at once, use Popen.communicate().

j_process = subprocess.Popen(["ijconsole"],
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE)
j_output, _ = j_process.communicate(j_input)

If the interaction is more complex, you may use communicate with Popen.stdin/Popen.stdout, but be careful - it's possible to deadlock due to buffering.

Upvotes: 2

Related Questions