/hoft

Higher Order Func Tools

Primary LanguagePythonMIT LicenseMIT

H.O.F.T

Author

Higher Order Func Tools for Python2.7

Build Status Coverage Status Documentation Status PyPI version PyPI PyPI PyPI PyPI Updates Python 3

Decorators that can be used to analyse a function's positional, keyword and default arguments.

HOFT uses getargspec and getcallargs (from the inspect module) under the hood.

The params are then passed directly to the decorated function and any exceptions are propagated back to the caller.

Read documentation

Complete documentation can be found at readthedocs

Use case

  1. Used in conjunction with a parameter checking and certification library to perform parameter certification prior to function execution.
    from hoft import analyse_sig, IGNORE
    from certifiable import certify_int, certify_string
    ...

    @analyse_sig(certify_int(min_value=-100, max_value=100), IGNORE, c=IGNORE, d=certify_string(max_length=2))
    def my_function(a, b, c=None, d=None, e='world'):
        ...

    >>> my_function(-256, 'x', 'y', 'abcd')
    Traceback (most recent call last):
    ...
    CertifierError: .....

Simple example

    from hoft import analyse_sig, IGNORE
    
    def func(arg_name, arg_index, arg_value, default_value=None):
        # do my thing and potentially raise an exception here
        if arg_name == 'a':
            assert arg_index==0
            assert arg_value==5
        elif arg_name == 'd':
            assert arg_index==2
            assert called_with_value==7
            assert default_value==None

        ...
        raise MyError(value)

    ...


    @analyse_sig(func, IGNORE, c=IGNORE, d=func)
    def my_function(a, b, c=None, d=None, e='world'):
        ...


    # call the decorated method, and the arguments will be checked prior to my_function execution:
    my_function(5, 6, c=7, d=8)

    # my_function is called as expected and receives: a=5, b=6, c=7, d=8, e='world'

To install

    $ pip install hoft

Build documentation

    $ make sphinx-html