Script Imports

Overview

In this tutorial you will learn:

  • to create a script that can be imported by another script
  • use functions of a imported script

The import statement

The import statement can be used to make functions defined in other scripts availabe the current script.The statement can either import all functions from a script at once or only a single function from the script. That way it is possible to create reusable script libraries.

The script files to be imported must be located in one of the following directories in order that FlowModeler can find them.

  • In the global scripts/ directory under the FlowModeler-Installation-Directory (On Linux: /usr/share/flowmodeler/scripts/)
  • In the scripts/ directory under the User-Data-Directory (On Linux: /home/user/.flowmodeler/scripts/).
  • In one of the paths defined as Script-Search-Path in the Preferences

In case you want to set up a new directory: create the directory in a convenient location on your machine and add the path under Script/Preprocessor/Search-Paths. Multiple paths can be specified when they are separated with asemicolon (:). In the following we assume that a directory named geometry has been created in one of the search-paths above.

Setup a script library

Scripts that serve as a library must be placed in one of the search paths of FlowModeler as described above. When setting up a library script it can be useful to think of it in the following hierarchy: libraryname.module.submodule.functionname where the libraryname is the name of the top directory within the search path, module and submodule may be either a subdirectory or the name of the scriptfile itself. and functionname the name of a function in the script file. Here we will use rectangle.flowScript as name for the script file.

Now start FlowModeler and open the Script-IDE.

Add the following code to the Text-Editor and save it as rectangle.flowScript in the geometry directory:

# computes the area of an rectangle

function area( width, height )
    assert( type.isReal( width ), "'width' must be a Real number" )
    assert( type.isReal( height ), "'height' must be a Real number" )
    assert( width greater 0, "'width' must be positive" )
    assert( height greater 0, "'height' must be positive" )
    
    value = width * height
    
    return value
endfunction

# tests

assert( area(10.0, 20.0) equal 200 )
assert( area(100.0, 200.0) equal 20000 )
assert( area(5.5, 2.8) equal 15.4 )
assert( area(10.3, 20.2) equal 208.06 )


# computes the circumference of an rectangle

function circumference( width, height )
    assert( type.isReal( width ), "'width' must be a Real number" )
    assert( type.isReal( height ), "'height' must be a Real number" )
    assert( width greater 0, "'width' must be positive" )
    assert( height greater 0, "'height' must be positive" )
    
    value = 2.0 * width + 2.0 * height
    
    return value
endfunction

# tests

assert( circumference(10.0, 20.0) equal 60 )
assert( circumference(100.0, 200.0) equal 600 )
assert( circumference(5.5, 2.8) equal 16.6 )
assert( circumference(10.3, 20.2) equal 61 )

This defines two functions in the recatange script: one to compute the area and one to compute the circumfence. As a best practice you should add some unit tests right below the functions. When developing the library functions these can easily be executed with the Play button to make sure the function works as expected. When the script is imported into another script, these tests are ignored as the import statement only looks for function definitions in the script.

Use the script library in another script

Now we will create a script that uses the rectangle module we created in the geometry library.

Create a new script with the following code:

import geometry.rectangle

# definition of the rectangle

width = 40.5
height = 62.4

area = geometry.rectangle.area( width, height )
circumference = geometry.rectangle.circumference( width, height )

print( "area: " + area )
print( "circumference: " + circumference )

In the above code first the entire rectangle module is imported from the geometry library. Then the dimensions for a rectangle are defined. At last the area and circumference are computed using the functions from the library.

Some libraries may be quite tall and define multiple functions. If only a small set of functions is needed then these can be explicitly imported ignoring the other functions in the library script:

import geometry.rectangle.area

# definition of the rectangle

width = 40.5
height = 62.4

area = geometry.rectangle.area( width, height )

print( "area: " + area )

In the above code only the area function is used. Therefore the import statement explictly imports only the area function from the rectangle module.

Conclusion

Scripts that contain function definitions can be used as libraries in other scripts. For these library scripts FlowModeler already provides directories where these can be placed or other location can be defined. As a best practice these library scripts should only contain function definitions and tests to verify the behaviour of the functions. The tests should be created right below the function in the library script. That way the tests can be executed during the developement of the library function.

The import statement can import all functions of a library script at once or only a single function.

Downloads