Addons/api/python3

From J Wiki
Jump to navigation Jump to search

Overview

This addon has functions for calling J from Python.

See the jbase.py file in the addon for installation and usage instructions.

Example Session

Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
Type "copyright", "credits" or "license" for more information.

IPython 7.19.0 -- An enhanced Interactive Python.

In [1]: import jbase

In [2]: jbase.init(True)        # True (default) loads profile - False avoids

In [3]: jbase.dor('i.2 3 4')    # run sentence and print output result
 0  1  2  3
 4  5  6  7
 8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23

In [4]: jbase.do('+a.')         # run and return error code
Out[4]: 3

In [5]: jbase.getr()            # get last output result
Out[5]: '|domain error\n|       +a.\n'

In [6]: jbase.do('abc=: i.2 3') # define abc
Out[6]: 0

In [7]: q= jbase.get('abc')     # get q as numpy array from J array

In [8]: jbase.set('ghi',23+q)   # set J array from numpy array

In [9]: jbase.dor('ghi')        # print array
23 24 25
26 27 28

In [10]: jbase.j()              # J repl - .... to exit

   +/\ p: i. 7
2 5 10 17 28 41 58

   ....

In [11]: