Creating Python Modules

A Python modules is used to store code of macros in the Python language in repository. A unit can be opened and edited in the development environment of Foresight Analytics Platform. Python code can be run without debugging in the development environment or from application code in the Fore language. It is recommended to develop and debug the macros in specialized development environments aimed at working with the Python language.

NOTE. To work with Python modules, install the Python language on your computer. For details about installation see the Connecting External Modules to Foresight Analytics Platform subsection.

Create a Python module

In the created unit write the code using Python language syntax or insert the previously written and tested macro. The development environment of Foresight Analytics Platform supports highlighting Python keywords, code autoformatting, IntelliSense, the previously set tabulation that is an important part of the Python language, is taken into account on moving to new strings. When you enter various brackets, quotation marks, apostrophes in the text, the corresponding second element is automatically added - a closing bracket, a quotation mark, an apostrophe. Auto-substitution is also implemented for the try-except structure when specifying a colon after the try keyword.

The code formatting is applied that was determined in IDE settings.

To use code from the Python module in the Fore code, use resources of the Python assembly: to execute single functions, use the Python.InvokeModule or IPythonUtils.InvokeModule method; to work with classes, use the PythonClassObject.CreateFromModule constructor. One can also use the Pyimport statement that allows for importing and directly calling functions from Python modules.

Access to Repository

The Python module code can be used to get access to the current repository connection. To do this, use the GetActive method of the MetabaseClass class. This method returns the context to work with the repository described by the IMetabase interface.

from foresight.metabase import *
def CurrentMB():
    mbcls = CMetabaseClass.Create()
    mb = mbcls.GetActive()
    # Return the current context to work with repository
    return mb

NOTE. The GetActive method can be used only on development of a Python module in the repository of Foresight Analytics Platform.

A repository connection can also be created in Python module's application code. Various examples of Python code that can be used both in a platform Python code and in development in third-party development environments are given in the Working with Python knowledge base subsection.

Link between Python Modules

The standard Python commands - import and from...import - are used to access the code implemented in other Python modules. As a parameter, specify identifier of Python module in the repository. After executing the operations contents of the Python module is imported once and is available until code execution is complete.

Consider the example of getting access to Python module code from another Python module. Executing the example requires a Python module with the TESTMODULE identifier that contains the following code:

def summXY(x, y):
    return x + y
 
class Door:
    def __init__(self, color, height, width):
        self.color = color
        self.height = height
        self.width = width
 
    def getDoorDescription(self, comment):
        return comment + ". Color: " + self.color + " Size: " + str(self.height) + "X" + str(self.width) + " mm"

The code can be accessed in the following ways:

#1
import TESTMODULE
print(TESTMODULE.summXY(10, 20))
#2
import TESTMODULE as tm
print(tm.summXY(30, 40))
#3
from TESTMODULE import Door
tclass = Door("Red", 2000, 800)
print(tclass.getDoorDescription("Interior door"))
#4
from TESTMODULE import summXY as summ
print(summ(50, 60))

Executing the second Python module with various methods will result in the access to the function and the class from the first Python module.

See also:

Developing User Application