Python functions are currently in a beta state and may not be available on all enrollments.
Below is the full list of currently supported functions API types, their corresponding Python types, and whether that type can be declared using its corresponding Python type instead of the functions API type:
Functions API type | Can declare as Python type? | Corresponding Python type |
---|---|---|
Array | Yes | list |
Binary | Yes | bytes |
Boolean | Yes | bool |
Byte | No | int* |
Date | Yes | datetime.date |
Decimal | Yes | decimal.Decimal |
Double | No | float* |
Float | Yes | float |
Integer | Yes | int |
Long | No | int* |
Map | Yes | dict |
Set | Yes | set |
Short | No | int* |
String | Yes | str |
Timestamp | Yes | datetime.datetime |
Structs / custom types | Yes | dataclass.dataclass (Example) |
Although both Integer
and Long
correspond to the Python type int
, any fields marked as int
directly in your function signature will be registered with type Integer
. Therefore, we instead recommend using either the Integer
or Long
types from the API to register numerical data types. Similar guidelines apply to Float
and Double
; if the Python type float
is directly in your function signature, it will be registered as Float
by default.
Another example function with inputs is shown below:
Copied!1 2 3 4 5 6
from functions.api import function, Long, String, Timestamp @function def get_end_day_of_week(start_time: Timestamp, elapsed_millis: Long) -> String: # function logic here pass
As shown in the above types table, this function could also be declared using only built-in Python types:
Copied!1 2 3 4 5 6 7
from functions.api import function from datetime import datetime @function def get_end_day_of_week(start_time: datetime, elapsed_millis: int) -> str: # function logic here pass
Additionally, you could use a combination of built-in and API types:
Copied!1 2 3 4 5 6 7
from functions.api import function, Long, String from datetime import datetime @function def get_end_day_of_week(start_time: datetime, elapsed_millis: Long) -> String: # function logic here pass
Custom Python classes composed of other supported types can also be used in Function signatures. The API package does not have an explicit Custom
type; instead, custom types are declared as user-defined Python classes.
To be a valid custom type, the class must adhere to the following:
__init__
method must accept only named arguments with the same names and type annotations as the fields.The dataclasses.dataclass
↗ decorator can be used to automatically generate an __init__
method that conforms with these requirements.
dataclasses.dataclass
Here is an example using the dataclasses.dataclass
:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from dataclasses import dataclass from functions.api import Float, Integer, String, function @dataclass class InventoryItem: """Class for keeping track of an item in inventory.""" name: String unit_price: Float quantity_on_hand: Integer = 0 def total_cost(self) -> Float: return self.unit_price * self.quantity_on_hand @function def custom_type_with_init_from_decorator(inventory_item: InventoryItem) -> Float: return inventory_item.total_cost()
dataclass.dataclass
)Here is an example without dataclass.dataclass
using API types:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from functions.api import function, String, Timestamp class Event: timestamp: Timestamp message: String def __init__( self, timestamp: Timestamp, message: String ): self.timestamp = timestamp self.message = message @function def get_event_message(event: Event) -> String: return event.message
Learn how to set up a Python functions repository, or learn more about using Python functions in Pipeline Builder or Workshop.