Skip to content

Instantly share code, notes, and snippets.

@Synthetica9
Created Jun 10, 2014
Embed
What would you like to do?
__author__ = 'Synthetica'
#PyJ, using J within Python.
#NOTES FROM AUTHOR, IGNORE THIS (OR NOT)
"""
http://www.jsoftware.com/user/dll_so.htm
http://www.jsoftware.com/help/user/calling_jdll.htm
http://jsoftware.2058.n7.nabble.com/Using-J-DLL-in-Python-td21723.html
"""
#CONFIG
jDir = "G:\\Program Files (x86)\\JQT\\bin\\"
debug = True
#IMPORTS
import ctypes as ct
import os
### DEVER CODE ###
def debugger():
# Start interactive shell, but more code might be added in the future.
JObj = J()
print "JObj =",JObj
import pdb
pdb.set_trace()
def PrintRaws(desc):
for i in desc:
print ct.c_int.from_addres(i.contents.value).value
def JTypes(desc, master):
newdesc = [item.contents.value for item in desc]
type = newdesc[0]
if debug: print type
rank = newdesc[1]
shape = ct.c_int.from_address(newdesc[2]).value
adress = newdesc[3]
#string
if type == 2:
charlist = (ct.c_char.from_address(adress+i) for i in range(shape))
return "".join((i.value for i in charlist))
#integer
if type == 4:
return ct.c_int.from_address(adress).value
#arb-price int
if type == 64:
return ct.c_int.from_address(adress).value
else:
raise Exception("Unexpected type for JType: "+ `newdesc[0]`)
#Main class: an J Object for calling and such. Links to an process.
class J(object):
def __init__(self):
self.JDll = ct.cdll.LoadLibrary(os.path.join(jDir, "j.dll"))
self.JProc = self.JDll.JInit()
def __call__(self, code):
#Exec code, I suppose.
self.JDll.JDo(self.JProc, "tmp=:"+code)
return JTypes(self.deepvar("tmp"),self)
def var(self, name):
#Isn't this redundant? Any variable can also be accessed using __call__...
#As it stands it's just a worse version of call: anything that can be done
#with this can also be done with call, and call can do things that this
#can't. The only real reason I can think of for keeping this is the fact
#that it might be a bit quicker. Might remove.
Desc = [ct.pointer(ct.c_int()) for _ in range(4)]
self.JDll.JGetM(self.JProc, name, *Desc)
return JTypes(Desc)
def deepvar(self,name):
Desc = [ct.pointer(ct.c_int()) for _ in range(4)]
self.JDll.JGetM(self.JProc, name, *Desc)
return Desc
JObj = J()
if debug and __name__ == "__main__":
debugger()
@DaneWeber

This comment has been minimized.

Copy link

@DaneWeber DaneWeber commented Jul 23, 2014

Are you still working on this? I would definitely like to be able to call J from Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment