def test_cvt_methods(self): """Test the methods used to enable JSON coding/decoding. Before JSON encoding, a SpikeElement is converted into a data-based dictionary representation using cvt_elem_to_dict(). After JSON decoding, the resultant dictionary is re-instantiated as a SpikeElement using cvt_dict_to_elem(). This test runs through both conversions back-to-back and compares the resultant SpikeElement with the original SpikeElement to ensure identity. """ cls_list = RecordingExtractor.get_installed_spif_cls_list() elem = RecordingExtractor(cls_list[random.randint( 0, len(cls_list) - 1)]) elem_dict = config.cvt_elem_to_dict(elem) new_elem = config.cvt_dict_to_elem(elem_dict) print(f'SpikeElement tested: {elem.display_name}') assert elem.__class__.__name__ == new_elem.__class__.__name__ assert elem.__module__ == new_elem.__module__ assert elem.spif_class.__name__ == new_elem.spif_class.__name__ assert elem.param_list == new_elem.param_list
def run(elem_list_str): elem_jdict_list = json.loads(elem_list_str) elem_list = [ cfg.cvt_dict_to_elem(elem_jdict) for elem_jdict in elem_jdict_list ] payload = None last_elem_index = len(elem_list) - 1 for count, elem in enumerate(elem_list): next_elem = elem_list[count + 1] \ if count < last_elem_index else None payload = elem.run(payload, next_elem)
def run(elem_list_str): elem_jdict_list = json.loads(elem_list_str) elem_list = [ cfg.cvt_dict_to_elem(elem_jdict) for elem_jdict in elem_jdict_list ] payload = None last_index = len(elem_list) - 1 for index, elem in enumerate(elem_list): if index == last_index: next_elem = None else: next_elem = elem_list[index + 1] payload = elem.run(payload, next_elem)
def _perform_load_action() -> None: """Loads current pipeline with elements from a previously saved JSON file Launches a file dialog box that allows the user to select a previously saved JSON file, attempts to decode it, and if successful adds the elements to the current pipeline replacing any elements extant in the pipeline. config.cvt_dict_to_elem() does most of the hard work, and throws exceptions if the element is no longer installed, or is no longer compatible with the version saved previously. """ global _pipeline_model options = QtWidgets.QFileDialog.Options() options |= QtWidgets.QFileDialog.DontUseNativeDialog file_name, _filter = QtWidgets.QFileDialog.getOpenFileName( config.get_main_window(), caption='Open File', filter='JSON (*.json)', options=options) if file_name: _pipeline_model.clear() try: with open(file_name, 'r') as json_file: elem_dict_list = json.load(json_file) for elem_dict in elem_dict_list: elem = config.cvt_dict_to_elem(elem_dict) _pipeline_model.add_element(elem) except (json.decoder.JSONDecodeError, ValueError) as e: QtWidgets.QMessageBox.warning( config.get_main_window(), 'JSON File Load Failure', f'Failed to load {file_name}: {str(e)}') _pipeline_model.clear() except Exception as e: QtWidgets.QMessageBox.warning( config.get_main_window(), 'JSON File Load Failure', f'Unspecified exception: {str(e)}') _pipeline_model.clear()
def test_cvt_dict_raises(self): """cvt_dict_to_elem() shoule raise exception on invalid parameter""" with pytest.raises(TypeError): config.cvt_dict_to_elem('not a SpikeElement')