Exemplo n.º 1
0
def test_is_trivially_callable():
    def foo():
        pass

    def bar(x, y):
        pass

    assert util.is_trivially_callable(foo)
    assert util.is_trivially_callable(bar, non_def_args=2)
    with pytest.raises(TypeError):
        util.is_trivially_callable(1)
Exemplo n.º 2
0
    def __init__(self, values=None, inherit_params=False, filter_params=None):
        if values is None:
            values = []

        # By default, filter out all the parameter values defined in the
        # base classes.
        if not inherit_params:

            def filter_params(x):
                return ()

        # If inherit_params==True, inherit all the parameter values from the
        # base classes as default behaviour.
        elif filter_params is None:

            def filter_params(x):
                return x

        self.values = tuple(values)

        # Validate the filter_param argument
        try:
            valid = utils.is_trivially_callable(filter_params, non_def_args=1)
        except TypeError:
            raise TypeError(
                'the provided parameter filter is not a callable') from None
        else:
            if not valid:
                raise TypeError('filter function must take a single argument')

        self.filter_params = filter_params
Exemplo n.º 3
0
    def get_variant_nums(self, **conditions):
        '''Filter the paramter indices with a given set of conditions.

        The conditions are passed as key-value pairs, where the keys are the
        parameter names to apply the filtering on and the values are functions
        that expect the parameter's value as the sole argument.
        '''
        candidates = range(len(self))
        if not conditions:
            return list(candidates)

        # Validate conditions
        for param, cond in conditions.items():
            if param not in self:
                raise NameError(f'no such parameter: {param!r}')
            elif not utils.is_trivially_callable(cond, non_def_args=1):
                raise ValueError(
                    f'condition on {param!r} must be a callable accepting a '
                    f'single argument')

            def param_val(variant):
                return self._get_param_value(param, variant)

            # Filter the given condition
            candidates = [v for v in candidates if cond(param_val(v))]

        return candidates
Exemplo n.º 4
0
    def __init__(self,
                 values=None,
                 inherit_params=False,
                 filter_params=None,
                 fmt=None,
                 loggable=False):
        if values is None:
            values = []

        if not inherit_params:
            # By default, filter out all the parameter values defined in the
            # base classes.
            def filter_params(x):
                return ()
        elif filter_params is None:
            # If inherit_params==True, inherit all the parameter values from
            # the base classes as default behaviour.
            def filter_params(x):
                return x

        self.values = tuple(values)

        # Validate and set the filter_params function
        if (not callable(filter_params)
                or not utils.is_trivially_callable(filter_params,
                                                   non_def_args=1)):
            raise TypeError("'filter_params' argument must be a callable "
                            "accepting a single argument")

        self.filter_params = filter_params

        # Validate and set the alternative function
        if fmt is None:

            def fmt(x):
                return x

        if (not callable(fmt)
                or not utils.is_trivially_callable(fmt, non_def_args=1)):
            raise TypeError("'fmt' argument must be a callable "
                            "accepting a single argument")

        self.__fmt_fn = fmt
        self.__loggable = loggable
Exemplo n.º 5
0
    def _deco_wrapper(func):
        if not utils.is_trivially_callable(func, non_def_args=1):
            raise TypeError(f'performance function {func.__name__!r} has more '
                            f'than one argument without a default value')

        @functools.wraps(func)
        def _perf_fn(*args, **kwargs):
            return _DeferredPerformanceExpression(func, unit, *args, **kwargs)

        _perf_key = perf_key if perf_key else func.__name__
        setattr(_perf_fn, '_rfm_perf_key', _perf_key)
        return _perf_fn