Adding object instances to a list only works with tedious depth, how can I change this?

I have a pymzml.run.Reader class from the pymzml package. This is a generator object, and when passed through it yields instances of the Spectrum class (also from the pymzml package). I compare different instances to each other. Since pymzml.run.Reader is a generator object, after passing through them, they can no longer be used, so I save them in a comparison list later.

However, when I save them in the list and then go through the list that prints out the id of the spectra, it shows that it only stores the last spectrum. To clarify:

import pymzml

def test(msrun):
    for spectrum in msrun:
        print spectrum['id']            
        spectrumList.append(spectrum)
    print '-'*20
    for i in spectrumList:
        print i['id']

msrun = pymzml.run.Reader(r'JG_Ti02-C1-1_C2-01A_file1.aligned.mzML')

      

gives:

1
2
3
4
5
--------------------
5
5 
5 
5
5

      

The pymzml has a deRef () function that makes a deep copy of the spectrum, so the following works correctly:

import pymzml

def test(msrun):
    for spectrum in msrun: 
        print spectrum['id']
        spectrumList.append(spectrum.deRef())

msrun = pymzml.run.Reader(r'JG_Ti02-C1-1_C2-01A_file1.aligned.mzML') 

      

However, creating deepcopies is the main bottleneck I am trying to get out of the application. How do I add instances of the spectrum to the list so that not only the last spectrum is added several times?

+3


source to share


1 answer


You can't just keep the last spectrum - you are doing everything right to keep every object in the list.

The problem is that you get the same object over and over.

Printing id(spectrum)

in a loop to get its memory address will show that it is one object that is repeated with its id

and other attributes.



Although you don't have to copy.deepcopy()

, you need to make a copy. Try it copy.copy()

and look at the source Spectrum.decRef()

to see how it copies it.

Chances are, you need decRef()

to make each one to make them independent - otherwise, why did the class provide a custom method?

+1


source







All Articles