Pyimport Statement

The Pyimport statement is used to import description of functions and classes from Python units. Import can be executed from both the Python units implemented in the repository and from the system units installed together with or additionally to the Python language. The Pyimport statement supports two call types:

When classes are imported, only class static fields/methods are available. All input parameters and returned values will have the Variant data type. Imported fields or methods can be directly called in the Fore code or used to compare any expressions. The system supports calculation of expressions based on imported types in the watches during Fore code debugging. To facilitate code writing, all imported types will be available in IntelliSense.

Example

Executing the example required that the repository contains a Python unit with the PYTEST identifier. The unit contains the following code:

import math;
import random;

def summxy(x, y):
    
return x + y

def getrandom():
    
return random.random()

class testclass:
    fieldint = 
1
    fieldstr = 
'Test class'
    
    @staticmethod
    
def test(a, b, c = 5):
        
return a * b + c
        
    @staticmethod
    
def getpi(self):
        
return math.pi

Besides, create a unit containing the following Fore code:

// Import system units of the Python language
Pyimport math, time;
Pyimport randint From random;
// Import of Python unit from repository
Pyimport pytest;

Sub UserProc;
Var
    v: Variant;
Begin
    
//---System functions---
    Debug.WriteLine("Execute system functions:");
    v := math.sqrt(
10);
    Debug.WriteLine(
"sqrt(10) = " + v);
    v := math.log2(
3);
    Debug.WriteLine(
"log2(10) = " + v);
    v := randint(
1100);
    Debug.WriteLine(
"randint(1, 20) = " + v);
    
//---Application functions---
    Debug.WriteLine("Execute functions from application Python unit with the pytest identifier:");
    
// Single functions
    v := pytest.summxy(37);
    Debug.WriteLine(
"summxy(3,7) = " + v);
    v := pytest.getrandom;
    Debug.WriteLine(
"random = " + v);
    
// Class method
    v := pytest.testclass.getpi(Null);
    Debug.WriteLine(
"Pi = " + v);
    
// Class fields
    v := pytest.testclass.fieldint;
    Debug.WriteLine(
"fieldint = " + v);
    v := pytest.testclass.fieldstr;
    Debug.WriteLine(
"fieldstr = " + v);
End Sub UserProc;

When the UserProc procedure is executed from Fore code, various Python functions will be executed, and class field values will be obtained. All functions results and field values will be displayed in the development environment console.

See also:

Fore Language Guide