levitsky / pyteomics

Pyteomics is a collection of lightweight and handy tools for Python that help to handle various sorts of proteomics data. Pyteomics provides a growing set of modules to facilitate the most common tasks in proteomics data analysis.
http://pyteomics.readthedocs.io
Apache License 2.0
105 stars 34 forks source link

file_mode keyword only param to write mgf function breaks linters #111

Closed hechth closed 1 year ago

hechth commented 1 year ago

With the file_mode being passed as keyword only to the write function, this breaks linters which are raising an error:

matchms/exporting/save_as_mgf.py
  Line: 46
    pylint: unexpected-keyword-arg / Unexpected keyword argument 'file_mode' in function call (col 8)

Is the decorator really necessary to open the file and take care of the handling? Would it be possible to just add the 2 arguments to the function and do the file handling within it?

levitsky commented 1 year ago

Hi!

I find that the decorator is necessary because it is used in multiple functions. It helps ensure unified behavior and reduces code duplication, improving reliability.

You can quite easily rewrite the main loop in save_as_mgf such that the file_mode argument is not used at all, eliminating the linting error and arguably optimizing the code:

# Convert matchms.Spectrum() into dictionaries for pyteomics
spectrum_dicts = (
                   {"m/z array": spectrum.peaks.mz,
                    "intensity array": spectrum.peaks.intensities,
                    "params": spectrum.metadata}
   for spectrum in spectrums)

# Append all spectra to file
with open(filename, 'a') as out:
    py_mgf.write(spectrum_dicts, out)

For more info and examples, see #109.

hechth commented 1 year ago

Thanks for the hint!