b20b5da970264162002689608d4f412a7f640c30 wong Tue Dec 13 12:11:28 2011 -0800 corrected an error in morgan's modification diff --git python/lib/ucscgenomics/ra.py python/lib/ucscgenomics/ra.py index f8e44ba..d0f087f 100644 --- python/lib/ucscgenomics/ra.py +++ python/lib/ucscgenomics/ra.py @@ -61,82 +61,83 @@ We don't always have to just return the stanza in the second parameter however. If we wanted to, for each stanza, return the file associated with that stanza, we could easily do that as well. This would return a simple list of the string filenames in a ra file: files = rafile.filter(lambda s: 1, lambda s: s['fileName']) Note that once again, we don't have to ensure 'fileName' exists. Also note that lambda s: 1 means always return true. Lambda expressions are always preferable to functions unless the expression would need to be reused multiple times. It is also best to reduce the set of stanzas as much as possible before operating over them. Filtering allows you to eliminate a lot of code. ''' - def __init__(self, filePath=None): + def __init__(self, filePath=None, key=None): OrderedDict.__init__(self) if filePath != None: - self.read(filePath) + self.read(filePath, key) def read(self, filePath, key=None): ''' Reads an rafile stanza by stanza, and internalizes it. ''' file = open(filePath, 'r') #entry = None stanza = list() keyValue = '' reading = 1 while reading: line = file.readline() if line == '': reading = 0 line = line.strip() - if len(stanza) == 0 and (line.startswith('#') or (line == '' and reading)): OrderedDict.append(self, line) continue if line != '': stanza.append(line) elif len(stanza) > 0: if keyValue == '': keyValue, name, entry = self.readStanza(stanza, key) else: testKey, name, entry = self.readStanza(stanza, key) if entry != None and keyValue != testKey: raise KeyError('Inconsistent Key ' + testKey) if entry != None: if name != None or key == None: if name in self: raise KeyError('Duplicate Key ' + name) self[name] = entry stanza = list() file.close() def readStanza(self, stanza, key=None): entry = RaStanza() + if entry.readStanza(stanza, key) == None: + return None, None, None val1, val2 = entry.readStanza(stanza, key) return val1, val2, entry def iter(self): pass def iterkeys(self): for item in self._OrderedDict__ordering: if not(item.startswith('#') or item == ''): yield item def itervalues(self):