Repository: matchms/matchms
Pull request #276 (2021-12-08 12:32:44)

Remove assertion


Patch:
diff --git a/matchms/exporting/save_as_msp.py b/matchms/exporting/save_as_msp.py
index 32fc26b9..d7b73529 100644
--- a/matchms/exporting/save_as_msp.py
+++ b/matchms/exporting/save_as_msp.py
@@ -1,3 +1,4 @@
+import logging
 import os
 from typing import IO
 from typing import List
@@ -5,6 +6,12 @@ from ..Spectrum import Spectrum
 from ..Spikes import Spikes
 
 
+logger = logging.getLogger("matchms")
+
+
+_extentions_not_allowed = ["mzml", "mzxml", "json", "mgf"]
+
+
 def save_as_msp(spectra: List[Spectrum], filename: str):
     """Save spectrum(s) as msp file.
 
@@ -35,39 +42,42 @@ def save_as_msp(spectra: List[Spectrum], filename: str):
     filename:
         Provide filename to save spectrum(s).
     """
-
-    assert filename.endswith(".msp"), "File extension must be 'msp'."
-
-    spectra = ensure_list(spectra)
+    file_extension = filename.split(".")[-1]
+    assert file_extension.lower() not in _extentions_not_allowed, \
+        f"File extension '.{file_extension}' not allowed."
+    if not filename.endswith(".msp"):
+        logger.warning("Spectra will be stored as msp file with extension .%s",
+                       filename.split(".")[-1])
+    spectra = _ensure_list(spectra)
 
     with open(filename, "w", encoding="utf-8") as outfile:
         for spectrum in spectra:
-            write_spectrum(spectrum, outfile)
+            _write_spectrum(spectrum, outfile)
 
 
-def write_spectrum(spectrum: Spectrum, outfile: IO):
-    write_metadata(spectrum.metadata, outfile)
-    write_peaks(spectrum.peaks, outfile)
+def _write_spectrum(spectrum: Spectrum, outfile: IO):
+    _write_metadata(spectrum.metadata, outfile)
+    _write_peaks(spectrum.peaks, outfile)
     outfile.write(os.linesep)
 
 
-def write_peaks(peaks: Spikes, outfile: IO):
+def _write_peaks(peaks: Spikes, outfile: IO):
     outfile.write(f"NUM PEAKS: {len(peaks)}\n")
     for mz, intensity in zip(peaks.mz, peaks.intensities):
         outfile.write(f"{mz}\t{intensity}\n")
 
 
-def write_metadata(metadata: dict, outfile: IO):
+def _write_metadata(metadata: dict, outfile: IO):
     for key, value in metadata.items():
-        if not is_num_peaks(key):
+        if not _is_num_peaks(key):
             outfile.write(f"{key.upper()}: {value}\n")
 
 
-def is_num_peaks(key: str) -> bool:
+def _is_num_peaks(key: str) -> bool:
     return key.lower().startswith("num peaks")
 
 
-def ensure_list(spectra) -> List[Spectrum]:
+def _ensure_list(spectra) -> List[Spectrum]:
     if not isinstance(spectra, list):
         # Assume that input was single Spectrum
         spectra = [spectra]


Test patch:
diff --git a/tests/test_save_as_msp.py b/tests/test_save_as_msp.py
index ae4fdaa6..36a1b96b 100644
--- a/tests/test_save_as_msp.py
+++ b/tests/test_save_as_msp.py
@@ -43,8 +43,8 @@ def test_spectrum_none_exception(none_spectrum, filename):
     assert message == "'NoneType' object has no attribute 'metadata'"
 
 
-def test_wrong_filename_exception():
-    """ Test for exception being thrown if output file doesn't end with .msp. """
+def test_not_allowed_filename_extension():
+    """ Test for exception if output file ends with not allowed extension."""
     with tempfile.TemporaryDirectory() as temp_dir:
         filename = os.path.join(temp_dir, "test.mzml")
 
@@ -52,10 +52,20 @@ def test_wrong_filename_exception():
             save_as_msp(None, filename)
 
         message = exception.value.args[0]
-        assert message == "File extension must be 'msp'."
+        assert message == "File extension '.mzml' not allowed."
+
+
+def test_non_msp_filename_extension(spectrum, caplog):
+    """ Test for log message if output file doesn't end with .msp. """
+    with tempfile.TemporaryDirectory() as temp_dir:
+        filename = os.path.join(temp_dir, "test.dat")
+
+        save_as_msp(spectrum, filename)
+
+        expected_log = "Spectra will be stored as msp file with extension .dat"
+        assert expected_log in caplog.text
 
 
-# Using tmp_path fixture from pytest: https://docs.pytest.org/en/stable/tmpdir.html#the-tmp-path-fixture
 def test_file_exists_single_spectrum(spectrum, filename):
     """ Test checking if the file is created. """
     save_as_msp(spectrum, filename)