Resolve input expression.

    :param str expr: configuration expression to resolve in this language.
    :param bool safe: safe run execution context (True by default).
    :param bool tostr: format the result.
    :param dict scope: execution scope (contains references to expression
        objects).
    :param bool besteffort: try to resolve unknown variable name with execution
        runtime.

def resolvepy(
        expr,
        safe=DEFAULT_SAFE, tostr=DEFAULT_TOSTR, scope=DEFAULT_SCOPE,
        besteffort=DEFAULT_BESTEFFORT
    ):
    """Resolve input expression.

    :param str expr: configuration expression to resolve in this language.
    :param bool safe: safe run execution context (True by default).
    :param bool tostr: format the result.
    :param dict scope: execution scope (contains references to expression
        objects).
    :param bool besteffort: try to resolve unknown variable name with execution
        runtime."""

    result = None

    _eval = safe_eval if safe else eval

    _expr = expr

    scope = {} if scope is None else scope.copy()

    while True:

        try:
            result = _eval(_expr, scope)

        except (AttributeError, NameError) as nex:

            if not besteffort:
                raise

            arg = nex.args[0]
            missing = MISSING_NAME_REGEX.findall(arg)[-1]

            try:
                _expr = sub(
                    MISSING_VARIABLE.format(missing),
                    genrepl(scope=scope),
                    _expr
                )

            except ImportError:
                raise nex

        else:
            break

    if tostr:
        result = str(result)

    return result