免责声明:几个月前开始编码,我经常使用不正确的语言,提前道歉。
我正在编写一个函数,它将 sbml 模型(.xml 文件)转换为乳胶中更易读的东西(转换为 .tex 文件)。
我在将独立代码转换为函数时遇到了问题。我最好的猜测是问题出在嵌套函数中(转移后)listfiller。
我从 sbml 模型中获取信息的主要(而且不是太漂亮)的方法是将代码预先写入一个列表(Commands在函数中调用,这些基本上是对 libSBML 库中的函数的调用,这些函数提取了一些信息 [字符串、整数、等]),它listfiller根据obj我输入嵌套函数的任何对象(称为)进行评估。
出于某种原因,一旦我将整个代码移动到一个函数中,listfiller就再也看不到对象了。此外,它确实看到了字面上命名的那些obj:
试图把*args而**kwargs不是obj无济于事
将所有内容命名为“obj”是不可能的(即真的很麻烦)
这是函数本身(需要运行Tellurium库pip install tellurium:)和 libSBML 的任何 .xml 文件,如下所示
def xml2tex(name):
import tesbml
def listfiller(Commands , obj, tofill = [], twoD = 1):
'''
Uses a dismal method of evaluating a piece of code
from 'Commands' to fit a specific string into 'tofill'
takes in a libsbml object as obj
if twoD = 0, then does not fill 'tofill' with the templin as one element
but returns the compiled templin as 1-D list
'''
l = len(Commands)
templin = [None]*l
for i in range(l):
templin[i] = eval(Commands[i])
if twoD == 1:
tofill.append(templin)
return tofill
elif twoD == 0:
return templin
reader = tesbml.SBMLReader()
mod = reader.readSBML(name)
Reactions = []
l = mod.model.getNumReactions()
Rlist = ['R.getId()','R.getReversible()','R.getFast()']
for x in range(0,l):
R = mod.model.getReaction(x)
RL = listfiller(Rlist, R, twoD=0) #starting the element of common matrix/list to append at the end
Reactions.append(RL);
return Reactions
错误:
Traceback (most recent call last):
File "<ipython-input-56-48f48283afb5>", line 1, in <module>
xml2tex('model.xml')
File "/Users/sergejczan/Desktop/Lab/untitled1.py", line 38, in xml2tex
RL = listfiller(Rlist, obj = R, twoD=0) #starting the element of common matrix/list to append at the end
File "/Users/sergejczan/Desktop/Lab/untitled1.py", line 23, in listfiller
templin[i] = eval(Commands[i])
File "<string>", line 1, in <module>
NameError: name 'R' is not defined