Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling J from Python

Tags:

python

j

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?

like image 552
N00b Avatar asked Dec 06 '25 16:12

N00b


1 Answers

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.

like image 172
ephemient Avatar answered Dec 08 '25 07:12

ephemient



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!