def validate(self, value: Any) -> Array[str]: """Check if the value is valid returns it""" if value is None: return Array[self.enum_cls]() else: ret = [] if isinstance(value, str): value = [value] # If we have an Array of the right type, start off assuming it's the # same is_same = value.__class__ is Array and value.typ is self.enum_cls for i, choice in enumerate(value): # Our lookup table contains all the possible values try: new_choice = self.choices_lookup[choice] except KeyError: raise ValueError( f"{value} is not a valid value in {self.choices} " f"for element {i}" ) else: is_same &= choice == new_choice ret.append(new_choice) if is_same: return value else: return to_array(Array[self.enum_cls], ret)
def validate(self, value: Any) -> Array: """Check if the value is valid returns it""" cast = to_array(Array[str], value) for v in cast: assert isinstance(v, str), "Expected Array[str], got %r" % (value, ) return cast
def to_np_array(dtype, value): # Give the Array the shorthand version if dtype == np.float64: dtype = float elif dtype == np.int64: dtype = int if isinstance(value, Sequence): # Cast to numpy array value = np.array(value, dtype=dtype) return to_array(Array[dtype], value)
def to_np_array(dtype, value: Any) -> Any: # Give the Array the shorthand version if dtype == np.float64: dtype = float elif dtype == np.int64: dtype = int if value.__class__ is Array and getattr(value.seq, "dtype", None) == dtype: # If Array wraps a numpy array of the correct type we are done return value else: if isinstance(value, Sequence): # Cast to numpy array value = np.array(value, dtype=dtype) return to_array(Array[dtype], value)
def validate(self, value): # type: (Any) -> Array[str] """Check if the value is valid returns it""" if value is None: return Array[self.enum_cls]() else: ret = [] if isinstance(value, str_): value = [value] for i, choice in enumerate(value): # Our lookup table contains all the possible values try: ret.append(self.choices_lookup[choice]) except KeyError: raise ValueError( "%s is not a valid value in %s for element %s" % (value, self.choices, i)) return to_array(Array[self.enum_cls], ret)