Hi, is there a documented method of loading modules from python files in Analyst.
Thanks,
Allen
Hi, is there a documented method of loading modules from python files in Analyst.
Thanks,
Allen
Is the below code the type of thing you are looking to do?
q)\l pykx.q
q)np:.pykx.import[`numpy]
q)np[`:__version__]`
`2.2.6
q)np[`:arange][-3;3;0.5]`
-3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
Hey Rian,
A member of our quant team has written a custom class in his own python script. He wants to know how to load that python script and then import the custom class as a module in a different script.
Thanks,
Allen
If you just want to execute a python files contents then you can use .pykx.loadPy.
q).pykx.loadPy["file.py"]
If you specifically want to import the file as a a module then look at this example:
It has python code in qxml.py:
import json
import xmltodict
import codecs
def read(file):
with open(file, 'rb') as xml_file:
content = xml_file.read()
if content[:3] == codecs.BOM_UTF8:
content = content[3:]
data_dict = xmltodict.parse(content.decode("utf-8"))
return json.dumps(data_dict).encode()
In q add it to the sys.path and then can call .pykx.import on it:
.qxml.init:{[]
.qxml.filePath:{x -3+count x} value .z.s;
slash:$[.z.o like "w*";"\\";"/"];
.qxml.basePath:slash sv -1_slash vs .qxml.filePath;
if[not `pykx in key `;system"l pykx.q"];
.pykx.pyexec"import sys";
.pykx.pyexec "sys.path.append(\"",ssr[;"\\";"\\\\"] .qxml.basePath,"\")";
.qxml.py.lib:.pykx.import`qxml;
};
.qxml.init[];
.qxml.read:{[file]
data:.qxml.py.lib[`:read;<] 1_ string file;
.j.k data
};
Then the function can be used:
q)xmldata:.qxml.read[`:books.xml];
q).Q.id xmldata[`catalog;`book]
id author title genre price ..
-------------------------------------------------------------------------------------------------..
"bk101" "Gambardella, Matthew" "XML Developer's Guide" "Computer" "44.95"..
"bk102" "Ralls, Kim" "Midnight Rain" "Fantasy" "5.95" ..
"bk103" "Corets, Eva" "Maeve Ascendant" "Fantasy" "5.95" ..
"bk104" "Corets, Eva" "Oberon's Legacy" "Fantasy" "5.95" ..
"bk105" "Corets, Eva" "The Sundered Grail" "Fantasy" "5.95" ..
"bk106" "Randall, Cynthia" "Lover Birds" "Romance" "4.95" ..
"bk107" "Thurman, Paula" "Splish Splash" "Romance" "4.95" ..
"bk108" "Knorr, Stefan" "Creepy Crawlies" "Horror" "4.95" ..
"bk109" "Kress, Peter" "Paradox Lost" "Science Fiction" "6.95" ..
"bk110" "O'Brien, Tim" "Microsoft .NET: The Programming Bible" "Computer" "36.95"..
"bk111" "O'Brien, Tim" "MSXML3: A Comprehensive Guide" "Computer" "36.95"..
"bk112" "Galos, Mike" "Visual Studio 7: A Comprehensive Guide" "Computer" "49.95"..
Thanks Rian, this works for their use case
There’s a small issue actually that is bugging them. After following the steps above and importing the new class, if they make any changes to the original python file then running the steps again to reimport the file fails to carry over the changes. I assume that the .pykx.import is ignoring the file because it has already been imported?
This is a very minor issue for them but is there a way to unload the module or reimport it? I tried the .pykx.safeReimport analytic but it did not seem to work
Thanks
Try importlib.reload in a Python process to see if it works, then it could be attempted to be used in q if successful.
That’s worked, cheers