Why does way_1() obtain the correct list but way_2() gets an empty list?

WP no.i.dont at want.mail.from.spammers.com
Thu Feb 12 04:07:53 EST 2009


Hello group! This is probably a silly newbie mistake but consider the 
following program:

import libsbml

def getSBMLModel(biomodel_path):
     reader = libsbml.SBMLReader()

     sbml_doc = reader.readSBML(biomodel_path)
     sbml_model = None

     if sbml_doc.getNumErrors() > 0:
         print 'I couldn\'t read the file %s!' % biomodel_path
         return None
     else:
         sbml_model = sbml_doc.getModel()

     return sbml_doc.getModel() # None if file couldn't be opened


def way_1(biomodel_path):
     reader = libsbml.SBMLReader()
     sbml_doc = reader.readSBML(biomodel_path)
     sbml_model = sbml_doc.getModel()

     if sbml_model == None:
         return

     l = sbml_model.getListOfSpecies()
     print 'In way_1(): Got list %s with the length %i' % (l, len(l))


def way_2(biomodel_path):
     sbml_model = getSBMLModel(biomodel_path)

     if sbml_model == None:
         return

     l = sbml_model.getListOfSpecies()
     print 'In way_2(): Got list %s with the length %i' % (l, len(l))


file = '../BIOMD0000000003.xml'
# Changing the order of these two calls doesn't help, only way_1() works.
way_1(file)
way_2(file)

When run, the output is:
In way_1(): Got list <libsbml.ListOfSpecies; proxy of <Swig Object of 
type 'ListOfSpecies *' at 0x291c8bc> > with the length 3
In way_2(): Got list <libsbml.ListOfSpecies; proxy of <Swig Object of 
type 'ListOfSpecies *' at 0x27fb57c> > with the length 0

I don't get it way the species list obtained in way_2() is empty? For 
the input file I'm using I should be getting 3 species for the test file 
I'm using.
I'm sorry that this program uses a third-party library which is probably 
unknown to many, but since I make a lot of newbie mistakes I thought 
that it's very likely that this is a issue in how I use python, not a 
library issue. To compensate somewhat I tried to show my entire test 
program and its output and make that test program as clear as possible.

So if anyone knows why I get this behavior I would like to hear it and I 
will have learned something today too! :)

- WP



More information about the Python-list mailing list