def test_setting_list_path(): """Test that setting a value via a dotted path creates the required structure.""" tmp = NavigableDict({}) tmp.set(['data', 'type'], 'tests') tmp.set(['data', 'id'], '1') assert tmp.data.type == 'tests' assert tmp.data.id == '1'
def test_setting_dotted_path_list_value(): """Test that setting a value via a dotted path creates the required structure.""" tmp = NavigableDict({'data': {'numbers': [1, 2, 3]}}) tmp.set('data.numbers.0', 4) tmp.set('data.numbers.2', 0) assert tmp.data.numbers[0] == 4 assert tmp.data.numbers[2] == 0
def test_setting_dotted_path_nested_list_value(): """Test that setting a value via a dotted path works across a nested list.""" tmp = NavigableDict({'data': {'numbers': [{'a': 1}]}}) tmp.set('data.numbers.0.a', 4) assert tmp.data.numbers[0].a == 4
def __call__(self, record): """Transform the ``record`` according to the mappings of this :class:`~polymatheia.transform.Transform`. :param record: The record to transform :type record: :class:`~polymatheia.data.NavigableDict` :return: The transformed record :rtype: :class:`~polymatheia.data.NavigableDict` """ result = NavigableDict({}) if self._mapping[0] == 'copy': result.set(self._mapping[1], record.get(self._mapping[2])) elif self._mapping[0] == 'static': result.set(self._mapping[1], self._mapping[2]) elif self._mapping[0] == 'fill': if record.get(self._mapping[1]) is None: result.set(self._mapping[1], self._mapping[2]) else: result.set(self._mapping[1], record.get(self._mapping[1])) elif self._mapping[0] == 'split': value = record.get(self._mapping[3]) if value: if isinstance(value, str): for idx, part in enumerate(value.split(self._mapping[2])): result.set(self._mapping[1].format(idx + 1), part) elif isinstance(value, list): for idx, part in enumerate(value): result.set(self._mapping[1].format(idx + 1), part) elif self._mapping[0] == 'combine': result.set(self._mapping[1], [record.get(path) for path in self._mapping[2:]]) elif self._mapping[0] == 'join': if len(self._mapping) == 4: value = record.get(self._mapping[3]) if value: result.set(self._mapping[1], self._mapping[2].join(value)) else: result.set(self._mapping[1], self._mapping[2].join([record.get(path) for path in self._mapping[3:]])) elif self._mapping[0] == 'sequence': tmp = record for part in self._mapping[1:]: result = part(tmp) tmp = result elif self._mapping[0] == 'parallel': for part in self._mapping[1:]: result.merge(part(record)) elif self._mapping[0] == 'custom': result.set(self._mapping[1], self._mapping[2](record)) return result