13.5.7.6 ユーザー定義Python関数のエクスポート

ユーザー定義Python関数をOML4Pyスクリプト・リポジトリから宛先ファイルにエクスポートするには、oml.export_script関数を使用します。

この関数の構文は次のとおりです。
oml.export_script(file_name, name=None, regex_match=False, sctype='user', overwrite=False)

関数のパラメータ:

  • file_name (型: string): エクスポートされたスクリプトを含む宛先ファイルのファイル名またはパスのいずれかを指定します。どちらも.jsonまたは.pyで終わる必要があります。.json.pyの2つの形式のみがサポートされています。ファイル・タイプは、接尾辞によって判断されます。たとえば、宛先ファイル・パスに関する次の2つのケースを考えてみます:
    • /dir1/file1.json: これは有効なパスで、出力はfile1.jsonとして/dir1/file1.jsonに保存されます。
    • /dir1: これは無効なパスであり、エラーになります。

    .py形式の場合も同様です。

  • name (型: string、stringのリストまたはNone(デフォルト)): スクリプト名または正規表現を指定して、スクリプト名と照合します。nameがNoneまたは空のリストの場合、指定された引数sctypeに基づいてすべてのスクリプトがエクスポートされます。
  • regex_match (型: boolまたはFalse (デフォルト)): 引数nameが正規表現かどうかを示します。複数のユーザー定義関数が正規表現に一致する場合、1つのファイルのみが生成されます。一致したすべてのユーザー定義関数は、この単一のファイルに含められます。このファイルは、file_nameパラメータに従って名前が付けられます。
  • sctype (型: string): エクスポートするユーザー定義Python関数のタイプを指定します。次のいずれかの値を指定できます。
    • user (デフォルト): 現在のセッション・ユーザーによって作成されたユーザー定義Python関数をエクスポートします。
    • grant: 現在のユーザーが読取りおよび実行の権限を他のユーザーに付与したユーザー定義Python関数をエクスポートします。
    • granted: 他のユーザーが読取りおよび実行の権限を現在のユーザーに付与したユーザー定義Python関数をエクスポートします。
    • global: 現行ユーザーによって作成されたすべてのグローバル・ユーザー定義Python関数をエクスポートします。
    • all: 現在のユーザーが読取りアクセス権を持ち使用できるすべてのユーザー定義Python関数をエクスポートします。
  • overwrite (型: boolまたはFalse (デフォルト)): Trueに設定すると、file_nameがすでに存在する場合は上書きされます。

例13-15 oml.export_script関数の使用

この例では、oml.export_script関数を使用して、ユーザー定義Python関数を.json.pyの両方のファイル形式でエクスポートします。たとえば、 oml.export_script(file_name="script_user.json", sctype="user")の場合、引数名がNoneでsctypeが"user"であるため、この関数は現在のセッション・ユーザーが作成したすべてのユーザー定義関数に一致します。

ユーザー定義Python関数の作成については、例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="./"))

この例のリスト

>>> 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