Create a Python function

Updated at:

This topic describes how to create a Python function.

Prerequisites

A business process has been created. For details, see Create and manage business processes.

Background information

In DataService Studio, you can create, test, and publish functions. You can then associate a published function with an API as a filter that processes the API's request parameters or response results. This supports complex API queries and lets you adapt to different business scenarios.

Limits

  • To use Python functions and filters, you must purchase DataWorks Professional Edition or later.

  • Python functions must run on a shared resource group for DataService Studio.

  • Python functions are being redesigned. You cannot currently create, clone, or publish new Python functions. If you need function capabilities, we recommend using Aviator functions. For details, see Create an Aviator function.

  • When you use functions as API filters, the pre-filter and post-filter must be of the same function type. Select Python functions for both. You cannot currently use different function types for the pre-filter and post-filter of the same API.

  • The following limits apply when a Python function is used as a filter:

    • The filter supports only Python 3.0 syntax.

    • The filter supports only import json,time,random,pickle,re,math.

    • The filter requires the function name and signature def handler(event,context):.

Function structure

# -*- 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 this function and rename its input parameters as needed.

Parameter 1 [context]: A string that contains the API execution context. It is currently empty and not in use.
Parameter 2 [event]: A string that contains the API request parameters or the output of the previous filter.
Note
  • The system converts API request parameters or the previous filter's output from a key-value map to JSON and passes that JSON to the event parameter. Values in the JSON value field must be strings.

  • The pre-filter outputs a key-value map. Because this output is used as input to the SQL statement, only a key-value map with a depth of 1 is supported.

Go to DataService Studio

Log on to the DataWorks console. In the target region, click Data Analysis and Service > DataService Studio in the left-side navigation pane. Select a workspace from the drop-down list and click Go to DataService Studio.

Create a function

  1. On the Service Development page, move the pointer over the image.png icon and click Create Function > Create Python Function.

    Alternatively, find the business process, right-click Function, and select Create > Create Python Function.

  2. In the Create Python Function dialog box, configure the parameters.

    Parameter

    Description

    Function Name

    The name of the custom function. It can contain up to 256 characters.

    Function Template

    Only the Python3 Standard v1 template is supported.

    Runtime Environment

    Only Python 3.0 is supported.

    Function Description

    A brief description of the function. It can contain up to 512 characters.

    Location

    The directory where the function is stored.

  3. Click Confirm.

  4. Configure the function on the function editing page.

    1. Enter the function code in the Edit Code area.

    2. In the Environment Configuration area, set Memory and Timeout.

  5. Click the Save icon in the toolbar.

Next steps