Package with functions that call Python


This package contains functions that call Python.

Extends from Modelica.Icons.BasesPackage (Icon for packages containing base classes).

Package Content

Name Description exchange Function that communicates with Python
Buildings.Utilities.IO.Python_3_8.Functions.Examples Examples Collection of models that illustrate model use and test models
Buildings.Utilities.IO.Python_3_8.Functions.BaseClasses BaseClasses Package with functions that call Python

Function that communicates with Python


This function is a wrapper for It adds the directory modelica://Buildings/Resources/Python-Sources to the environment variable PYTHONPATH prior to calling the function that exchanges data with Python. After the function call, the PYTHONPATH is set back to what it used to be when entering this function. See Buildings.Utilities.IO.Python_3_8.UsersGuide for instructions, and Buildings.Utilities.IO.Python_3_8.Functions.Examples for examples.

Extends from Modelica.Icons.Function (Icon for functions).


StringmoduleName Name of the python module that contains the function
StringfunctionNamemoduleNameName of the python function
PythonObjectpytObj Memory that holds the Python object
BooleanpassPythonObject Set to true if the Python function returns and receives an object, see User's Guide
RealdblWri[max(1, nDblWri)] Double values to write
IntegerintWri[max(1, nIntWri)] Integer values to write
StringstrWri[max(1, nStrWri)] String values to write
IntegernDblWri Number of double values to write
IntegernDblRea Number of double values to read
IntegernIntWri Number of integer values to write
IntegernIntRea Number of integer values to read
IntegernStrWri Number of strings to write


RealdblRea[max(1, nDblRea)]Double values returned by Python
IntegerintRea[max(1, nIntRea)]Integer values returned by Python

Modelica definition

impure function exchange "Function that communicates with Python" extends Modelica.Icons.Function; input String moduleName "Name of the python module that contains the function"; input String functionName=moduleName "Name of the python function"; input BaseClasses.PythonObject pytObj "Memory that holds the Python object"; input Boolean passPythonObject "Set to true if the Python function returns and receives an object, see User's Guide"; input Real dblWri[max(1, nDblWri)] "Double values to write"; input Integer intWri[max(1, nIntWri)] "Integer values to write"; input String strWri[max(1, nStrWri)] "String values to write"; input Integer nDblWri(min=0) "Number of double values to write"; input Integer nDblRea(min=0) "Number of double values to read"; input Integer nIntWri(min=0) "Number of integer values to write"; input Integer nIntRea(min=0) "Number of integer values to read"; input Integer nStrWri(min=0) "Number of strings to write"; // input Integer nStrRea(min=0) "Number of strings to read"; // input Integer strLenRea(min=0) // "Maximum length of each string that is read. If exceeded, the simulation stops with an error"; output Real dblRea[max(1, nDblRea)] "Double values returned by Python"; output Integer intRea[max(1, nIntRea)] "Integer values returned by Python"; protected String pytPatOld "Old value of PYTHONPATH environment variable"; String pytPatBuildings "PYTHONPATH of Buildings library"; String pytPat "Value of PYTHONPATH environment variable"; Boolean havePytPat "true if PYTHONPATH is already set by the user"; package P constant String filNam = "modelica://Buildings/legal.html" "Name to a file of the Buildings library"; constant String buiLibFil = Modelica.Utilities.Files.loadResource(uri=filNam) "Absolute path to filNam"; end P; String searchString = "legal.html" "String to be replaced"; algorithm // Get the directory to Buildings/Resources/Python-Sources pytPatBuildings := Modelica.Utilities.Strings.replace( string=P.buiLibFil, searchString=searchString, replaceString="Resources/Python-Sources"); // Update the PYTHONPATH variable (pytPatOld, havePytPat) :=Modelica.Utilities.System.getEnvironmentVariable("PYTHONPATH"); if havePytPat then if Modelica.Utilities.Strings.find(pytPatOld, pytPatBuildings) == 0 then // The new python path is not yet in the environment variable, add it. pytPat:=pytPatOld + ":" + pytPatBuildings; else // The new python path is already in the variable. pytPat:=pytPatOld; end if; else pytPat := pytPatBuildings; end if; // Call the exchange function (dblRea, intRea) moduleName=moduleName, functionName=functionName, pytObj=pytObj, passPythonObject=passPythonObject, pythonPath=pytPat, dblWri=dblWri, intWri=intWri, strWri=strWri, nDblWri=nDblWri, nDblRea=nDblRea, nIntWri=nIntWri, nIntRea=nIntRea, nStrWri=nStrWri); end exchange;