Use of the Python API in Matlab and Converting Python Results to Matlab Objects

LabKey Support Forum (Inactive)
Use of the Python API in Matlab and Converting Python Results to Matlab Objects cmwest3  2017-10-24 14:04
Status: Closed
 
Matlab allows the capability to execute Python code and we have found that the use of the Python API from within Matlab can reach 20% better performance over using Matlab and the Web API. Although, converting results from Python objects to Matlab objects, specifically py.dict objects to structs using the struct() function, proves difficult due to the fact that the LabKey server will return Python Dictionaries/Matlab Structs with keys that begin with an underscore "_", specifically "_labkeyurl_DisplayName" . This violates Matlab's naming conventions and causes Matlab to throw an InvalidFieldName exception.

After searching through the LabKey source code, I believe I've found the line of code that might fix this issue: FileSystemResource.java line 107: <public static final String URL_COL_PREFIX = "_labkeyurl_";>

I could be mistaken or there could be a variable located somewhere else, but removing the leading underscore from "_labkeyurl_" to "labkeyurl_" should fix this issue. Would it be possible to incorporate this change in the next update or would there be an easier way to modify this prefix?

Thank you.
 
 
Jon (LabKey DevOps) responded:  2017-10-25 22:03
Hi Cameron,

We're looking into this, but I am not sure how that FileSystemResource.java file has any relation to the Python API. That specific file deals with WebDav File Management and doesn't seem to be related to what you're describing.

As soon as I get some confirmation on the Python API capabilities, we'll follow up with you on this forum post.

Thank you for your patience.

Regards,

Jon
 
cmwest3 responded:  2017-10-26 10:38
I was looking for the code responsible for placing an underscore in front of python dictionary keys and was thinking that might be it. But, I have found a way to rename the key within Matlab so that it conforms to struct naming conventions:

try
    matObj = struct(pyObj);
catch ME
    if strcmp(ME.identifier, 'MATLAB:Cell2Struct:InvalidFieldName')
        offending_key = regexp(ME.message, '(?<=")[^"]+(?=")', 'match');
        offending_key = offending_key{1};
        pyObj{offending_key(2:end)} = pyObj.pop(offending_key);
        matObj = struct(pyObj);
    end
end