Skip to main content

In-line Functions Documentation

Overview

The in-line functions feature offers a convenient way to perform basic Python operations within Swarm while entering data. This functionality allows users to embed Python expressions directly into their input fields, enclosed by {{}}. When the system processes the data, it evaluates the code within the braces and replaces it with the result.

For example, if you wanted to perform a simple mathematical operation like multiplying two numbers, you could enter:

{{ 10 * 5 }}

When processed, the system would replace it with:

50

The in-line functions support a range of basic Python operations and built-in functions, providing flexibility to perform calculations, format data, and much more during the data entry process.

How to Use In-line Functions

To use in-line functions, you simply need to write a valid Python expression inside double curly braces. For instance:

{{ expression }}

Examples:

Adding two numbers: {{ 5 + 10 }}

Converting a date to a specific format: {{ reformat_date('2023-01-01', '%d-%m-%Y') }}

Generating a one-time password (OTP): {{ generate_otp('your_seed') }}

The system will replace these placeholders with the computed results.

Supported Functions

Below are the key functions supported by the system, along with examples of how to use them in-line.

1. Mathematical Operations

You can perform basic arithmetic such as addition, subtraction, multiplication, and division.

Multiplication

Example: {{ 10 * 5 }}

Result: 50
Addition

Example: {{ 5 + 15 }}

Result: 20
Subtraction

Example: {{ 100 - 60 }}

Result: 40

2. Date Formatting

You can use the reformat_date function to format dates in a specific way.

Basic Date Formatting

Example: {{ reformat_date('2023-01-01', '%d-%m-%Y') }}

Result: 01-01-2023

Custom Date Formats

If the input date has a non-standard format, you can provide a custom input format as well.

Example:

{{ reformat_date('2023 . 01 . 01T12:34:56', '%d-%m-%Y %H:%M:%S', '%Y . %m . %dT%H:%M:%S') }}

Result: 01-01-2023 12:34:56

3. Number Formatting

The format_number_as_string function allows you to format numbers with specific patterns, such as adding commas or decimal places.

Adding Commas to Large Numbers

Example: {{ format_number_as_string(1000000, '{:,}') }}

Result: 1,000,000

Formatting a Number with Decimal Places

Example: {{ format_number_as_string(1234.567, '{:,.2f}') }}

Result: 1,234.57

4. Leading Zeros

The add_leading_zero_single_digit function adds a leading zero to a number if it is a single digit.

Padding Single-Digit Numbers

Example: {{ add_leading_zero_single_digit(7) }}

Result: 07

5. OTP Generation

The generate_otp function generates a one-time password (OTP) based on a given seed. This can be useful for security purposes.

Generating an OTP

Example: {{ generate_otp('JBSWY3DPEHPK3PXP') }}

Result: An OTP, such as 123456

6. Data Lookup

You can use the lookup function to fetch specific values from predefined datasets or mappings within the system. This can simplify data entry when specific codes or values need to be referenced.

Lookup Example

Example: {{ lookup('currency', 'USD') }}

Result: The system would return the corresponding value for 'USD' from the currency dataset.