converting textobject to name of other object.
Peter Otten
__peter__ at web.de
Wed Dec 8 02:27:36 EST 2004
Lars Tengnagel wrote:
> I'm trying to use the matrix variable to collect a matrix in the file
> MatrixInfo which contains a lot of different matrices. Yhe commented line
> is the problem.
> HOW ???????
> import re
> import MatrixInfo
>
> class Diff:
> def __init__(self,filobj,matrix='pam250',begin=0,end='none'):
> self.fil = filobj
> self.begin = begin
> self.end = end
> if matrix in MatrixInfo.available_matrices:
> ######self.matrix =MatrixInfo.matrix
> self.matrix= MatrixInfo.pam250
> else print "matrix don't exist %s" %matrix
Assuming available_matrices_dict is a dictionary with the matrices' name as
keys:
if matrix in MatrixInfo.available_matrices_dict:
self.matrix = MatrixInfo.available_matrices_dict[matrix]
else:
print "matrix don't exist %s" %matrix
or a bit more pythonic, since a Diff instance with an unknown matrix
probably doesn't make much sense:
# you have to handle the exception outside of
# Diff.__init__()
self.matrix = MatrixInfo.available_matrices_dict[matrix]
> self.seqnr=0
> self.basenr=0
> self.dict = {}
Now how will the available_matrices_dict dictionary come to be?
At the end of MatrixInfo do
available_matrices_dict = dict([(name, globals()[name]) for name in
available_matrices])
Or you drop the available_matrices list (I suppose) altogether and use a
dict literal instead
available_matrices_dict = {"pam250": pam250, ...}
Peter
More information about the Python-list
mailing list