From 22774bf15e51a8cf0f1dbdca23a6ffd932d6a9dc Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sat, 28 Jun 2008 21:02:25 +0000 Subject: Shorten long lines. --- numpy/lib/format.py | 97 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 39 deletions(-) (limited to 'numpy/lib/format.py') diff --git a/numpy/lib/format.py b/numpy/lib/format.py index 8762db785..13e96f303 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -1,12 +1,14 @@ -""" Define a simple format for saving numpy arrays to disk with the full +"""Define a simple format for saving numpy arrays to disk. + +Define a simple format for saving numpy arrays to disk with the full information about them. WARNING: Due to limitations in the interpretation of structured dtypes, dtypes with fields with empty names will have the names replaced by 'f0', 'f1', etc. -Such arrays will not round-trip through the format entirely accurately. The data -is intact; only the field names will differ. We are working on a fix for this. -This fix will not require a change in the file format. The arrays with such -structures can still be saved and restored, and the correct dtype may be +Such arrays will not round-trip through the format entirely accurately. The +data is intact; only the field names will differ. We are working on a fix for +this. This fix will not require a change in the file format. The arrays with +such structures can still be saved and restored, and the correct dtype may be restored by using the `loadedarray.view(correct_dtype)` method. Format Version 1.0 @@ -24,11 +26,11 @@ version of the numpy package. The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN. -The next HEADER_LEN bytes form the header data describing the array's format. It -is an ASCII string which contains a Python literal expression of a dictionary. -It is terminated by a newline ('\\n') and padded with spaces ('\\x20') to make -the total length of the magic string + 4 + HEADER_LEN be evenly divisible by 16 -for alignment purposes. +The next HEADER_LEN bytes form the header data describing the array's format. +It is an ASCII string which contains a Python literal expression of a +dictionary. It is terminated by a newline ('\\n') and padded with spaces +('\\x20') to make the total length of the magic string + 4 + HEADER_LEN be +evenly divisible by 16 for alignment purposes. The dictionary contains three keys: @@ -43,8 +45,8 @@ The dictionary contains three keys: The shape of the array. For repeatability and readability, the dictionary keys are sorted in alphabetic -order. This is for convenience only. A writer SHOULD implement this if possible. -A reader MUST NOT depend on this. +order. This is for convenience only. A writer SHOULD implement this if +possible. A reader MUST NOT depend on this. Following the header comes the array data. If the dtype contains Python objects (i.e. dtype.hasobject is True), then the data is a Python pickle of the array. @@ -52,6 +54,7 @@ Otherwise the data is the contiguous (either C- or Fortran-, depending on fortran_order) bytes of the array. Consumers can figure out the number of bytes by multiplying the number of elements given by the shape (noting that shape=() means there is 1 element) by dtype.itemsize. + """ import cPickle @@ -112,10 +115,11 @@ def dtype_to_descr(dtype): """ Get a serializable descriptor from the dtype. The .descr attribute of a dtype object cannot be round-tripped through the - dtype() constructor. Simple types, like dtype('float32'), have a descr which - looks like a record array with one field with '' as a name. The dtype() - constructor interprets this as a request to give a default name. Instead, we - construct descriptor that can be passed to dtype(). + dtype() constructor. Simple types, like dtype('float32'), have a descr + which looks like a record array with one field with '' as a name. The + dtype() constructor interprets this as a request to give a default name. + Instead, we construct descriptor that can be passed to dtype(). + """ if dtype.names is not None: # This is a record array. The .descr is fine. @@ -169,10 +173,10 @@ def write_array_header_1_0(fp, d): # Need to use repr here, since we eval these when reading header += "'%s': %s, " % (key, repr(value)) header += "}" - # Pad the header with spaces and a final newline such that the magic string, - # the header-length short and the header are aligned on a 16-byte boundary. - # Hopefully, some system, possibly memory-mapping, can take advantage of - # our premature optimization. + # Pad the header with spaces and a final newline such that the magic + # string, the header-length short and the header are aligned on a 16-byte + # boundary. Hopefully, some system, possibly memory-mapping, can take + # advantage of our premature optimization. current_header_len = MAGIC_LEN + 2 + len(header) + 1 # 1 for the newline topad = 16 - (current_header_len % 16) header = '%s%s\n' % (header, ' '*topad) @@ -210,7 +214,8 @@ def read_array_header_1_0(fp): # header. hlength_str = fp.read(2) if len(hlength_str) != 2: - raise ValueError("EOF at %s before reading array header length" % fp.tell()) + msg = "EOF at %s before reading array header length" + raise ValueError(msg % fp.tell()) header_length = struct.unpack('