LSPModeler Class

class LSPModeler

Modeler environment. Main class of the Modeler library which enables the creation and manipulation of a virtual machine that can load and execute programs written in the LSP language (see the language reference for more information).

Since:

10.0

Summary

Attributes

stdout

Stream used by the modeler for its standard output methods.

stderr

Stream used by the modeler for its standard error output methods.

Methods

create_solver

Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.run().

get_module

Returns the module with the given name.

add_module_lookup_path

Adds a new lookup path for modules.

clear_module_lookup_paths

Deletes all paths used to search for modules.

load_module

Loads a program written in LSP format into a LSPModule whose name corresponds to the filename.

create_module

Creates an empty module with the given name.

create_map

Creates a LSPMap.

create_function

Creates an external LSPFunction.

delete

Deletes this modeler environment and all associated resources.

get_stdout

Returns the stream used by the modeler for its standard output methods like print.

set_stdout

Sets the stream used by the modeler for its standard output methods like print.

get_stderr

Returns the stream used by the modeler for its standard error output.

set_stderr

Sets the stream used by the modeler for its standard error output.

Instance attributes

LSPModeler.stdout

Stream used by the modeler for its standard output methods. This is a shortcut for LSPModeler.get_stdout().

LSPModeler.stderr

Stream used by the modeler for its standard error output methods. This is a shortcut for LSPModeler.get_stderr().

Instance methods

LSPModeler.create_solver()

Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.run(). The returned solver will be automatically disposed when the modeler is destroyed or when the current reference scope is released.

Returns:

Solver.

Return type:

LocalSolver

See:

LocalSolver

LSPModeler.get_module(module_name)

Returns the module with the given name. User modules can be accessed as well as builtin modules like the JSON module or the CSV module. If the module is not loaded, this method attempts to load it from the paths specified by add_module_lookup_path().

The variables of the module can then be manipulated through the associated LSPModule object.

Parameters:

module_name (str) – Module name.

Returns:

Module created.

Return type:

LSPModule

LSPModeler.add_module_lookup_path(path)

Adds a new lookup path for modules. The paths specified when calling this method are taken into account when the LSP virtual machine loads a module, either by a direct call to the get_module() method, or indirectly when loading a dependency to another module. By default, the lookup path contains at least the current runtime folder.

Parameters:

path (str) – New path to consider for modules.

LSPModeler.clear_module_lookup_paths()

Deletes all paths used to search for modules. This method deletes all the paths previously added by add_module_lookup_path() method, as well as the default location(s) (including the current execution folder).

__Important note:__ after calling this method, you must at least add a path with AddModuleLookupPath(). Without a path, you won’t be able to load any LSP modules other than the native ones supplied by the virtual machine.

LSPModeler.load_module(module_name, filepath)

Loads a program written in LSP format into a LSPModule whose name corresponds to the filename. The variables of the module can be manipulated through the returned module object.

Parameters:
  • module_name (str) – Name taken by the module once loaded.

  • filepath (str) – Path to the module file.

Returns:

Module loaded.

Return type:

LSPModule

LSPModeler.create_module(module_name)

Creates an empty module with the given name. The variables of the module can be manipulated through the returned module object.

Parameters:

module_name (str) – Name of the module.

Returns:

Module created.

Return type:

LSPModule

LSPModeler.create_map()

Creates a LSPMap. A map is a data structure mapping keys to values that can also be used as an array-like structure. The map can be assigned to any variable in a module with LSPModule.__setitem__() or can be part of another map with LSPMap.__setitem__().

Returns:

Map created.

Return type:

LSPMap

LSPModeler.create_function(name, function)
LSPModeler.create_function(function)

Creates an external LSPFunction. The provided function must take a modeler instance as first argument and the LSP function arguments as subsequent arguments. For instance, the following example creates a simple function that accepts two arguments and returns the sum of both values. The generated function is then exposed in an LSP module under the name “myCustomFunction”:

def my_custom_function(modeler, arg1, arg2):
    return arg1 + arg2

module["myCustomFunction"] = modeler.create_function(my_custom_function)

Note: This method should only be used to expose functions used during the modeling process. You should not use this method to create a function that will be used during the resolution as an external function. In this case, you should instead use the solver API directly (see LSModel.create_int_external_function() or LSModel.create_double_external_function() for example).

Parameters:
  • function – A python function that accepts a modeler instance as first argument and the LSP function arguments as subsequent arguments. The function must returns a value whose type is supported by the modeler (None, int, float, str, LSPMap, LSPFunction, LSPModule, LSExpression)

  • name (str) – Name of the function (optional). The name is only used to identify the function in the generated stack trace when an exception occurs. Once created, the function can be associated with any variable in any module, regardless of its name.

Returns:

Function created.

Return type:

LSPFunction

LSPModeler.delete()

Deletes this modeler environment and all associated resources. This also deletes the solvers created using LSPModeler.create_solver(). This method is automatically called when you use the LSPModeler in a with statement.

LSPModeler.get_stdout()

Returns the stream used by the modeler for its standard output methods like print. The default stream used by the modeler corresponds to the Python standard output, retrieved with sys.stdout.

You can also use the shortcut member stdout

Returns:

The stream used by the modeler for its standard output or null if the standard output is disabled.

LSPModeler.set_stdout(stream)

Sets the stream used by the modeler for its standard output methods like print. The default is to redirect all the modeler’s outputs to the Python standard output. If the given stream is null, the standard output of the modeler will be disabled and all calls to the print related functions will do nothing.

Parameters:

stream – stream to use for the standard output or null to disable standard output.

LSPModeler.get_stderr()

Returns the stream used by the modeler for its standard error output. The default stream used by the modeler corresponds to the Python standard error output, retrieved with sys.stderr.

You can also use the shortcut member stderr

Returns:

The stream used by the modeler for its standard error output or null if the standard error output is disabled.

LSPModeler.set_stderr(stream)

Sets the stream used by the modeler for its standard error output. The default is to redirect the standard error output stream to the Python standard error output stream. If the given stream is null, the standard error output of the modeler will be disabled.

Parameters:

stream – stream to use for the standard error output or null to disable standard error output.