Products Resources Support About Us

Rocket Software

Passing a Basic Array to python

Hi All,

I have a problem that I just cannot seem to figure out,

I am calling a Python using the PyCallFunction I am passing a few variables to the Function and among them is an Array from basic. The Python Function calls a restful service with the Data ( Variables ) formatted into Json, All the other variables works 100%, But i am facing problems with the array as in basic i woud use

OPEN “FILE” TO F.FILE ELSE STOP
READ REC FROM F.FILE ELSE STOP
REC<1> = Some Var in UniVerse
REC<2> = Soem Var in UniVerse
CLOSE F.FILE

The REC<1> means i want to retrive the value of Index item 1, But when i send this to the python and the try to serialize the data in Json Char(254) is the array separator which i cannot use as in Json the array structure should be either Object :[{ “Key”:“Value”}] here i would call the key to retrive the value or Object:[ “value”, Value2"] here i would call the index [0] or [1] to retrive the value,

But UniVerse Basic gives me the array in this format

valueCHAR(254)value1CHAR(254) etc which i cannot serialize I need that array in this format
[“value”,“value1”] or [{“key”:“value”, “key1”:“value1”}]

Does someone have some advice for me maybe on what i’m missing or doing wrong?

The parameters received in Python need to be used to build u2py.DynArray() objects. These recognise the UV system delimiters correctly.
You can then use the to_list() method of the u2py.DynArray to get a python list, which you can then pass on to your JSON build function.

1 Like

There are many ways to resolve what you are trying to do, the first three I can think of are:

  1. Universe has UDO which will let you work with JSON, you could create the JSON from basic and send the JSON string to Python
  2. Python can read from UniVerse, and you could build your JSON in Python
    3.Pass two dynamic arrays(names and values) from basic to the Python function, then in the Python code marshal them into json ( or a Python dict object )

You mentioned you could not serialize the dynamic array into a json, what you are missing is the intermediate step of putting the names and values in a Python list, then creating the JSON from them.

The following should get you started: ( Note I work from the XDEMO account )

AE PP simple.py
Top of “simple.py” in “PP”, 7 lines, 174 characters.
*–: p
001: import u2py
002:
003: def showlist( stuff ):
004: a = u2py.DynArray(stuff)
005: b = a.to_list()
006: print(“The following is the dynamic array displayed as a python list:”)
007: print(str(b))
008: return str(b)
Bottom.

CT PBP MIKE

 MIKE

0001 ************************************************************************
0002 ModuleName = “simple”
0003 FuncName = “showlist”
0004
0005 REC = 1:@AM:2:@AM:3
0006 * call the Python function
0007 pyresult = PyCallFunction(ModuleName, FuncName, REC)
0008 PRINT "BACK IN BASIC: ":pyresult
0009
0010 ************************************************************************


RUN PBP MIKE
The following is the dynamic array displayed as a python list:
[‘1’, ‘2’, ‘3’]
BACK IN BASIC: [‘1’, ‘2’, ‘3’]

1 Like

Hi mrajkowski

Thanks for the advice and feedback i will test this and come back to you, I think the UDO solution will work as i am already familiar with it

Regards

Hi gregors

Thanks for the feedback

Regards