19.13 Use Variables

The Variable class creates and manages variables in Data Transforms. A variable has a name, a VariableTypes type, a default value, and a project.

The constructor is:

Variable(name, variable_type, default_value, project_name)

The supported types are DATE, SHORT_TEXT, LONG_TEXT, and NUMERIC. workbench.save_variable(variable) creates a new variable or updates an existing variable with the same name in the project.

Refresh options

Variables are refresh-enabled by default. There are two ways to configure the refresh option:

  • For a static variable, set is_refresh_set=False before saving. The default value is used and no refresh query is required.
  • For a database-backed variable, call refresh with a connection, schema, and SQL query. This keeps refresh enabled and configures the query used to obtain the variable value.

Static variable

Use is_refresh_set=False for variables whose value is supplied by the default value rather than a database query.

from datatransforms.variables import Variable, VariableTypes

# Assume workbench is already connected.
variable = Variable(
    name="numeric_variable",
    variable_type=VariableTypes.NUMERIC,
    default_value=80,
    project_name="<your-project>",
)
variable.is_refresh_set = False

workbench.save_variable(variable)

Refreshing from a database query

Use refresh(connection_name, schema_name, sql_query) to configure a refreshable variable. The connection must exist in Data Transforms. When the variable is saved, the SDK resolves the connection and schema and validates the SQL query.

from datatransforms.variables import Variable, VariableTypes

# Assume workbench is already connected and the connection/schema exist.
variable = Variable(
    name="THRESHOLD_LIMIT",
    variable_type=VariableTypes.SHORT_TEXT,
    default_value="SimpleValue5678",
    project_name="<your-project>",
)
variable.refresh(
    connection_name="<sql-connection>",
    schema_name="<schema>",
    sql_query="SELECT tlimit FROM some_configtable",
)

workbench.save_variable(variable)

Additional variable APIs

add_to_project(project_name=..., project_code=...) associates a variable with a project name or project code after construction. keep_history(...) sets the value persistence option; the currently supported value is "LATEST_VALUE".

variable.add_to_project(project_name="<another-project>")
variable.keep_history("LATEST_VALUE")