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
In DataService Studio, you can create, test, and publish functions. You can then associate a function with an API and use it as a filter to process the API's request parameters or response data. This allows you to perform complex queries that meet 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.
Python functions are being refactored. 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.
When using functions as API filters, the functions for both pre-processing requests and post-processing responses must be of the same type. 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 function used as a filter must have the following signature:
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 : contains environment information. Currently unused.
# 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 objYou can modify the function template to write your own function. You can change the names of the input parameters based on your business requirements.
context (string): The API's execution context. This parameter is currently unused and left empty.
event (string): The API request parameters or the result data from the previous function.
The system converts the API request parameters or the previous filter's output from a map to a JSON string, which is then passed to the event parameter. All values in the JSON object must be strings.
The output of a pre-processing filter (which processes request parameters) must be a map. The key-value map is used as the input parameter of an SQL statement. Therefore, the map cannot contain nested key-value pairs.
Create a function
In the Service Development pane, move the pointer over the
icon and choose . Alternatively, you can find the desired business process, right-click Function, and then choose .
In the Create Python Function dialog box, configure the parameters based on your business requirements.

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 where the function is stored.
Click OK.
Configure the function on its configuration tab.
In the Edit Code section, enter the function code.
In the Environment Configuration section, configure the Memory and Function Timeout parameters.
Click the Save
icon in the toolbar.
What to do next
After you create the function, you can test and publish the function. For more information, see Test, publish, and use a function and Publish a function.
After you publish the function, you can associate the function with an API and use the function as a prefilter or a post-filter for the API. This way, the function can process the request parameters or responses of the API. For more information, see Use a function as a filter for an API.