13.5.7.7 Import a User-Defined Python Function
Use the oml.import_script
function to import a user-defined
Python functions to the OML4Py script repository from a specified file.
The syntax of the function is the
following:
oml.import_script(file_name, is_global=False, overwrite=False)
Function Parameter:
- file_name (type: string): Specifies the file containing the scripts
to import. Only two formats are supported:
.json
and.py
. The file type is determined by the suffix. - is_global (type: bool or False (default)): Indicates whether the
user-defined Python function is globally accessible after imported. By default,
it is set to
False
, meaning it is only accessible to you. - overwrite (type: bool or False (default)): If set to
True
, overwrite the existing script if it already exists.
Example 13-16 Using the oml.import_script Function
This example uses the oml.import_script
function to
import the user-defined Python function in both .json
and
.py
file formats from a specified file. For the creation of the
user-defined Python functions, see Example 13-11.
import oml
# Import the file named script_user.json as a private user-defined Python function.
oml.import_script(file_name="script_user.json")
oml.script.dir()[['name', 'script', 'description']]
# Import the file name script_global.py as a global user-defined Python function.
oml.import_script(file_name="script_global.py", is_global=True)
oml.script.dir(name="LM", regex_match=True, sctype="all")[['owner', 'name', 'script','description']]
# Drop the private user-defined Python function.
oml.script.drop("MYLM")
# Drop the global user-defined Python function.
oml.script.drop("GLBLM", is_global=True)
Listing for This Example
oml.import_script(file_name="script_user.json")
>>> oml.script.dir()[['name', 'script', 'description']]
name script description
0 MYLM def build_lm1(dat):\n from sklearn import lin... my lm model build
>>> oml.import_script(file_name="script_global.py", is_global=True)
>>> oml.script.dir(name="LM", regex_match=True, sctype="all")[['owner', 'name', 'script', 'description']]
owner name script \
0 PYQSYS GLBLM def build_lm2(dat):\n from sklearn import lin...
1 PYQUSER MYLM def build_lm1(dat):\n from sklearn import lin...
<BLANKLINE>
description
0 None
1 my lm model build
>>> oml.script.drop("MYLM")
>>> oml.script.drop("GLBLM", is_global=True)