def with_connection(func):
    """Decorate a function to open a new datafind connection if required

    This method will inspect the ``connection`` keyword, and if `None`
    (or missing), will use the ``host`` and ``port`` keywords to open
    a new connection and pass it as ``connection=<new>`` to ``func``.
    """
    @wraps(func)
    def wrapped(*args, **kwargs):
        if kwargs.get('connection') is None:
            kwargs['connection'] = _choose_connection(host=kwargs.get('host'),
                                                      port=kwargs.get('port'))
        try:
            return func(*args, **kwargs)
        except HTTPException:
            kwargs['connection'] = reconnect(kwargs['connection'])
            return func(*args, **kwargs)
    return wrapped
Decorate a function to open a new datafind connection if required

    This method will inspect the ``connection`` keyword, and if `None`
    (or missing), will use the ``host`` and ``port`` keywords to open
    a new connection and pass it as ``connection=<new>`` to ``func``.