converting textobject to name of other object.

L

Lars Tengnagel

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
self.seqnr=0
self.basenr=0
self.dict = {}

Thanks Lars
 
P

Peter Otten

Lars said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,105
Members
47,699
Latest member
lovelybaghel

Latest Threads

Top