Skip to content

Instantly share code, notes, and snippets.

@0racle
Created May 31, 2023 00:14
Show Gist options
  • Save 0racle/fed6ec55a198ad0dbc5af56b8f1352c0 to your computer and use it in GitHub Desktop.
Save 0racle/fed6ec55a198ad0dbc5af56b8f1352c0 to your computer and use it in GitHub Desktop.
JGetM on Boxed array
#!/usr/bin/env python3
from ctypes import CDLL, byref, c_void_p, c_char_p, c_longlong, string_at
class J():
def __init__(self, load_profile=False):
BIN = "/opt/j905/bin"
LIB = f"{BIN}/libj.so"
PRO = f"{BIN}/profile.ijs"
self.libj = CDLL(LIB)
self.libj.JInit.restype = c_void_p
self.libj.JGetR.restype = c_char_p
self.jt = c_void_p(self.libj.JInit())
if load_profile:
self.do(f"0!:0<'{PRO}'[BINPATH_z_=:'{BIN}'[ARGV_z_=:''");
def do(self, expr):
self.libj.JDo(self.jt, expr.encode())
def getr(self):
ptr = self.libj.JGetR(self.jt)
return string_at(ptr).decode().rstrip()
def eval(self, expr):
self.do(expr)
return self.getr()
def getm(self, name, elems):
jtype = c_longlong(0)
jrank = c_longlong(0)
jshape = c_longlong(0)
jdata = c_longlong(0)
self.libj.JGetM(
self.jt,
name.encode(),
byref(jtype),
byref(jrank),
byref(jshape),
byref(jdata),
)
return string_at(jdata.value, elems * 8)
j = J(load_profile=True)
print(j.eval("] a =. ('abc';'def')"))
print(j.eval("(3!:3) a"))
elems = 32 # arbitrary
bs = j.getm('a', elems)
data = [bs[o:o+8] for o in range(0, 8*elems, 8)]
print('--')
for buf in data:
print(' '.join(f"{b:02x}" for b in buf))
┌───┬───┐
│abc│def│
└───┴───┘
e300000000000000
2000000000000000
0200000000000000
0100000000000000
0200000000000000
3800000000000000
6800000000000000
e300000000000000
0200000000000000
0300000000000000
0100000000000000
0300000000000000
6162630000000000
e300000000000000
0200000000000000
0300000000000000
0100000000000000
0300000000000000
6465660000000000
--
40 48 bd 7f d3 55 00 00
40 41 bd 7f d3 55 00 00
00 00 00 00 00 00 00 00
40 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
98 d2 ba 7f d3 55 00 00
02 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00
24 00 00 00 00 00 00 00
01 00 92 01 00 00 00 00
24 00 00 00 00 00 00 00
20 20 55 73 65 72 4e 75
6d 62 65 72 5f 6a 61 5f
3d 3a 20 30 2d 41 50 49
4c 45 56 45 4c 5f 6a 61
5f 3c 31 37 61 20 65 6e
64 2e 91 e8 52 7f 00 00
00 08 80 01 10 00 05 00
00 00 00 14 00 00 00 00
40 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 d1 ba 7f d3 55 00 00
02 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00
1a 00 00 00 00 00 00 00
01 00 96 01 00 00 00 00
1a 00 00 00 00 00 00 00
2f 68 6f 6d 65 2f 6a 6f
73 68 75 61 2f 2e 6a 70
72 6f 66 69 6c 65 2e 69
6a 73 00 00 00 00 00 00
10 ed 8e e8 52 7f 00 00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment