ReseekFile and MultiDict

Andrew Dalke
Fri, 10 Jan 2003 19:44:52 -0700

I've developed two new modules people may find interesting.  They
can be found at .  Both modules
have been placed in the public domain.

The first is ReseekFile.

This wraps a file handle to allow seeks back to the beginning. It is 
very useful in Internet work when you need to check that the the input 
stream is in the proper format before passing it to to the parser.

 >>> # Check first if the file starts with 'ERROR:'
 >>> # and if so, raise an exception
 >>> import urllib2, ReseekFile
 >>> infile = urllib2.urlopen("http://somewhere/")
 >>> infile = ReseekFile(infile)
 >>> s = infile.readline()
 >>> if s.startswith("ERROR:"):
 >>>     raise Exception(s[:-1])
 >>> infile.nobuffer()
 >>> ... proces the input file as normal ..

The second is MultiDict

This implements two types of dictionary-like objects where there can be 
more than one entry with the same key. One is OrderedMultiDict, which 
preserves the order of all entries across all keys. The other is 
UnorderedMultidict, which only preserves the order of entries for the 
same key.

 >>> import MultiDict
 >>> od = MultiDict.OrderedMultiDict()
 >>> od["Name"] = "Andrew"; od["Color"] = "Green"
 >>> od["Name"] = "Karen"; od["Color"] = "Brown"
 >>> od["Name"]
 >>> od.getall("Name")
['Andrew', 'Karen']
 >>> for k, v in od.allitems():
...     print "%r == %r", (k, v)
'Name' == 'Andrew'
'Color' == 'Green'
'Name' == 'Karen'
'Color' == 'Brown'
 >>> ud = MultDict.UnorderedMultiDict(od)
 >>> for k, v in ud.allitems():
...     print "%r == %r", (k, v)
'Name' == 'Andrew'
'Name' == 'Karen'
'Color' == 'Green'
'Color' == 'Brown'

Need usable, robust software for bioinformatics or chemical
informatics?  Want to integrate your different tools so you can
do more science in less time?  Contact us!