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.

1import time
2
3@Benchmark()
4def my_method():
5    time.sleep(1)

Debug

Debug function inputs.

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

1@Debug()
2def my_method(arg1, args2):
3    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@FailOnOutput(
2    fail_on=['false'], fail_msg='Operation returned a value of "false".'
3)
4def my_method(data):
5    return data.lowercase()
param fail_enabled:

Accepts a boolean or string value. If a boolean value is provided that value will control enabling/disabling this feature. A string value should reference an item in the args namespace which resolves to a boolean. The value of this boolean will control enabling/disabling this feature.

type fail_enabled:

boolean|str, kwargs

param fail_msg:

The message to log when raising RuntimeError.

type fail_msg:

str, kwargs

param fail_msg_property:

The App property containing the dynamic exit message.

type fail_msg_property:

str, kwargs

param fail_on:

Fail if return value from App method is in the list.

type fail_on:

list, kwargs

param write_output:

Defaults to True. If true, will call App.write_outputs() before failing on matched fail_on value.

type write_output:

bool, kwargs

IterateOnArg

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@OnException(exit_msg='Failed to process JSON data.')
2def my_method(json_data):
3    json.dumps(json_data)
param exit_msg:

The message to send to exit method.

type exit_msg:

str

param exit_enabled:

Accepts a boolean or string value. If a boolean value is provided that value will control enabling/disabling this feature. A string value should reference an item in the args namespace which resolves to a boolean. The value of this boolean will control enabling/disabling this feature.

type exit_enabled:

boolean|str, kwargs

param write_output:

default True. If enabled, will call app.write_output() when an exception is raised.

type write_output:

boolean

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@OnSuccess(exit_msg='Successfully processed JSON data.')
2def my_method(json_data):
3    json.dumps(json_data)
param exit_msg:

The message to send to exit method.

type exit_msg:

str

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.

1def __init__(self, _tcex):
2    super(App, self).__init__(_tcex)
3    self.output_strings = []  # Output decorator writes here.
4
5@Output(attribute='output_strings')
6def my_method(data):
7    return data.lowercase()
param attribute:

The name of the App attribute to write data.

type attribute:

str

param overwrite:

When True and the method is called more than once the previous value will be overwritten.

type overwrite:

bool

ReadArg

WriteOutput