All Products
Search
Document Center

:Create a Python function

Last Updated:Nov 14, 2024

This topic describes how to create a Python function.

Prerequisites

A business process is created. For more information, see Create and manage business processes.

Background information

On the DataService Studio page, you can create, test, and publish a function. Then, you can associate the function with an API, and use the function as a filter to process the request parameters or responses of the API to perform complex queries based on your business requirements.

Limits

  • You must activate DataWorks Professional Edition or a more advanced edition before you use Python functions.

  • Python functions must be executed on the shared resource group for DataService Studio.

  • The feature of using a Python function as a filter is being optimized. You cannot create, clone, or publish new Python functions. If you need to use this feature, we recommend that you use Aviator functions. For more information, see Create an Aviator function.

  • If you use one or more functions as a filter to process the request parameters of an API and one or more functions as a filter to process the results returned by the API, you must use the same type of function as the filters. You cannot use different types of functions as the filters for an API.

  • If you want to use a Python function as a filter, take note of the following limits:

    • Only Python 3.0 functions can be used as filters.

    • Python functions support the import of only the following modules: json, time, random, pickle, re, and math.

    • The name of a function used as a filter must be def handler(event,context):.

Function template

# -*- coding: utf-8 -*-

# event (str) : in filter it is the API result, in other cases, it is your param
# context : some environment information, temporarily useless
# import module limit: json,time,random,pickle,re,math
# do not modify function name
import json
def handler(event,context):
    # load str to json object
    obj = json.loads(event)
    # add your code here
    # end add
    return obj

You can modify the function template to write your own function. You can change the names of the input parameters based on your business requirements.

Parameter 1 [context]: the context of calling the API. The value is of the STRING type. This parameter is not in use and is left empty.   
Parameter 2 [event]: the request parameters of the API or the result data returned by the previous function. The value is of the STRING type.

Note
  • The system converts the request parameters of an API or the result data returned by the previous filter from a key-value map to a JSON object, and passes the JSON object to the event parameter. The values in the JSON object must be of the STRING type.

  • The processing result of a filter used to process the request parameters of an API is a key-value map. The key-value map is used as the input parameter of an SQL statement. Therefore, the key-value map cannot contain nested key-value pairs.

Create a function

  1. In the Service Development pane, move the pointer over the image.png icon and choose Create Function > Create Python Function.

    Alternatively, you can find the desired business process, right-click Function, and then choose Create Function > Create Python Function.

  2. In the Create Python Function dialog box, configure the parameters based on your business requirements.

    PYthon

    Parameter

    Description

    Function Name

    The name of the function. The name can be up to 256 characters in length.

    Function Template

    The template that is used to create the function. Set the value to Python3 Standard v1.

    Running Environment

    The runtime environment of the function. Set the value to Python 3.0.

    Function Description

    The description of the function. The description can be up to 512 characters in length.

    Destination Folder

    The folder for storing the function.

  3. Click Determine.

  4. Configure the function on its configuration tab.

    1. In the Edit Code section, enter the function code.

    2. In the Environment Configuration section, configure the Memory and Function Timeout parameters.

  5. Click the 保存 icon in the toolbar.