Re: [Numpy-discussion] Making NpzFiles behave more like dictionaries.

2009-06-02 Thread Pauli Virtanen
Tue, 02 Jun 2009 14:55:02 -0400, David Warde-Farley wrote:
> It's occasionally annoyed me that NpzFiles can't be swapped in
> transparently for an in-memory dictionary since getting at the keys
> requires an attribute access. Below is a patch that implements some more
> of the dictionary interface for the NpzFile class. Any comments as to
> whether this is a good/bad idea, or about the specific implementation?

+0 

I don't see any drawbacks, and the implementation looks good.

-- 
Pauli Virtanen

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Making NpzFiles behave more like dictionaries.

2009-06-02 Thread David Warde-Farley
Hi,

It's occasionally annoyed me that NpzFiles can't be swapped in  
transparently for an in-memory dictionary since getting at the keys  
requires an attribute access. Below is a patch that implements some  
more of the dictionary interface for the NpzFile class. Any comments  
as to whether this is a good/bad idea, or about the specific  
implementation?

Regards,

David


Index: io.py
===
--- io.py   (revision 7031)
+++ io.py   (working copy)
@@ -118,6 +118,25 @@
  else:
  raise KeyError, "%s is not a file in the archive" % key

+def __iter__(self):
+return iter(self.files)
+
+def items(self):
+return [(f, self[f]) for f in self.files]
+
+def iteritems(self):
+return ((f, self[f]) for f in self.files)
+
+def keys(self):
+return self.files
+
+def iterkeys(self):
+return self.__iter__()
+
+def __contains__(self, key):
+return self.files.__contains__(key)
+
+
  def load(file, mmap_mode=None):
  """
  Load a pickled, ``.npy``, or ``.npz`` binary file.

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion