Welcome to package's documentation!¶
snippetchecker package¶
Submodules¶
snippetchecker.api module¶
snippetchecker
SnippetChecker API実行版
- class snippetchecker.api.LoopStructure(loop: LoopType, body: List[LoopType | NamedTuple])¶
ベースクラス:
NamedTuple
LoopType
ループの入れ子構造
- class snippetchecker.api.LoopType(value)¶
ベースクラス:
Enum
ループのタイプ
- NO_LOOP = 1¶
- CONSTANT_LOOP = 2¶
- GENERAL_LOOP = 3¶
- CONSTANT_COMPREHENSION = 4¶
- GENERAL_COMPREHENSION = 5¶
- class snippetchecker.api.SnippetChecker(allow_save_code=False, write_configure_file=True, uniq_id=None)¶
ベースクラス:
object
snippetchecker
SnippetChecker API実行版
- check_formal_assign(code: str, target_name: str, safe_function: List[str]) bool ¶
正常な代入文かどうかをチェックする
- パラメータ:
code (str) -- スニペットの文字列
target_name (str) -- 代入先の変数名
safe_function (List[str]) -- 呼び出しを許可する関数名
- 戻り値:
正常な代入文かどうか
- 戻り値の型:
bool
サンプル
>>> check_formal_assign("hoge = 1 + random.randint(1,100)", "hoge", ["random.randint"]) True
- check_formal_snippet(code: str, safe_function: List[str], safe_objects: List[str], safe_modules: List[str]) bool ¶
正常なスニペットどうかをチェックする
- パラメータ:
code (str) -- スニペットの文字列
safe_function (List[str]) -- 呼び出しを許可する関数名
safe_objects (List[str]) -- 使用を許可するオブジェクト名
safe_modules (List[str]) -- インポートを許可するモジュール名
- 戻り値:
正常な代入文かどうか
- 戻り値の型:
bool
サンプル
>>> check_formal_snippet('import random\nhoge = 1\nwhile True:\n hoge += random.randint(1,100)', ['random.randint'], [], ['random']) True
- clear_errors()¶
- determine_snippet_type(code: str) SnippetType ¶
スニペットのタイプを認識する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
スニペットのタイプ
- 戻り値の型:
サンプル
>>> determine_snippet_type("hoge = hogehoge + 1") == LINE_ASSIGN True
- enumerate_classes_define(code: str) List[str] ¶
スニペット内で定義されているクラスを列挙する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
定義されているクラスの一覧
- 戻り値の型:
List[str]
サンプル
>>> enumerate_classes_define("class hoge():\n hogehoge=1") == ['hoge'] True
- enumerate_difinitions_used(code: str) List[str] ¶
スニペット内での定義を列挙する
- パラメータ:
List[str] -- 定義されている名前の一覧
- 戻り値:
スニペットのタイプ
- 戻り値の型:
サンプル
>>> enumerate_difinitions_used("hoge = hogehoge + 1") == ['hoge'] True
- enumerate_functions_called(code: str) List[str] ¶
スニペット内で呼び出されている関数を列挙する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
呼び出されている関数の一覧
- 戻り値の型:
List[str]
サンプル
>>> enumerate_functions_called("hoge = random.randint(1,100) + 1") == ['random.randint'] True
- enumerate_functions_define(code: str) List[str] ¶
スニペット内で定義されている関数を列挙する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
定義されている関数の一覧
- 戻り値の型:
List[str]
サンプル
>>> enumerate_functions_define("def hoge():\n pass") == ['hoge'] True
- enumerate_packages_import(code: str) List[str] ¶
スニペット内でインポートされているパッケージを列挙する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
インポートされているパッケージの一覧
- 戻り値の型:
List[str]
サンプル
>>> enumerate_packages_import("import random") == ['random'] True
- enumerate_variables_assign(code: str) List[str] ¶
スニペット内で代入更新されている定義を列挙する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
代入されている名前の一覧
- 戻り値の型:
List[str]
サンプル
>>> enumerate_variables_assign("hoge = hogehoge + 1") == ['hoge'] True
- find_loop_in_snippet(code: str) List[LoopType | LoopStructure] ¶
スニペットに含まれるループの種類を判定する
- パラメータ:
code (str) -- スニペットの文字列
- 戻り値:
ループの入れ子構造
- 戻り値の型:
List[Union[LoopType,LoopStructure]]
サンプル
>>> find_loop_in_snippet("a=0\nfor _ in range(10):\n a+=1\nprint(a)") == [CONSTANT_LOOP] True >>> find_loop_in_snippet("while True:\n [b for b in range(20)]") [LoopStructure(loop=<LoopType.GENERAL_LOOP: 3>, body=[<LoopType.CONSTANT_COMPREHENSION: 4>])]
- print_errors()¶
snippetchecker.support module¶
- snippetchecker.support.make_cleaned_builtins(allow_global_functions: List[str], allow_import_modules: List[str]) object ¶
許可された組み込み関数とインポートのみ含んだ__builtins__を返す
- パラメータ:
allow_global_functions (List[str]) -- 呼び出しを許可する関数名
allow_import_modules (List[str]) -- インポートするモジュール名
- 戻り値:
新しい__builtins__
- 戻り値の型:
object
サンプル
execで実行するときにコードチェックと組み合わせるとある程度安全なサンドボックスになる
>>> global_builtins = make_cleaned_builtins(allow_global_functions=['int'], allow_import_modules=['random']) >>> local_variables = dict() >>> exec('test_val = int(random.random()*100)', >>> {'__builtins__':global_builtins}, >>> local_variables >>> ) >>> print(local_variables) {'test_val': 58} >>> exec('test_val = float(random.random()*100)', >>> {'__builtins__':global_builtins}, >>> local_variables >>> ) NameError: name 'float' is not defined
注釈
__builtins__だけでは安全な実行は出来ないので、事前のコードチェックを必ず行うこと 無限ループによるリソースの食い潰し等には対処できないので、その場合はSnippetRunnerを使うこと