Пример #1
0
    def coerce(self, instance, num):
        if num is not None:
            if is_array(num) and num.shape == ():
                num = num.item()  # convert scalar array to Python object

            if not is_number(num):
                raise ValidationError("Must be a number; got '%s'" % num,
                                      attr=self.name,
                                      obj=instance)
            low_comp = 0 if self.low_open else -1
            if self.low is not None and compare(num, self.low) <= low_comp:
                raise ValidationError(
                    "Value must be greater than %s%s (got %s)" %
                    ("" if self.low_open else "or equal to ", self.low, num),
                    attr=self.name,
                    obj=instance,
                )
            high_comp = 0 if self.high_open else 1
            if self.high is not None and compare(num, self.high) >= high_comp:
                raise ValidationError(
                    "Value must be less than %s%s (got %s)" %
                    ("" if self.high_open else "or equal to ", self.high, num),
                    attr=self.name,
                    obj=instance,
                )
        return super().coerce(instance, num)
Пример #2
0
    def coerce(self, instance, num):
        if num is not None:
            if is_array(num) and num.shape == ():
                num = num.item()  # convert scalar array to Python object

            if not is_number(num):
                raise ValidationError(
                    f"Must be a number; got '{num}'", attr=self.name, obj=instance
                )
            low_comp = 0 if self.low_open else -1
            if self.low is not None and compare(num, self.low) <= low_comp:
                eq_phrase = "" if self.low_open else " or equal to"
                raise ValidationError(
                    f"Value must be greater than{eq_phrase} {self.low} (got {num})",
                    attr=self.name,
                    obj=instance,
                )
            high_comp = 0 if self.high_open else 1
            if self.high is not None and compare(num, self.high) >= high_comp:
                eq_phrase = "" if self.high_open else " or equal to"
                raise ValidationError(
                    f"Value must be less than{eq_phrase} {self.high} (got {num})",
                    attr=self.name,
                    obj=instance,
                )
        return super().coerce(instance, num)
Пример #3
0
 def _lift_arg(cls, arg):
     # A bit like the inverse of base.py::lower_folds::_lower_arg except direct mode
     # stimuli are automatically created from arrays for testing convenience.
     if is_array(arg):
         return stimuli(arg).configure(neuron_type=nengo.Direct())
     if isinstance(arg, (tuple, list)):
         return type(arg)(map(cls._lift_arg, arg))
     return arg
Пример #4
0
def asoperator(x):
    """Returns x as a single Operator if possible, otherwise as a Fold."""
    if isinstance(x, Operator):
        op = x
    elif is_array(x) and x.shape == ():
        op = x.item()
        if not isinstance(op, Operator):
            raise TypeError(f"expected array scalar ({op}) to be an Operator")
    else:
        op = fold(x)
    return op
Пример #5
0
    def coerce(self, instance, num):
        if num is not None:
            if is_array(num) and num.shape == ():
                num = num.item()  # convert scalar array to Python object

            if not is_number(num):
                raise ValidationError("Must be a number; got '%s'" % num,
                                      attr=self.name, obj=instance)
            low_comp = 0 if self.low_open else -1
            if self.low is not None and compare(num, self.low) <= low_comp:
                raise ValidationError(
                    "Value must be greater than %s%s (got %s)" % (
                        "" if self.low_open else "or equal to ",
                        self.low,
                        num), attr=self.name, obj=instance)
            high_comp = 0 if self.high_open else 1
            if self.high is not None and compare(num, self.high) >= high_comp:
                raise ValidationError(
                    "Value must be less than %s%s (got %s)" % (
                        "" if self.high_open else "or equal to ",
                        self.high,
                        num), attr=self.name, obj=instance)
        return super().coerce(instance, num)
Пример #6
0
 def _is_number_or_scalar(x):
     return is_number(x) or (is_array(x) and x.ndim == 0)