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

W

WP

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
 
B

Bruno Desthuilliers

WP a écrit :
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

This should go to stderr. stdout is for normal program outputs.
return None

Also, Python has exception handling, which is usually a better solution
than returning None or whatever.

since you returned in the other branch, this else is useless
sbml_model = sbml_doc.getModel()

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

You're calling sbml_doc.getModel() a first time, binding the returned
value to name sbml_model, then you're calling it a second time to return it.

A first fix:

import sys

def getSBMLModel(biomodel_path):
reader = libsbml.SBMLReader()
sbml_doc = reader.readSBML(biomodel_path)
if sbml_doc.getNumErrors() > 0:
# XXX : there's perhaps a better error message
# to get from sbml_doc ?
print >> sys.stderr, "I couldn't read the file %s!"\
% biomodel_path
return None

return sbml_doc.getModel()

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

if sbml_model == None:

Better to use the identity test here - None is garanteed to be a singleton:

if sbml_model is 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'

This shadows the builtin 'file' symbol. Better to use another name.
# 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?

The only explanation I can imagine here would be that a second call to
sbml_doc.getModel() doesn't return the same thing as the first...
(remember, there are two calls in your getSBMLModel() function).

My 2 cents...
 
G

Gabriel Genellina

En Thu, 12 Feb 2009 07:07:53 -0200, WP
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

Note that sbml_doc.getModel() is called *twice*, the first one is
discarded. Probably you want to replace the last line with: return
sbml_model
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top