Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/alignment_util.py
Дата изменения: Fri Feb 28 14:46:08 2014
Дата индексирования: Sat Mar 1 17:12:47 2014
Кодировка:

Поисковые слова: п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п п
#
#MODULE alignment_util
#
#***********************************************************************
"""

**PURPOSE** --
A module for dealing with alignments

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
- Initial implementaion 2/18/04
- beefed up constructor and added set_target_type method. dc 2/7/06
"""
#***********************************************************************
import obset_util
import spss_sys_util
import stpydb
from boolean import *

__version__ = '2/7/06'

if spss_sys_util.get_environ_variable('SPSS_DB'):
SPSS_DB = spss_sys_util.get_environ_variable('SPSS_DB')[0]
else:
SPSS_DB = None

class alignment(dict):
"""A class for alignments.

"""
def __init__(self, data={}):
"""The alignment object constructor.

The input parameter 'data' must be dictionary with, at least, 'proposal_id'
'obset_id', 'alignment_id' defined or a string of the form 'ppppp:oo:aa[:vv]'.
"""
if isinstance(data, dict):
dict.__init__(self, data)

required_keys = ['proposal_id', 'obset_id', 'alignment_id']
for key in required_keys:
if not self.has_key(key):
raise ValueError("input dictionary to alignment constructor must have '%s' as a key" % key)
if not self.has_key('version_num'):
self['version_num'] = '01'
if not self.has_key('obset'):
self['obset'] = obset_util.obset(self.get('proposal_id'),
self.get('obset_id'),
self.get('version_num'))
elif isinstance(data, type('')):
sdata = data.split(':')
if len(sdata) == 3:
alignment.__init__(self, {'proposal_id' : sdata[0],
'obset_id' : sdata[1],
'alignment_id': sdata[2]})
elif len(sdata) == 4:
alignment.__init__(self, {'proposal_id' : sdata[0],
'obset_id' : sdata[1],
'alignment_id': sdata[2],
'version_num' : sdata[3]})
else:
raise ValueError('Input alignment string must be of the form ppppp:oo:aa[:vv]. Input value was: %s' % data)
self.qalignment_flag = False
self.qaposition_flag = False

def __repr__(self):
return str(self.get('obset').proposal) + ":" + self.get('obset_id') + ":"+ self.get('alignment_id') + ":" + self.get('version_num')

def __eq__(self, other):
"""Compare two alignments.
"""
if other['obset'] == self['obset'] and other.get('alignment_id') == self.get('alignment_id'):
return true
else:
return false

def get(self, field, failobj=None):
"""Returns the value of 'field'.

The value of 'field' will be retrieved from the PMDB if necessary.
If the field is 'proposal', the proposal object associated with this
alignment is returned.
If 'field' is not already in the internal dictionary and is not
found in a relation in the db, None will be returned."""
if dict.has_key(self, field):
#If we already have the value, just return it.
return dict.get(self, field)
elif field == 'proposal':
#If 'proposal' is requested, return the proposal object from the obset object
return self.get('obset').get('proposal')
elif self.get('obset').has_field(field):
#If field exists in the obset object, return it.
return self.get('obset').get(field)
elif not self.qalignment_flag:
self.get_qalignment_data()
return self.get(field, failobj)
elif not self.qaposition_flag:
self.get_qaposition_data()
return self.get(field, failobj)
else:
#If field wasn't found anywhere, return failobj.
return failobj

def has_key(self, field):
"""Check the object for the existence of 'field' in its internal dictionary.

Returns TRUE if it is there, FALSE otherwise."""
if (dict.has_key(self, field) or (dict.has_key(self, 'obset') and self['obset'].has_key(field))):
return true
else:
return false

def set_field(self, relation, field, value):
"""Update the database setting the given field
in the given relation to particular value.
"""
dbconn = stpydb.stpydb(dbmsName=SPSS_DB)
dbconn.beginTransaction()
dbconn.query("update %s" % relation)
dbconn.query("set %s = '%s'" % (field, value))
dbconn.query("where proposal_id = @prop")
dbconn.query(" and obset_id = @obs")
dbconn.query(" and alignment_id = @aid")
dbconn.query(" and version_num = @ver")
dbconn.setParam([['prop', self.get('proposal_id')],
['obs' , self.get('obset_id')],
['aid' , self.get('alignment_id')],
['ver' , self.get('version_num')]])
dbconn.executeUpdate()
dbconn.commitTransaction()
self[field] = value
return

def get_qalignment_data(self):
"""Populate this alignment object with the data from the
qalignment relation.
"""
dbconn = stpydb.stpydb(dbmsName=SPSS_DB)
dbconn.query("select * from qalignment")
dbconn.query("where proposal_id = @prop")
dbconn.query(" and obset_id = @obs")
dbconn.query(" and alignment_id = @aid")
dbconn.query(" and version_num = @ver")
dbconn.setParam([['prop', self.get('proposal_id')],
['obs' , self.get('obset_id')],
['aid' , self.get('alignment_id')],
['ver' , self.get('version_num')]])
result = {}
while dbconn.execute(result):
pass
self.update(result)
self.qalignment_flag = True
return

def get_qaposition_data(self):
"""Populate this alignment object with the data from the
qaposition relation.
"""
dbconn = stpydb.stpydb(dbmsName=SPSS_DB)
dbconn.query("select * from qaposition")
dbconn.query("where proposal_id = @prop")
dbconn.query(" and obset_id = @obs")
dbconn.query(" and alignment_id = @aid")
dbconn.query(" and version_num = @ver")
dbconn.setParam([['prop', self.get('proposal_id')],
['obs' , self.get('obset_id')],
['aid' , self.get('alignment_id')],
['ver' , self.get('version_num')]])
result = {}
while dbconn.execute(result):
pass
self.update(result)
self.qaposition_flag = True
return