Entering edit mode
7.1 years ago
mrth
▴
30
"Hello world",
I am using Python (2.7) and have a class that creates a string in the format of a dictionary. However, I would like to have an actual dictionary output where I can extract values by using raw_input
for the keys. I have the following code so far:
class FastaFile:
def __init__(self, path):
self.path = path
self._map = {}
self.__fasta_iter()
def __str__(self):
return self._map.__str__()
def __fasta_iter(self):
fasta = open(self.path)
fasta_iter = (x[1] for x in groupby(fasta, lambda line: line[0] == ">"))
for header in fasta_iter:
header = header.next()[1:9].strip()
seq = "".join(s.strip() for s in fasta_iter.next())
self._map[header] = seq
cff = FastaFile("obs.fasta")
I know I can use a for loop like so:
for kv in cff._map.viewitems():
print kv
Which will give me an output like this:
('ID001','ACCGTA')
('ID002','AGTCCA')
However, I would very much like to continue with my class or perhaps create a subclass with the same output. I've been looking into it and I believe I may need __getitem__? I've tried fiddling about with my code using __getitem__ but keep getting the dreaded error messages. Any help would be much appreciated.