本文整理汇总了Python中app.model.DBSession.all方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.all方法的具体用法?Python DBSession.all怎么用?Python DBSession.all使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.model.DBSession
的用法示例。
在下文中一共展示了DBSession.all方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mergeMol
# 需要导入模块: from app.model import DBSession [as 别名]
# 或者: from app.model.DBSession import all [as 别名]
def mergeMol(mol):
"""
Certain attributes of a molecule (e.g. InChI, CAS number, IUPAC name) are unique. This method analyzes
a molecule to see if it might already exist in the database based on those unique attributes. If the
molecule is found in the database, this method does a consistency check, ensuring that any non-null, non-empty
unique attributes match those of the database molecule.
If the molecule is found in the db and the consistency check passes, merge the molecule's attributes into the
db molecule. "Merge" means overwrite the db attribute if the db attribute is the empty string or null.
If merging fails this function will raise an exception. Merging should only fail if the mol passed in to this
function and the mol in the database have conflicting values for a unique attribute. For example, a CAS number
is unique to a structure. If the mol passed in has a different CAS number from the db mol, there is a problem--we
don't know which is the correct one.
Return the database molecule if found. Otherwise add the passed in mol to the session and return it.
At least ONE of the unique attributes must be set--in the case of a corporate compound, for example, you
must set the structure, or a reg ID, or a CAS number, or the IUPAC name. Otherwise there is no handle to
uniquely identify the molecule.
@param mol: molecule to merge with matching database molecule
@type mol: _RegMolBase
@return: the merged molecule
@rtype: _RegMolBase
"""
# There should be at most one version of the molecule in the database
# Get list of indexes for the table storing the molecules
m = object_mapper(mol)
indexes=m.tables[0].indexes
# Get list of attributes that must be unique
uniqueAttributes = []
for col in m.tables[0].primary_key:
uniqueAttributes.append(col.name)
for i in indexes:
if i.unique:
if len(i.columns) == 1:
attr = i.columns[0].name
if attr not in uniqueAttributes:
uniqueAttributes.append(attr)
# Sort the unique attributes. This guarantees that we analyze them in the same way each time, improving our
# ability to properly unit test this method.
uniqueAttributes.sort()
# Check each unique attribute. If the passed in molecule has a non-None, non-empty string value, search
# the db. If a db mol is found, move on to the merge. If no db mol is found, continue checking the unique
# attributes.
query = None
attr = None
mols=[]
validMol = False # a valid molecule must have at least ONE unique attribute set
for attr in uniqueAttributes:
attrVal = getattr(mol,attr)
if attrVal is not None and attrVal is not None:
query = DBSession().query(type(mol)).filter(type(mol).__dict__[attr] == getattr(mol,attr))
mols = query.all()
validMol = True
if mols:
break
if not validMol:
raise MissingUniqueAttrError('At least one unique attribute must be set for a molecule: check ' + ','. join(uniqueAttributes))
if len(mols) > 1:
raise Exception("Unique constraint on " + attr + " violated: check " + ','.join([t.name for t in object_mapper(mol).tables]))
# Check if we were not able to find a molecule in the database
if len(mols) == 0:
mol.mol_id = None # make sure database assigns the primary key
DBSession().add(mol)
return mol
dbMol = mols[0]
# The molecule exists in the database. Make sure that any unique property of the database molecule matches
# the one passed in. For example, CAS number is unique. The CAS number of the passed in molecule must
# be the same as the one in the database, or something is wrong.
msgs = []
parameters = { 'molid' : dbMol.molid, 'param' : []}
for attr in uniqueAttributes:
molVal = getattr(mol, attr, None)
dbVal = getattr(dbMol, attr, None)
if dbVal and molVal and dbVal != molVal:
msgs.append('%s: Compound database has value of %s, tried to register new value of %s' % (attr, dbVal, molVal))
parameters['param'].append(dict(attr=attr, dbVal=dbVal, molVal=molVal))
if msgs:
msg = 'Inconsistent values for properties of molecule during registration to database\n' + '\n'.join(msgs)
raise InconsistentMolError(msg, parameters)
# Everything checks out as consistent. Update the db mol from the passed-in mol (for any cases where the mol
# attribute has a value)
for c in m.tables[0].columns:
if c.primary_key:
continue
attr=c.name
molVal = getattr(mol, attr, None)
dbVal = getattr(dbMol, attr, None)
#.........这里部分代码省略.........