Skip to content

Decorators

The Decorators module in Outlify provides a collection of decorators designed to extend the capabilities of features, preserving the original feature signatures and metadata and adding useful behavior.

To view the demo for the Decorators module use:

python -m outlify.decorators
    @timer()
    def dummy_func(a: int, b: int) -> int:
        return a + b

Function 'dummy_func' took 00:00:00.123

Continued...

timer

Use the timer decorator to time the execution of the function

import time
from outlify.decorators import timer

@timer()
def dummy():
    time.sleep(1)

dummy()
Function 'dummy' took 00:00:01.000

label

To set a custom label for a function, use:

import time
from outlify.decorators import timer

@timer(label='Custom name')
def dummy():
    time.sleep(1)

dummy()
Custom name took 00:00:01.000

label_style

To set a colors / styles for label of a function, use:

import time
from outlify.decorators import timer
from outlify.style import Colors

@timer(label_style=[Colors.red])
def dummy():
    time.sleep(1)

dummy()

For details on styling, see Styles.

connector

Word or phrase used to connect the label and the measured duration in the output message (e.g. "took", "in", "completed in")

import time
from outlify.decorators import timer

@timer(connector='in')
def dummy():
    time.sleep(1)

dummy()
Function 'dummy' in 00:00:01.000

time_format

Specifies the format string used to display the function's execution duration.

import time
from outlify.decorators import timer

@timer(time_format='{h} hours {m:02} minutes {s:02} seconds')
def dummy():
    time.sleep(1)

dummy()
Function 'dummy' took 0 hours 00 minutes 01 seconds

The string should use Python’s standard str.format syntax and supports the following placeholders:

  • {h} - hours
  • {m} - minutes (0–59)
  • {s} - seconds (0–59)
  • {ms} - milliseconds (0–999)

You can fully customize the output format using any combination of these placeholders along with Python formatting options.

Examples

  • Default format: "{h:02}:{m:02}:{s:02}.{ms:03}"00:01:23.456
  • Human-readable format: "{m} min {s} sec"1 min 23 sec
  • Minimal format: "{m}:{s}"1:23

If the format string contains any invalid key, e.g., {minutes} instead of {m}, a KeyError will be raised, indicating the allowed keys.

time_style

To set a colors / styles for function runtime, use:

import time
from outlify.decorators import timer
from outlify.style import Colors, Styles

@timer(time_style=[Colors.crimson, Styles.underline])
def dummy():
    time.sleep(1)

dummy()

For details on styling, see Styles.

output_func

Specifies the function that will be used to output the final timing message.

import logging
import time
from outlify.decorators import timer

logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)

@timer(output_func=logger.info)
def dummy():
    time.sleep(1)

dummy()
INFO:root:Function 'dummy' took 00:00:01.000