def test_flatten(self): sequence = [[1, 2, 3], [], [[2, [3], 4], 6]] expected_value = [1, 2, 3, 2, 3, 4, 6] self.assertEqual(flatten(sequence), expected_value) max_recursion = 2 expected_value = [1, 2, 3, 2, [3], 4, 6] self.assertEqual(flatten(sequence, max_recursion=max_recursion), expected_value)
def is_numeric_sequence(obj: Sequence[Number]) -> bool: try: from cereja.array import flatten sum(flatten(obj)) except (TypeError, ValueError): return False return True
def test_get_shape(self): for expected_shape, seq in zip(self.shapes, self.sequences): shape_received = get_shape(seq) sequence_length = len(flatten(seq)) self.assertEqual( sequence_length, prod(shape_received), msg=f"array shape {shape_received} is inconsistent") self.assertEqual(shape_received, expected_shape)
def normalize_data(cls, data: Any, *args, **kwargs) -> dict: if data is None: return {} if isinstance(data, (dict, set)): return cls.parse(data) if is_sequence(data): if len(data) == 2: if len(flatten(data)) == 2: return cls.parse(cls._parse_key_value(data)) normalized_data = {} for item in data: k, v = cls._parse_key_value(item, take_tuple=True) assert k not in normalized_data, ValueError( f'Data contains duplicate keys <{k}>') normalized_data[k] = v return cls.parse(normalized_data) return cls.parse({data: None})
def flatten(self): return flatten(self.lines)
def flatten(self): return flatten(self._data)