Skip to main content

Darwin Tools: Python Code

S
Written by Support Team

This new tool allows you to run your own custom code. This functionality is useful for performing calculations, generating values, or interacting with queries and components. This step executes your Python code in an isolated environment and passes the results to the following stages of the workflow. If you prefer to work with JavaScript, use the available JavaScript code tool.


How the Tool Works

Step 1: Add the Tool to the Workflow

Open the Automation Builder, click the + button to add an action, and select the Python tool.

Step 2: Configure Input Data

In the Input Data section, you define the variables that you will receive from previous steps and that you will use within your code.

For each variable you must specify:

  • Name: the identifier you will use in the script.

  • Data type: string, number, boolean, etc.

  • Value: select the data from a previous step that will be assigned to this variable.

You can add as many variables as you need using the Add field button. You can also delete any that you do not use.

Next, configure Output Data. Here, you manually specify the name and type of the variables you wish to obtain as a result. In the following steps of your automation, you will be able to use the fields you defined in this section.

Configuring output data is mandatory, even if you did not create any new variables in the code.

Step 3: Write the Code and Configure the Output

In the code editor, write your Python script using the variables declared in the input data. You can create new variables or modify existing ones.

In the Output Data section, you define the variables that you will expose as a result. It is mandatory to configure this section, even if you do not create new variables in the code. For each output variable, you specify a name and a data type.

When fields are defined in Output Data, your code must include an assignment to the Outputdata variable with a dictionary whose keys exactly match the declared names. For example:


python

Outputdata = {"resultado": valor, "otro_campo": otro_valor}

If this assignment is not present, the step will fail.

After that, you can write any Python code using the variables you have declared or create new ones from them.

The code runs in an isolated environment. To ensure safe execution, we have restricted external requests and network operations. For example, libraries used for HTTP requests cannot be used.

Once the code is configured, open the next step of the automation. In the fields where you need to insert the output values, select the variables defined in Output Data from the list of available fields. Save the changes and start the automation.

Limitations and Security

  • Python version: 3.11

  • Environment time zone: UTC

  • Memory limit: 512 MB

  • Maximum execution time: 10 seconds per step

  • Isolated environment: the code runs in a sandbox with no network access. HTTP requests and network operations are not allowed (for example, the requests, urllib, and socket libraries).

  • Available modules: only modules from an approved list can be imported. These include: string, re, math, cmath, statistics, random, datetime, calendar, collections, collections.abc, functools, itertools, operator, heapq, bisect, array, copy, json, numpy, csv, base64, binascii, hashlib, hmac, pprint, textwrap, uuid, typing, dataclasses, traceback, decimal, fractions.

Errors and Diagnostics

If the step fails:

  • Check that the output data is correctly configured and that the output variable names match the names you assigned in your code.

  • Verify the input data: make sure that the structure or number of lines is sufficient. Many errors occur after splitting when the input string does not contain the expected lines.

  • Ensure that you are not using restricted modules or making network requests.

Usage Examples

Calculate the Difference in Minutes Between Two Dates

Returns the duration in minutes from two timestamps, useful for calculating processing times, calls, or meetings.

python

from datetime import datetime date_start = datetime.fromisoformat(date_start)  # or another format in which the string arrives date_end   = datetime.fromisoformat(date_end) dif_minutes = (date_end - date_start).total_seconds() / 60
phytoncode7.png

  • Split Date and Time and Remove Seconds

    Converts a full datetime into two independent fields (date and time) and discards the seconds.

python

from datetime import datetime date_time_str = date_time.strip()  # input is ALWAYS a string if date_time_str.isdigit():     dt = datetime.fromtimestamp(int(date_time_str) / 1000) else:     try:         dt = datetime.fromisoformat(date_time_str)     except ValueError:         dt = datetime.strptime(date_time_str, "%Y-%m-%d %H:%M:%S") def date_edit(arr):     return [(f"0{el}" if el < 10 else str(el)) for el in arr] date_str = ".".join(date_edit([dt.day, dt.month, dt.year])) time_str = ":".join(date_edit([dt.hour, dt.minute]))
phytoncode10.png
  • Clean HTML Tags from Text

    Removes HTML tags, line breaks, and multiple spaces from the body of an email or notification.

python

import re regexp = re.compile(r'<(\/?[^>]+)>|(\n)|(\s{4,})', flags=re.MULTILINE) clean_text = regexp.sub('', html).strip() Outputdata = {"clean_text": clean_text}
phytoncode8.png
Did this answer your question?