def test_get_example(): data = {'a': [1, 2, [3, 4]]} path = ['a', 2, 0] assert json.get(data, path) == 3 data = [1, 2, 3] assert json.get(data, 0) == 1 assert json.get(data, 5) is None
def on_change(): mid = json.get(client.remote_data, 'mid') local_components = json.get(client.remote_data, 'local_components') global_components = json.get(client.remote_data, 'global_components') self._data.set([], dict(self._data.data, mid=mid or 0, local_components=local_components or [], global_components=global_components or []))
def on_change(data): nonlocal last_value new_value = json.get(data, path) if new_value == last_value: return queue.put_nowait(new_value) last_value = new_value
def on_change(): nonlocal last_data new_data = json.get(conn.remote_data, path) if new_data == last_data: return queue.put_nowait(new_data) last_data = new_data
def matches(self, event): if event.payload is None: return False if event.payload.type != common.EventPayloadType.JSON: return False data_path = self._conf.get('data_path', []) data = json.get(event.payload.data, data_path) if 'data_type' in self._conf: data_type = self._conf['data_type'] if data_type == 'null': if data is not None: return False elif data_type == 'boolean': if not isinstance(data, bool): return False elif data_type == 'string': if not isinstance(data, str): return False elif data_type == 'number': if not (isinstance(data, float) or (isinstance(data, int) and not isinstance(data, bool))): return False elif data_type == 'array': if not isinstance(data, list): return False elif data_type == 'object': if not isinstance(data, dict): return False if 'data_value' in self._conf: if self._conf['data_value'] != data: return False return True
def on_change(): components = json.get(client.remote_data, 'components') or [] self._data.set('components', components)
def test_get_invalid_path(): with pytest.raises(ValueError): json.get(None, True)
def test_get(data, path, default, result): x = json.get(data, path, default) assert json.equals(x, result)