Decorator Utilites


check_kwargs_not_none

 check_kwargs_not_none (func)

*A decorator that checks if any keyword argument is None. Raises a ValueError if any argument is None.

Args: func: The function to be decorated.

Returns: The decorated function.

Raises: ValueError: If any keyword argument is None.*


io

 io (func)

*A decorator that inspects the inputs and outputs of a function.

Args: func: The function to be decorated.

Returns: The decorated function.*


timeit

 timeit (func)

*A decorator that measures the execution time of a function.

Args: func (callable): The function to be timed.

Returns: callable: The wrapped function.

Example: @timeit def my_function(): # code to be timed pass

my_function()  # prints the execution time of my_function*

warn_on_fail

 warn_on_fail (func)

format

 format (input)
@timeit
@io
def foo(a, b):
    """
    This function takes two arguments, `a` and `b`, and returns their sum.

    Parameters:
    a (int): The first number.
    b (int): The second number.

    Returns:
    int: The sum of `a` and `b`.
    """
    import time

    time.sleep(1)
    return a + b


foo(10, 11)
══════════════════════════════════════════════════════════════════
INPUTS:ARGS:
tuple of 2 items
    int: 10
    int: 11
══════════════════════════════════════════════════════════════════
══════════════════════════════════════════════════════════════════
OUTPUTS:
int: 21
══════════════════════════════════════════════════════════════════
[10/02/24 18:38:57] INFO     foo took 1.00 seconds to execute                                                                      d=177960;file://<ipython-input-1-6ac2073623b5>:47\<ipython-input-1-6ac2073623b5>;;\:d=13206;file://<ipython-input-1-6ac2073623b5>:47#wrapper:47\wrapper:47;;\
21
@check_kwargs_not_none
@io
def foo(*, a=None, b=None):
    return a + b


foo(a=None, b=10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[34], line 6
      1 @check_kwargs_not_none
      2 @io
      3 def foo(*, a=None, b=None):
      4     return a + b
----> 6 foo(a=None, b=10)

Cell In[32], line 32, in check_kwargs_not_none.<locals>.wrapper(*args, **kwargs)
     30 for key, value in kwargs.items():
     31     if value is None:
---> 32         raise ValueError(f"Input argument '{key}' cannot be None")
     33 return func(*args, **kwargs)

ValueError: Input argument 'a' cannot be None
import nbdev

nbdev.nbdev_export()