diff options
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index fe855a71a..5f274f27c 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -123,6 +123,14 @@ class BagObj(object): except KeyError: raise AttributeError(key) + def __dir__(self): + """ + Enables dir(bagobj) to list the files in an NpzFile. + + This also enables tab-completion in an interpreter or IPython. + """ + return object.__getattribute__(self, '_obj').keys() + def zipfile_factory(*args, **kwargs): import zipfile @@ -288,8 +296,7 @@ def load(file, mmap_mode=None): Parameters ---------- file : file-like object or string - The file to read. Compressed files with the filename extension - ``.gz`` are acceptable. File-like objects must support the + The file to read. File-like objects must support the ``seek()`` and ``read()`` methods. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional @@ -425,7 +432,9 @@ def save(file, arr): Notes ----- - For a description of the ``.npy`` format, see `format`. + For a description of the ``.npy`` format, see the module docstring + of `numpy.lib.format` or the Numpy Enhancement Proposal + http://docs.scipy.org/doc/numpy/neps/npy-format.html Examples -------- @@ -496,7 +505,9 @@ def savez(file, *args, **kwds): The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in ``.npy`` format. For a - description of the ``.npy`` format, see `format`. + description of the ``.npy`` format, see `numpy.lib.format` or the + Numpy Enhancement Proposal + http://docs.scipy.org/doc/numpy/neps/npy-format.html When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for @@ -1081,7 +1092,12 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', fh.write(asbytes(format % tuple(row2) + newline)) else: for row in X: - fh.write(asbytes(format % tuple(row) + newline)) + try: + fh.write(asbytes(format % tuple(row) + newline)) + except TypeError: + raise TypeError("Mismatch between array dtype ('%s') and " + "format specifier ('%s')" + % (str(X.dtype), format)) if len(footer) > 0: footer = footer.replace('\n', '\n' + comments) fh.write(asbytes(comments + footer + newline)) @@ -1519,7 +1535,9 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # Process the filling_values ............................... # Rename the input for convenience - user_filling_values = filling_values or [] + user_filling_values = filling_values + if user_filling_values is None: + user_filling_values = [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually @@ -1574,22 +1592,25 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, for (miss, fill) in zipit] # Update the converters to use the user-defined ones uc_update = [] - for (i, conv) in user_converters.items(): + for (j, conv) in user_converters.items(): # If the converter is specified by column names, use the index instead - if _is_string_like(i): + if _is_string_like(j): try: - i = names.index(i) + j = names.index(j) + i = j except ValueError: continue elif usecols: try: - i = usecols.index(i) + i = usecols.index(j) except ValueError: # Unused converter specified continue - # Find the value to test: + else: + i = j + # Find the value to test - first_line is not filtered by usecols: if len(first_line): - testing_value = first_values[i] + testing_value = first_values[j] else: testing_value = None converters[i].update(conv, locked=True, |