Skip to content

Decorators

decorators

parametrized(decorator)

Meta-decorator that allows decorators to accept arguments while preserving IDE intellisense.

Parameters:

Name Type Description Default
decorator

The decorator function to wrap.

required

Returns:

Name Type Description
Callable

A wrapper that accepts decorator arguments and returns the decorated function.

Example
@parametrized
def my_decorator(func, arg1, arg2=True):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@my_decorator(arg1='value')
def my_func():
    pass

cache_data(func, timeout, by_args=False, enabled=True, custom_cache=None)

Cache the return value of a function using Django's cache framework.

Useful for expensive calculations or database queries.

Parameters:

Name Type Description Default
func

The function to decorate (injected by @parametrized).

required
timeout int

Cache timeout in seconds.

required
by_args

If True, cache separately per unique arguments. Defaults to False.

False
enabled

Toggle caching on/off. Defaults to True.

True
custom_cache

Optional custom cache backend instance. Defaults to Django's default cache.

None

Returns:

Name Type Description
Callable

Wrapped function with caching.

Example
@cache_data(timeout=3600, by_args=True)
def get_expensive_data(user_id):
    return compute(user_id)

log_exec_time(func, func_name=None, enabled=getattr(settings, 'DR_LOG_EXEC_TIME', False), log_args=False)

Log the execution time of a function via PrintLogger.

Can be enabled globally by setting DR_LOG_EXEC_TIME = True in Django settings.

Parameters:

Name Type Description Default
func

The function to decorate (injected by @parametrized).

required
func_name

Custom name for log output. Defaults to the function's __name__.

None
enabled

Toggle logging on/off. Defaults to settings.DR_LOG_EXEC_TIME.

getattr(settings, 'DR_LOG_EXEC_TIME', False)
log_args

If True, include function arguments in the log key.

False

Returns:

Name Type Description
Callable

Wrapped function that logs its execution time.

Example
@log_exec_time(func_name='my_task', log_args=True)
def process_data(items):
    ...