Item(Index: Integer): Variant;
Item[Index: Integer]: Object;
Index. List element index.
The Item property returns value of list element with the specified index.
Executing the example requires a folder with Pythons units: d:\Work\Python\. The folder contains the sample.en unit. The following function is implemented in the unit:
import random
def getNewList(count):
res = []
i = 0
while i < count:
res.append(random.uniform(0, 100))
i = i + 1
return res
Add a link to the Python system assembly.
Sub UserProc;
Var
pUtils: IPythonUtils;
pList: IPythonList;
Result, v: Variant;
Arr: Array;
Begin
pUtils := New PythonUtils.Create;
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Get a list which is a result of the function execution
Result := pUtils.Invoke("sample", "getNewList", 10);
//Create a list from obtained result of the getNewList function
pList := New PythonList.Create(Result);
//As the getNewList function result is an object, the list created in pList
//will contain one element. This element must be cast to the IPythonList type, in order
//to work with separate elements from the list from the getNewList result
pList := pList.Item(0) As IPythonList;
Debug.WriteLine(pList.Length);
Arr := pList.ToArray;
For Each v In Arr Do
Debug.Write(v + ", ");
End For;
End Sub UserProc;
Imports Prognoz.Platform.Interop.Python;
Public Shared Sub Main(Params: StartParams);
Var
pUtils: IPythonUtils = New PythonUtilsClass();
pList: IPythonList = New PythonListClass();
Result, v: Object;
Arr: Array;
Begin
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Get a list that is a result of the function execution
Result := pUtils.Invoke("sample", "getNewList", 10);
//Create a list from the obtained result of the getNewList function
pList.Create(Result);
//As the getNewList function result is an object, then created list in pList
//will contain one element. This element must be cast to the IPythonList type, in order to
//work with separate list elements from the getNewList result
pList := pList.Item[0] As IPythonList;
System.Diagnostics.Debug.WriteLine(pList.Length);
Arr := pList.ToArray();
For Each v In Arr Do
System.Diagnostics.Debug.Write(v + ", ");
End For;
End Sub;
On executing the example the getNewList Python function is executed. This function returns a list with random numbers. The obtained list will be transformed into a Fore array. Values of array elements are displayed in the development environment console.
See also: