本文整理匯總了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)
#.........這裏部分代碼省略.........