Script Function

The Script Function DocType in TechMaju allows developers to define and execute reusable Python functions that can be invoked dynamically across the platform. This feature enables modular, scalable, and organized script execution.

Understanding the Script Function DocType

Purpose: The Script Function DocType is designed to allow developers to create modular and reusable Python functions that can be executed dynamically in other Python script sections.

Function:

  • Define reusable Python functions for repetitive logic that is used in multiple areas.
  • Execute Python code dynamically using mantera.exec_script.

Role Required

  • TM Core Developer: Can create, write, and delete Script Functions.

Script Function Fields

Field

Description

Display Name

Free text field that determines the script name.

Script Name

Generated based on the naming convention: prefix of global/app scope followed by the Display Name. This uniquely identifies the function, which is passed into mantera.exec_script.

Global

Checkbox to differentiate between global and application scope. If checked, the function is defined as being used globally.

Application

Identifies the scope of this script. Note that currently, cross-scope hard restrictions are not enforced.

Script

Python code that will be executed when the function is invoked.

Using Script Functions

To execute a Script Function, use the mantera.exec_script method.

Syntax

mantera.exec_script("scope.script_function_name", *args, **kwargs)

Here, args and kwargs are optional and can be passed to the script for dynamic execution.

Examples

1. Calling a Script Function Without Arguments

Executing a function without passing any arguments.

mantera.exec_script("scope.script_function_name")

2. Calling a Script Function With Positional Arguments

Passing positional arguments allows the script function to access the values using index positions.

mantera.exec_script("scope.script_function_name", 1, "two")

3. Calling a Script Function With Keyword Arguments

Passing keyword arguments allows the script function to access values using named keys.

mantera.exec_script("scope.script_function_name", flag_zero="Alpha", flag_one="Beta")

4. Modifying an Object to Return Value

Script section does not allow for return; instead, modify a passed-in object for value retrieval post-execution.

result_obj = {"key": "initial_value"}
mantera.exec_script("scope.script_function_name", result_obj=result_obj)
frappe.log_debug(result_obj["key"])  # Output: "new_value"

Example Script Function Handling Arguments and Modifying an Object for returning new value

# Args example
first_param = args[0] if len(args) > 0 else None
second_param = args[1] if len(args) > 1 else None

frappe.log_debug("first_param", first_param)
frappe.log_debug("second_param", second_param)

# Kwargs example
frappe.log_debug("flag_zero", kwargs.get("flag_zero"))
frappe.log_debug("flag_one", kwargs.get("flag_one"))

# Return new value with object example
if "result_obj" in kwargs:
    result_obj["key"] = "new_value"

Note: Both args and kwargs can be used simultaneously in a Script Function, but it would be better practice to stick to one or the other.

Procedure

  1. Navigate to Workspace: Develop > select Script Function.
  2. Click on '+ Add Script Function' to create a new function.
  3. Enter the Display Name. The Script Name will be auto-generated.
  4. Select the appropriate scope using field Global or Application.
  5. Enter the Python code in the Script field.
  6. Save the Script Function.
  7. Use mantera.exec_script function to call this function in other Python script sections.

Best Practices

  • Ensure function names are unique and meaningful.
  • Use arguments and/or keyword arguments to make functions reusable.
  • Test functions in a controlled environment before deploying them to production.
  • Follow Python best practices to maintain clean and efficient code.
Discard
Save

On this page

Review Changes ← Back to Content
Message Status Space Raised By Last update on