string_transform.py


Overview

The string_transform.py file is part of the InfiniFlow project and provides functionality for transforming strings within a component-based framework. It defines a component named StringTransform that supports two primary operations on strings:

This component is designed to be configurable via parameters and integrates with the larger InfiniFlow system to process string data flows. It leverages templating (via Jinja2) and regular expressions to perform flexible string transformations.


Classes

1. StringTransformParam

Defines the parameters for the StringTransform component.

Description:

StringTransformParam inherits from ComponentParamBase and encapsulates configuration parameters for the string transformation operations.

Attributes:

Attribute

Type

Description

Default

method

str

The transformation method, either "split" or "merge"

"split"

script

str

A script or template used when merging strings

""

split_ref

str

Reference variable name for the string to split

""

delimiters

List[str]

List of delimiter strings used to split or join strings

[","]

outputs

dict

Defines output structure; contains a "result" key with value and type

{"result": {"value": "", "type": "string"}}

Methods:

Usage Example:

param = StringTransformParam()
param.method = "merge"
param.script = "Hello {{name}}, welcome!"
param.delimiters = [" "]
param.check()  # Validates the parameters

2. StringTransform

Provides the core string transformation functionality.

Description:

StringTransform is an abstract base class (inherits from Message and ABC) representing a component that performs string transformations according to the defined StringTransformParam. It supports two main operations: splitting strings into lists and merging multiple strings into one using a script/template.

Attributes:

Attribute

Description

component_name

Class-level identifier "StringTransform"

_param

Instance of StringTransformParam with configuration parameters

_canvas

Context or environment used to get variable values (inherited or passed)

Methods:


Important Implementation Details


Interactions with Other Parts of the System


Usage Example

# Example for splitting a string
param = StringTransformParam()
param.method = "split"
param.split_ref = "input_string"
param.delimiters = [",", ";"]

transform = StringTransform()
transform._param = param
# Assume _canvas is properly set with variables
transform._invoke(line="apple,banana;cherry")
result = transform.get_output("result")
# result -> ['apple', 'banana', 'cherry']

# Example for merging strings
param.method = "merge"
param.script = "Hello {{name}}, your order {{order_id}} is ready."
param.delimiters = [" "]

transform._param = param
transform._invoke(name="Alice", order_id="12345")
result = transform.get_output("result")
# result -> "Hello Alice, your order 12345 is ready."

Mermaid Diagram - Flowchart of Main Functions

flowchart TD
    A[get_input_form()] --> B[_invoke(kwargs)]
    B --> |method == "split"| C[_split(line)]
    B --> |method == "merge"| D[_merge(kwargs)]
    C --> E[set_output("result", list_of_substrings)]
    D --> F[Render Jinja2 template or regex substitute]
    F --> G[set_output("result", merged_string)]

Summary

string_transform.py defines a configurable string transformation component for InfiniFlow capable of splitting strings into lists or merging multiple strings based on templates. The design supports flexible inputs, environment-based execution timeouts, and integration with the system's variable management, making it a versatile utility in data processing workflows.