13.5.7.6 Export a User-Defined Python Function

Use the oml.export_script function to export a user-defined Python functions from the OML4Py script repository to a destination file.

The syntax of the function is the following:
oml.export_script(file_name, name=None, regex_match=False, sctype='user', overwrite=False)

Function Parameter:

  • file_name (type: string): Specifies either the file name or the path of the destination file containing the exported scripts, and both must end with either .json or .py. Only two formats are supported: .json and .py. The file type is determined by the suffix. For example, consider the following two cases regarding the destination file path:
    • /dir1/file1.json: This is a valid path , and the output will be saved as file1.json at /dir1/file1.json.
    • /dir1: This is an invalid path and will result in an error.

    Similarly, for .py format.

  • name (type: string, list of string or None (default)): Specifies the script name or a regular expression to match script names. If name is None or empty list, it exports all the scripts based on the specified argument sctype.
  • regex_match (type: bool or False (default)): Indicates whether the argument name is a regular expression or not. If multiple user-defined functions match the regex, only one file will be generated. All matched user-defined functions will be included in this single file, which is named according to the file_name parameter.
  • sctype (type: string): Specifies the type of user-defined Python function to export. It can take one of the following values:
    • user (default): Exports user-defined Python function created by the current session user.
    • grant: Exports user-defined Python functions to which the current user has granted read and execute privileges to the other users.
    • granted: Exports user-defined Python functions to which other users have granted read and execute privileges to the current user.
    • global: Exports all of the global user-defined Python functions created by the current user.
    • all: Exports all user-defined Python functions that are available to the current user, for which the current user has read access.
  • overwrite (type: bool or False (default)): If set to True, it will overwrite the file_name if it already exists.

Example 13-15 Using the oml.export_script Function

This example uses the oml.export_script function to export the user-defined Python function in both .json and .py file formats. In the example oml.export_script(file_name="script_user.json", sctype="user"), since the argument name is None and sctype is "user", the function will match all user-defined functions created by the current session user.

For the creation of the user-defined Python functions, see Example 13-11.

import oml

#List the user-defined Python functions in the script  
# repository available to the current user only.
oml.script.dir(name="LM", regex_match=True, sctype="all")[['owner', 'name', 'script', 'description']]

#Export user-owned and global scripts to the files named 
#script_user.json and script_global.py respectively. 
oml.export_script(file_name="script_user.json", sctype="user")
oml.export_script(file_name="script_global.py", sctype="global")

#Drop the scripts named MYLM and GLBLM.
oml.script.drop("MYLM")
oml.script.drop("GLBLM", is_global=True) 

# List all of the user-defined Python functions available to the current user.
oml.script.dir(sctype="all")

#Verifies whether the specified set of files is present in the list of files located in a directory.
import os
{"script_user.json", "script_global.py"}.issubset(os.listdir(path="./"))

Listing for This Example

>>> oml.script.create("MYLM", func=build_lm1, description="my lm model build")
>>> oml.script.create("GLBLM", func=build_lm2, 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.export_script(file_name="script_user.json", sctype="user")
>>> oml.export_script(file_name="script_global.py", sctype="global")
>>> oml.script.drop("MYLM")
>>> oml.script.drop("GLBLM", is_global=True)
>>> oml.script.dir(sctype="all")
Empty DataFrame
Columns: [owner, name, script, description, date]
Index: []
>>> import os
>>> {"script_user.json", "script_global.py"}.issubset(os.listdir(path="./"))
True