<s> from __future__ import division , print_function , absolute_import <EOL> import struct <EOL> import numpy as np <EOL> from numpy . compat import asstr <EOL> import tempfile <EOL> import zlib <EOL> import warnings <EOL> DTYPE_DICT = { <NUM_LIT:1> : '<STR_LIT>' , <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:3> : '<STR_LIT>' , <EOL> <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT:5> : '<STR_LIT>' , <EOL> <NUM_LIT:6> : '<STR_LIT>' , <EOL> <NUM_LIT:7> : '<STR_LIT>' , <EOL> <NUM_LIT:8> : '<STR_LIT>' , <EOL> <NUM_LIT:9> : '<STR_LIT>' , <EOL> <NUM_LIT:10> : '<STR_LIT>' , <EOL> <NUM_LIT:11> : '<STR_LIT>' , <EOL> <NUM_LIT:12> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:15> : '<STR_LIT>' } <EOL> RECTYPE_DICT = { <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:6> : "<STR_LIT>" , <EOL> <NUM_LIT:10> : "<STR_LIT>" , <EOL> <NUM_LIT:12> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT:15> : "<STR_LIT>" , <EOL> <NUM_LIT:16> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT:20> : "<STR_LIT>" } <EOL> STRUCT_DICT = { } <EOL> def _align_32 ( f ) : <EOL> '''<STR_LIT>''' <EOL> pos = f . tell ( ) <EOL> if pos % <NUM_LIT:4> != <NUM_LIT:0> : <EOL> f . seek ( pos + <NUM_LIT:4> - pos % <NUM_LIT:4> ) <EOL> return <EOL> def _skip_bytes ( f , n ) : <EOL> '''<STR_LIT>''' <EOL> f . read ( n ) <EOL> return <EOL> def _read_bytes ( f , n ) : <EOL> '''<STR_LIT>''' <EOL> return f . read ( n ) <EOL> def _read_byte ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . uint8 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) [ : <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] ) <EOL> def _read_long ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . int32 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] ) <EOL> def _read_int16 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . int16 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) [ <NUM_LIT:2> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] ) <EOL> def _read_int32 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . int32 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] ) <EOL> def _read_int64 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . int64 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:8> ) ) [ <NUM_LIT:0> ] ) <EOL> def _read_uint16 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . uint16 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) [ <NUM_LIT:2> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] ) <EOL> def _read_uint32 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . uint32 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] ) <EOL> def _read_uint64 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . uint64 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:8> ) ) [ <NUM_LIT:0> ] ) <EOL> def _read_float32 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . float32 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] ) <EOL> def _read_float64 ( f ) : <EOL> '''<STR_LIT>''' <EOL> return np . float64 ( struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:8> ) ) [ <NUM_LIT:0> ] ) <EOL> class Pointer ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , index ) : <EOL> self . index = index <EOL> return <EOL> class ObjectPointer ( Pointer ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def _read_string ( f ) : <EOL> '''<STR_LIT>''' <EOL> length = _read_long ( f ) <EOL> if length > <NUM_LIT:0> : <EOL> chars = _read_bytes ( f , length ) <EOL> _align_32 ( f ) <EOL> chars = asstr ( chars ) <EOL> else : <EOL> chars = '<STR_LIT>' <EOL> return chars <EOL> def _read_string_data ( f ) : <EOL> '''<STR_LIT>''' <EOL> length = _read_long ( f ) <EOL> if length > <NUM_LIT:0> : <EOL> length = _read_long ( f ) <EOL> string_data = _read_bytes ( f , length ) <EOL> _align_32 ( f ) <EOL> else : <EOL> string_data = '<STR_LIT>' <EOL> return string_data <EOL> def _read_data ( f , dtype ) : <EOL> '''<STR_LIT>''' <EOL> if dtype == <NUM_LIT:1> : <EOL> if _read_int32 ( f ) != <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return _read_byte ( f ) <EOL> elif dtype == <NUM_LIT:2> : <EOL> return _read_int16 ( f ) <EOL> elif dtype == <NUM_LIT:3> : <EOL> return _read_int32 ( f ) <EOL> elif dtype == <NUM_LIT:4> : <EOL> return _read_float32 ( f ) <EOL> elif dtype == <NUM_LIT:5> : <EOL> return _read_float64 ( f ) <EOL> elif dtype == <NUM_LIT:6> : <EOL> real = _read_float32 ( f ) <EOL> imag = _read_float32 ( f ) <EOL> return np . complex64 ( real + imag * <NUM_LIT> ) <EOL> elif dtype == <NUM_LIT:7> : <EOL> return _read_string_data ( f ) <EOL> elif dtype == <NUM_LIT:8> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> elif dtype == <NUM_LIT:9> : <EOL> real = _read_float64 ( f ) <EOL> imag = _read_float64 ( f ) <EOL> return np . complex128 ( real + imag * <NUM_LIT> ) <EOL> elif dtype == <NUM_LIT:10> : <EOL> return Pointer ( _read_int32 ( f ) ) <EOL> elif dtype == <NUM_LIT:11> : <EOL> return ObjectPointer ( _read_int32 ( f ) ) <EOL> elif dtype == <NUM_LIT:12> : <EOL> return _read_uint16 ( f ) <EOL> elif dtype == <NUM_LIT> : <EOL> return _read_uint32 ( f ) <EOL> elif dtype == <NUM_LIT> : <EOL> return _read_int64 ( f ) <EOL> elif dtype == <NUM_LIT:15> : <EOL> return _read_uint64 ( f ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % dtype ) <EOL> def _read_structure ( f , array_desc , struct_desc ) : <EOL> '''<STR_LIT>''' <EOL> nrows = array_desc [ '<STR_LIT>' ] <EOL> columns = struct_desc [ '<STR_LIT>' ] <EOL> dtype = [ ] <EOL> for col in columns : <EOL> if col [ '<STR_LIT>' ] or col [ '<STR_LIT>' ] : <EOL> dtype . append ( ( ( col [ '<STR_LIT:name>' ] . lower ( ) , col [ '<STR_LIT:name>' ] ) , np . object_ ) ) <EOL> else : <EOL> if col [ '<STR_LIT>' ] in DTYPE_DICT : <EOL> dtype . append ( ( ( col [ '<STR_LIT:name>' ] . lower ( ) , col [ '<STR_LIT:name>' ] ) , <EOL> DTYPE_DICT [ col [ '<STR_LIT>' ] ] ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % <EOL> col [ '<STR_LIT>' ] ) <EOL> structure = np . recarray ( ( nrows , ) , dtype = dtype ) <EOL> for i in range ( nrows ) : <EOL> for col in columns : <EOL> dtype = col [ '<STR_LIT>' ] <EOL> if col [ '<STR_LIT>' ] : <EOL> structure [ col [ '<STR_LIT:name>' ] ] [ i ] = _read_structure ( f , <EOL> struct_desc [ '<STR_LIT>' ] [ col [ '<STR_LIT:name>' ] ] , <EOL> struct_desc [ '<STR_LIT>' ] [ col [ '<STR_LIT:name>' ] ] ) <EOL> elif col [ '<STR_LIT>' ] : <EOL> structure [ col [ '<STR_LIT:name>' ] ] [ i ] = _read_array ( f , dtype , <EOL> struct_desc [ '<STR_LIT>' ] [ col [ '<STR_LIT:name>' ] ] ) <EOL> else : <EOL> structure [ col [ '<STR_LIT:name>' ] ] [ i ] = _read_data ( f , dtype ) <EOL> if array_desc [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> dims = array_desc [ '<STR_LIT>' ] [ : int ( array_desc [ '<STR_LIT>' ] ) ] <EOL> dims . reverse ( ) <EOL> structure = structure . reshape ( dims ) <EOL> return structure <EOL> def _read_array ( f , typecode , array_desc ) : <EOL> '''<STR_LIT>''' <EOL> if typecode in [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:15> ] : <EOL> if typecode == <NUM_LIT:1> : <EOL> nbytes = _read_int32 ( f ) <EOL> if nbytes != array_desc [ '<STR_LIT>' ] : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> array = np . fromstring ( f . read ( array_desc [ '<STR_LIT>' ] ) , <EOL> dtype = DTYPE_DICT [ typecode ] ) <EOL> elif typecode in [ <NUM_LIT:2> , <NUM_LIT:12> ] : <EOL> array = np . fromstring ( f . read ( array_desc [ '<STR_LIT>' ] * <NUM_LIT:2> ) , <EOL> dtype = DTYPE_DICT [ typecode ] ) [ <NUM_LIT:1> : : <NUM_LIT:2> ] <EOL> else : <EOL> array = [ ] <EOL> for i in range ( array_desc [ '<STR_LIT>' ] ) : <EOL> dtype = typecode <EOL> data = _read_data ( f , dtype ) <EOL> array . append ( data ) <EOL> array = np . array ( array , dtype = np . object_ ) <EOL> if array_desc [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> dims = array_desc [ '<STR_LIT>' ] [ : int ( array_desc [ '<STR_LIT>' ] ) ] <EOL> dims . reverse ( ) <EOL> array = array . reshape ( dims ) <EOL> _align_32 ( f ) <EOL> return array <EOL> def _read_record ( f ) : <EOL> '''<STR_LIT>''' <EOL> record = { '<STR_LIT>' : _read_long ( f ) } <EOL> nextrec = _read_uint32 ( f ) <EOL> nextrec += _read_uint32 ( f ) * <NUM_LIT:2> ** <NUM_LIT:32> <EOL> _skip_bytes ( f , <NUM_LIT:4> ) <EOL> if not record [ '<STR_LIT>' ] in RECTYPE_DICT : <EOL> raise Exception ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> record [ '<STR_LIT>' ] = RECTYPE_DICT [ record [ '<STR_LIT>' ] ] <EOL> if record [ '<STR_LIT>' ] in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> else : <EOL> record [ '<STR_LIT>' ] = _read_long ( f ) <EOL> _skip_bytes ( f , <NUM_LIT:4> ) <EOL> rectypedesc = _read_typedesc ( f ) <EOL> if rectypedesc [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if nextrec == f . tell ( ) : <EOL> record [ '<STR_LIT:data>' ] = None <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> varstart = _read_long ( f ) <EOL> if varstart != <NUM_LIT:7> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if rectypedesc [ '<STR_LIT>' ] : <EOL> record [ '<STR_LIT:data>' ] = _read_structure ( f , rectypedesc [ '<STR_LIT>' ] , <EOL> rectypedesc [ '<STR_LIT>' ] ) <EOL> elif rectypedesc [ '<STR_LIT>' ] : <EOL> record [ '<STR_LIT:data>' ] = _read_array ( f , rectypedesc [ '<STR_LIT>' ] , <EOL> rectypedesc [ '<STR_LIT>' ] ) <EOL> else : <EOL> dtype = rectypedesc [ '<STR_LIT>' ] <EOL> record [ '<STR_LIT:data>' ] = _read_data ( f , dtype ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> _skip_bytes ( f , <NUM_LIT:4> * <NUM_LIT> ) <EOL> record [ '<STR_LIT:date>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT:user>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT:host>' ] = _read_string ( f ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT>' ] = _read_long ( f ) <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT:title>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT>' ] = _read_string ( f ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT:description>' ] = _read_string_data ( f ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT>' ] = _read_long ( f ) <EOL> record [ '<STR_LIT>' ] = [ ] <EOL> for i in range ( record [ '<STR_LIT>' ] ) : <EOL> record [ '<STR_LIT>' ] . append ( _read_long ( f ) ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT>' ] = _read_long ( f ) <EOL> record [ '<STR_LIT:name>' ] = _read_string ( f ) <EOL> record [ '<STR_LIT>' ] = [ ] <EOL> for i in range ( record [ '<STR_LIT>' ] ) : <EOL> record [ '<STR_LIT>' ] . append ( _read_string ( f ) ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> record [ '<STR_LIT:end>' ] = True <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> elif record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % <EOL> record [ '<STR_LIT>' ] ) <EOL> f . seek ( nextrec ) <EOL> return record <EOL> def _read_typedesc ( f ) : <EOL> '''<STR_LIT>''' <EOL> typedesc = { '<STR_LIT>' : _read_long ( f ) , '<STR_LIT>' : _read_long ( f ) } <EOL> if typedesc [ '<STR_LIT>' ] & <NUM_LIT:2> == <NUM_LIT:2> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> typedesc [ '<STR_LIT>' ] = typedesc [ '<STR_LIT>' ] & <NUM_LIT:4> == <NUM_LIT:4> <EOL> typedesc [ '<STR_LIT>' ] = typedesc [ '<STR_LIT>' ] & <NUM_LIT:32> == <NUM_LIT:32> <EOL> if typedesc [ '<STR_LIT>' ] : <EOL> typedesc [ '<STR_LIT>' ] = _read_arraydesc ( f ) <EOL> typedesc [ '<STR_LIT>' ] = _read_structdesc ( f ) <EOL> elif typedesc [ '<STR_LIT>' ] : <EOL> typedesc [ '<STR_LIT>' ] = _read_arraydesc ( f ) <EOL> return typedesc <EOL> def _read_arraydesc ( f ) : <EOL> '''<STR_LIT>''' <EOL> arraydesc = { '<STR_LIT>' : _read_long ( f ) } <EOL> if arraydesc [ '<STR_LIT>' ] == <NUM_LIT:8> : <EOL> _skip_bytes ( f , <NUM_LIT:4> ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> _skip_bytes ( f , <NUM_LIT:8> ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> arraydesc [ '<STR_LIT>' ] = [ ] <EOL> for d in range ( arraydesc [ '<STR_LIT>' ] ) : <EOL> arraydesc [ '<STR_LIT>' ] . append ( _read_long ( f ) ) <EOL> elif arraydesc [ '<STR_LIT>' ] == <NUM_LIT> : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> _skip_bytes ( f , <NUM_LIT:8> ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_uint64 ( f ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_uint64 ( f ) <EOL> arraydesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> _skip_bytes ( f , <NUM_LIT:8> ) <EOL> arraydesc [ '<STR_LIT>' ] = <NUM_LIT:8> <EOL> arraydesc [ '<STR_LIT>' ] = [ ] <EOL> for d in range ( arraydesc [ '<STR_LIT>' ] ) : <EOL> v = _read_long ( f ) <EOL> if v != <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> arraydesc [ '<STR_LIT>' ] . append ( _read_long ( f ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % arraydesc [ '<STR_LIT>' ] ) <EOL> return arraydesc <EOL> def _read_structdesc ( f ) : <EOL> '''<STR_LIT>''' <EOL> structdesc = { } <EOL> structstart = _read_long ( f ) <EOL> if structstart != <NUM_LIT:9> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> structdesc [ '<STR_LIT:name>' ] = _read_string ( f ) <EOL> predef = _read_long ( f ) <EOL> structdesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> structdesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> structdesc [ '<STR_LIT>' ] = predef & <NUM_LIT:1> <EOL> structdesc [ '<STR_LIT>' ] = predef & <NUM_LIT:2> <EOL> structdesc [ '<STR_LIT>' ] = predef & <NUM_LIT:4> <EOL> if not structdesc [ '<STR_LIT>' ] : <EOL> structdesc [ '<STR_LIT>' ] = [ ] <EOL> for t in range ( structdesc [ '<STR_LIT>' ] ) : <EOL> structdesc [ '<STR_LIT>' ] . append ( _read_tagdesc ( f ) ) <EOL> for tag in structdesc [ '<STR_LIT>' ] : <EOL> tag [ '<STR_LIT:name>' ] = _read_string ( f ) <EOL> structdesc [ '<STR_LIT>' ] = { } <EOL> for tag in structdesc [ '<STR_LIT>' ] : <EOL> if tag [ '<STR_LIT>' ] : <EOL> structdesc [ '<STR_LIT>' ] [ tag [ '<STR_LIT:name>' ] ] = _read_arraydesc ( f ) <EOL> structdesc [ '<STR_LIT>' ] = { } <EOL> for tag in structdesc [ '<STR_LIT>' ] : <EOL> if tag [ '<STR_LIT>' ] : <EOL> structdesc [ '<STR_LIT>' ] [ tag [ '<STR_LIT:name>' ] ] = _read_structdesc ( f ) <EOL> if structdesc [ '<STR_LIT>' ] or structdesc [ '<STR_LIT>' ] : <EOL> structdesc [ '<STR_LIT>' ] = _read_string ( f ) <EOL> structdesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> structdesc [ '<STR_LIT>' ] = [ ] <EOL> for s in range ( structdesc [ '<STR_LIT>' ] ) : <EOL> structdesc [ '<STR_LIT>' ] . append ( _read_string ( f ) ) <EOL> structdesc [ '<STR_LIT>' ] = [ ] <EOL> for s in range ( structdesc [ '<STR_LIT>' ] ) : <EOL> structdesc [ '<STR_LIT>' ] . append ( _read_structdesc ( f ) ) <EOL> STRUCT_DICT [ structdesc [ '<STR_LIT:name>' ] ] = structdesc <EOL> else : <EOL> if not structdesc [ '<STR_LIT:name>' ] in STRUCT_DICT : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> structdesc = STRUCT_DICT [ structdesc [ '<STR_LIT:name>' ] ] <EOL> return structdesc <EOL> def _read_tagdesc ( f ) : <EOL> '''<STR_LIT>''' <EOL> tagdesc = { '<STR_LIT>' : _read_long ( f ) } <EOL> if tagdesc [ '<STR_LIT>' ] == - <NUM_LIT:1> : <EOL> tagdesc [ '<STR_LIT>' ] = _read_uint64 ( f ) <EOL> tagdesc [ '<STR_LIT>' ] = _read_long ( f ) <EOL> tagflags = _read_long ( f ) <EOL> tagdesc [ '<STR_LIT>' ] = tagflags & <NUM_LIT:4> == <NUM_LIT:4> <EOL> tagdesc [ '<STR_LIT>' ] = tagflags & <NUM_LIT:32> == <NUM_LIT:32> <EOL> tagdesc [ '<STR_LIT>' ] = tagdesc [ '<STR_LIT>' ] in DTYPE_DICT <EOL> return tagdesc <EOL> def _replace_heap ( variable , heap ) : <EOL> if isinstance ( variable , Pointer ) : <EOL> while isinstance ( variable , Pointer ) : <EOL> if variable . index == <NUM_LIT:0> : <EOL> variable = None <EOL> else : <EOL> if variable . index in heap : <EOL> variable = heap [ variable . index ] <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> variable = None <EOL> replace , new = _replace_heap ( variable , heap ) <EOL> if replace : <EOL> variable = new <EOL> return True , variable <EOL> elif isinstance ( variable , np . core . records . recarray ) : <EOL> for ir , record in enumerate ( variable ) : <EOL> replace , new = _replace_heap ( record , heap ) <EOL> if replace : <EOL> variable [ ir ] = new <EOL> return False , variable <EOL> elif isinstance ( variable , np . core . records . record ) : <EOL> for iv , value in enumerate ( variable ) : <EOL> replace , new = _replace_heap ( value , heap ) <EOL> if replace : <EOL> variable [ iv ] = new <EOL> return False , variable <EOL> elif isinstance ( variable , np . ndarray ) : <EOL> if variable . dtype . type is np . object_ : <EOL> for iv in range ( variable . size ) : <EOL> replace , new = _replace_heap ( variable . item ( iv ) , heap ) <EOL> if replace : <EOL> variable . itemset ( iv , new ) <EOL> return False , variable <EOL> else : <EOL> return False , variable <EOL> class AttrDict ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , init = { } ) : <EOL> dict . __init__ ( self , init ) <EOL> def __getitem__ ( self , name ) : <EOL> return super ( AttrDict , self ) . __getitem__ ( name . lower ( ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> return super ( AttrDict , self ) . __setitem__ ( key . lower ( ) , value ) <EOL> __getattr__ = __getitem__ <EOL> __setattr__ = __setitem__ <EOL> __call__ = __getitem__ <EOL> def readsav ( file_name , idict = None , python_dict = False , <EOL> uncompressed_file_name = None , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> records = [ ] <EOL> if python_dict or idict : <EOL> variables = { } <EOL> else : <EOL> variables = AttrDict ( ) <EOL> f = open ( file_name , '<STR_LIT:rb>' ) <EOL> signature = _read_bytes ( f , <NUM_LIT:2> ) <EOL> if signature != b'<STR_LIT>' : <EOL> raise Exception ( "<STR_LIT>" % signature ) <EOL> recfmt = _read_bytes ( f , <NUM_LIT:2> ) <EOL> if recfmt == b'<STR_LIT>' : <EOL> pass <EOL> elif recfmt == b'<STR_LIT>' : <EOL> if verbose : <EOL> print ( "<STR_LIT>" ) <EOL> if uncompressed_file_name : <EOL> fout = open ( uncompressed_file_name , '<STR_LIT>' ) <EOL> else : <EOL> fout = tempfile . NamedTemporaryFile ( suffix = '<STR_LIT>' ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" % fout . name ) <EOL> fout . write ( b'<STR_LIT>' ) <EOL> while True : <EOL> rectype = _read_long ( f ) <EOL> fout . write ( struct . pack ( '<STR_LIT>' , int ( rectype ) ) ) <EOL> nextrec = _read_uint32 ( f ) <EOL> nextrec += _read_uint32 ( f ) * <NUM_LIT:2> ** <NUM_LIT:32> <EOL> unknown = f . read ( <NUM_LIT:4> ) <EOL> if RECTYPE_DICT [ rectype ] == '<STR_LIT>' : <EOL> fout . write ( struct . pack ( '<STR_LIT>' , int ( nextrec ) % <NUM_LIT:2> ** <NUM_LIT:32> ) ) <EOL> fout . write ( struct . pack ( '<STR_LIT>' , int ( ( nextrec - ( nextrec % <NUM_LIT:2> ** <NUM_LIT:32> ) ) / <NUM_LIT:2> ** <NUM_LIT:32> ) ) ) <EOL> fout . write ( unknown ) <EOL> break <EOL> pos = f . tell ( ) <EOL> rec_string = zlib . decompress ( f . read ( nextrec - pos ) ) <EOL> nextrec = fout . tell ( ) + len ( rec_string ) + <NUM_LIT:12> <EOL> fout . write ( struct . pack ( '<STR_LIT>' , int ( nextrec % <NUM_LIT:2> ** <NUM_LIT:32> ) ) ) <EOL> fout . write ( struct . pack ( '<STR_LIT>' , int ( ( nextrec - ( nextrec % <NUM_LIT:2> ** <NUM_LIT:32> ) ) / <NUM_LIT:2> ** <NUM_LIT:32> ) ) ) <EOL> fout . write ( unknown ) <EOL> fout . write ( rec_string ) <EOL> f . close ( ) <EOL> f = fout <EOL> f . seek ( <NUM_LIT:4> ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % recfmt ) <EOL> while True : <EOL> r = _read_record ( f ) <EOL> records . append ( r ) <EOL> if '<STR_LIT:end>' in r : <EOL> if r [ '<STR_LIT:end>' ] : <EOL> break <EOL> f . close ( ) <EOL> heap = { } <EOL> for r in records : <EOL> if r [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> heap [ r [ '<STR_LIT>' ] ] = r [ '<STR_LIT:data>' ] <EOL> for r in records : <EOL> if r [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> replace , new = _replace_heap ( r [ '<STR_LIT:data>' ] , heap ) <EOL> if replace : <EOL> r [ '<STR_LIT:data>' ] = new <EOL> variables [ r [ '<STR_LIT>' ] . lower ( ) ] = r [ '<STR_LIT:data>' ] <EOL> if verbose : <EOL> for record in records : <EOL> if record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT:date>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT:user>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT:host>' ] ) <EOL> break <EOL> for record in records : <EOL> if record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> break <EOL> for record in records : <EOL> if record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT:title>' ] ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT>' ] ) <EOL> break <EOL> for record in records : <EOL> if record [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> print ( "<STR_LIT>" % record [ '<STR_LIT:description>' ] ) <EOL> break <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> print ( "<STR_LIT>" % <EOL> ( len ( records ) ) ) <EOL> rectypes = [ r [ '<STR_LIT>' ] for r in records ] <EOL> for rt in set ( rectypes ) : <EOL> if rt != '<STR_LIT>' : <EOL> print ( "<STR_LIT>" % ( rectypes . count ( rt ) , rt ) ) <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> if '<STR_LIT>' in rectypes : <EOL> print ( "<STR_LIT>" ) <EOL> for var in variables : <EOL> print ( "<STR_LIT>" % ( var , type ( variables [ var ] ) ) ) <EOL> print ( "<STR_LIT:->" * <NUM_LIT:50> ) <EOL> if idict : <EOL> for var in variables : <EOL> idict [ var ] = variables [ var ] <EOL> return idict <EOL> else : <EOL> return variables </s>
