Decorators

Benchmark

Log benchmarking times.

This decorator will log the time of execution (benchmark_time) to the app.log file. It can be helpful in troubleshooting performance issues with Apps.

1
2
3
4
5
import time

@Benchmark()
def my_method():
    time.sleep(1)

Debug

Debug function inputs.

This decorator will log the inputs to the function to assist in debugging an App.

1
2
3
@Debug()
def my_method(arg1, args2):
    print(arg1, arg2)

FailOnOutput

Fail App if return value (output) value conditions are met.

This decorator allows for the App to exit on conditions defined in the function parameters.

1
2
3
4
5
@FailOnOutput(
    arg='fail_on_false', values=['false'], msg='Operation returned a value of "false".'
)
def my_method(data):
    return data.lowercase()

IterateOnArg

Iterate on values stored in self.args namespace.

This decorator will iterate over all value of the supplied arg and return results. This feature is helpful when processing a single value (String) or array of values (StringArray). If the App was provided the arg self.args.colors with a value of ['blue', 'green', 'magenta'], then this decorator would call the function 3 times. Each call to the function would pass one value from the array. The return values are stored and returned all at once after the last value is processed.

1
2
3
4
# the args value of "colors" refers to ``self.args.colors``.
@iterateOnArgs(arg='colors')
def my_method(value):
    return value

OnException

Set exit message on failed execution.

This decorator will catch the generic “Exception” error, log the supplied error message, set the “exit_message”, and exit the App with an exit code of 1.

1
2
3
@OnException(msg='Failed to process JSON data.')
def my_method(json_data):
    json.dumps(json_data)

OnSuccess

Set exit message on successful execution.

This decorator will set the supplied msg as the App “exit_message”. Typically and App would only have 1 exit message so this decorator would typically be used in App that used “actions”.

1
2
3
@OnSuccess(msg='Successfully processed JSON data.')
def my_method(json_data):
    json.dumps(json_data)

Output

Store the method return value in self.<attribute>.

This decorator will write, append, or extend the methods return value to the App attribute provided in the attribute input. The attribute must first be defined in the __init__() method of the App before the decorator is used.

1
2
3
4
5
6
7
def __init__(self, _tcex):
    super(App, self).__init__(_tcex)
    self.output_strings = []  # Output decorator writes here.

@Output(attribute='output_strings')
def my_method(data):
    return data.lowercase()

ReadArg

Read value of App arg resolving any playbook variables.

This decorator will read the args from Redis, if it is a playbook variable, and pass the resolved value to the function.

1
2
3
@ReadArg(arg='color')
def my_method(color):
    print('color', color)

WriteOutput

Write the App output variables to Redis.

This decorator will take the functions return value and write the data to Redis using the key and variable_type. An optional hard coded value can be passed, which will override the return value. If multiple value are provided for the same output variable there is an option to overwrite the previous value.

This decorator is intended for very simple Apps. Using the write_output() method of the App template is the recommended way of writing output data.

1
2
3
@WriteOutput(key='color', variable_type='String')
def my_method(color):
    return color.lowercase()