HOFT - Higher Order Func Tools for Python2.7

https://img.shields.io/badge/Author:%20francis%20horsman-Available-brightgreen.svg?style=plastic https://travis-ci.org/sys-git/hoft.svg?branch=master https://coveralls.io/repos/github/sys-git/hoft/badge.svg Documentation Status https://badge.fury.io/py/hoft.svg https://img.shields.io/pypi/l/hoft.svg https://img.shields.io/pypi/wheel/hoft.svg https://img.shields.io/pypi/pyversions/hoft.svg https://img.shields.io/pypi/status/hoft.svg Updates https://pyup.io/repos/github/sys-git/hoft/python-3-shield.svg

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.

Use case

  1. Used in conjunction with a parameter checking and validation library to perform parameter validation 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'

Helpful utilities

Signature Tools contains functions to extract a useful signature and signature components from an argspec.

To install

$ pip install hoft

Contributions

Fork me and create a pull request!

All contributions or suggestions welcome :)

Coding guidelines in the next version.

Indices and tables