[Scipy-svn] r2688 - in trunk/Lib/io: . tests
scipy-svn@scip...
scipy-svn@scip...
Wed Feb 7 06:18:45 CST 2007
Author: matthew.brett@gmail.com
Date: 2007-02-07 06:18:41 -0600 (Wed, 07 Feb 2007)
New Revision: 2688
Modified:
trunk/Lib/io/npfile.py
trunk/Lib/io/tests/test_npfile.py
Log:
Modified interface to npfile, cleaned up and expanded docstrings
Modified: trunk/Lib/io/npfile.py
===================================================================
--- trunk/Lib/io/npfile.py 2007-02-07 04:59:42 UTC (rev 2687)
+++ trunk/Lib/io/npfile.py 2007-02-07 12:18:41 UTC (rev 2688)
@@ -1,7 +1,7 @@
-# Author: Matthew Brett, Travis Oliphant
+# Authors: Matthew Brett, Travis Oliphant
"""
-Class for reading and writing numpy arrays from / to files
+Class for reading and writing numpy arrays from / to binary files
"""
import sys
@@ -23,10 +23,38 @@
(['native', 'n'], ['ieee-le', 'l'], ['ieee-be', 'B']) for
native, little-endian, or big-endian respectively.
- Attributes
+ Attributes:
endian -- default endian code for reading / writing
order -- default order for reading writing ('C' or 'F')
- file -- file object containing read / written data
+ file -- file object containing read / written data
+
+ Methods:
+ seek, tell, close -- as for file objects
+ rewind -- set read position to beginning of file
+ read_raw -- read string data from file (read method of file)
+ write_raw -- write string data to file (write method of file)
+ read_array -- read numpy array from binary file data
+ write_array -- write numpy array contents to binary file
+
+ Example use:
+ >>> from StringIO import StringIO
+ >>> import numpy as N
+ >>> from scipy.io import npfile
+ >>> arr = N.arange(10).reshape(5,2)
+ >>> # Make file-like object (could also be file name)
+ >>> my_file = StringIO()
+ >>> npf = npfile(my_file)
+ >>> npf.write_array(arr)
+ >>> npf.rewind()
+ >>> npf.read_array((5,2), arr.dtype)
+ >>> npf.close()
+ >>> # Or read write in Fortran order, Big endian
+ >>> # and read back in C, system endian
+ >>> my_file = StringIO()
+ >>> npf = npfile(my_file, order='F', endian='>')
+ >>> npf.write_array(arr)
+ >>> npf.rewind()
+ >>> npf.read_array((5,2), arr.dtype)
'''
def __init__(self, file_name,
@@ -44,7 +72,6 @@
if closed:
raise TypeError, 'File object should be open'
self.file = file_name
-
self.endian = endian
self.order = order
@@ -95,26 +122,11 @@
else:
self.seek(-howmany,1)
- def size(self):
- """Return the size of the file.
-
- Cached once found
- """
- try:
- sz = self.thesize
- except AttributeError:
- curpos = self.tell()
- self.seek(0,2)
- sz = self.tell()
- self.seek(curpos)
- self.thesize = sz
- return sz
-
- def raw_read(self, size=-1):
+ def read_raw(self, size=-1):
"""Read raw bytes from file as string."""
return self.file.read(size)
- def raw_write(self, str):
+ def write_raw(self, str):
"""Write string to file as raw bytes."""
return self.file.write(str)
@@ -134,16 +146,16 @@
dt_endian = sys_endian_code
return dt_endian
- def write(self, data, endian=None, order=None):
+ def write_array(self, data, endian=None, order=None):
''' Write to open file object the flattened numpy array data
Inputs
data - numpy array or object convertable to array
endian - endianness of written data
(can be None, 'dtype', '<', '>')
- (default from self.endian)
+ (if None, get from self.endian)
order - order of array to write (C, F)
- (default from self.order)
+ (if None from self.order)
'''
endian, order = self._endian_order(endian, order)
data = N.asarray(data)
@@ -153,20 +165,21 @@
data = data.byteswap()
self.file.write(data.tostring(order=order))
- fwrite = write
-
- def read(self, shape, dt, endian=None, order=None):
+ def read_array(self, shape, dt, endian=None, order=None):
'''Read data from file and return it in a numpy array.
Inputs
------
shape - shape of output array, or number of elements
dt - dtype of array to be read
- endian - endianness of written data
+ endian - endianness of data in file
(can be None, 'dtype', '<', '>')
- (default from self.endian)
- order - order of array to be read ('C' or 'F')
- (default from self.order)
+ (if None, get from self.endian)
+ order - order of array in file (C, F)
+ (if None get from self.order)
+
+ Outputs
+ arr - array from file with given dtype (dt)
'''
endian, order = self._endian_order(endian, order)
try:
@@ -185,4 +198,3 @@
return arr.byteswap()
return arr.copy()
- fread = read
Modified: trunk/Lib/io/tests/test_npfile.py
===================================================================
--- trunk/Lib/io/tests/test_npfile.py 2007-02-07 04:59:42 UTC (rev 2687)
+++ trunk/Lib/io/tests/test_npfile.py 2007-02-07 12:18:41 UTC (rev 2688)
@@ -14,12 +14,12 @@
fd, fname = mkstemp()
npf = npfile(fname)
arr = N.reshape(N.arange(10), (5,2))
- self.assertRaises(IOError, npf.write, arr)
+ self.assertRaises(IOError, npf.write_array, arr)
npf.close()
npf = npfile(fname, 'w')
- npf.write(arr)
+ npf.write_array(arr)
npf.rewind()
- self.assertRaises(IOError, npf.read,
+ self.assertRaises(IOError, npf.read_array,
arr.shape,
arr.dtype)
npf.close()
@@ -41,14 +41,14 @@
assert npf.parse_endian('dtype') == 'dtype'
self.assertRaises(ValueError, npf.parse_endian, 'nonsense')
- def test_raw_read_write(self):
+ def test_read_write_raw(self):
npf = npfile(StringIO())
str = 'test me with this string'
- npf.raw_write(str)
+ npf.write_raw(str)
npf.rewind()
- assert str == npf.raw_read(len(str))
+ assert str == npf.read_raw(len(str))
- def test_read_write(self):
+ def test_read_write_array(self):
npf = npfile(StringIO())
arr = N.reshape(N.arange(10), (5,2))
# Arr as read in fortran order
@@ -65,27 +65,27 @@
bs_arr = arr.newbyteorder(nbo)
adt = arr.dtype
shp = arr.shape
- npf.write(arr)
+ npf.write_array(arr)
npf.rewind()
- assert_array_equal(npf.read(shp, adt), arr)
+ assert_array_equal(npf.read_array(shp, adt), arr)
npf.rewind()
- assert_array_equal(npf.read(shp, adt, endian=swapped_code),
+ assert_array_equal(npf.read_array(shp, adt, endian=swapped_code),
bs_arr)
npf.rewind()
- assert_array_equal(npf.read(shp, adt, order='F'),
+ assert_array_equal(npf.read_array(shp, adt, order='F'),
f_arr)
npf.rewind()
- npf.write(arr, order='F')
+ npf.write_array(arr, order='F')
npf.rewind()
- assert_array_equal(npf.read(shp, adt),
+ assert_array_equal(npf.read_array(shp, adt),
cf_arr)
npf = npfile(StringIO(), endian='swapped', order='F')
- npf.write(arr)
+ npf.write_array(arr)
npf.rewind()
- assert_array_equal(npf.read(shp, adt), arr)
+ assert_array_equal(npf.read_array(shp, adt), arr)
npf.rewind()
- assert_array_equal(npf.read(shp, adt, endian='dtype'), bs_arr)
+ assert_array_equal(npf.read_array(shp, adt, endian='dtype'), bs_arr)
npf.rewind()
- assert_array_equal(npf.read(shp, adt, order='C'), cf_arr)
+ assert_array_equal(npf.read_array(shp, adt, order='C'), cf_arr)
More information about the Scipy-svn
mailing list