<s> from __future__ import division <EOL> import errno <EOL> import functools <EOL> import gzip <EOL> import itertools <EOL> import io <EOL> import mmap <EOL> import os <EOL> import platform <EOL> import signal <EOL> import sys <EOL> import tempfile <EOL> import textwrap <EOL> import threading <EOL> import warnings <EOL> import weakref <EOL> from distutils . version import LooseVersion <EOL> try : <EOL> from StringIO import StringIO <EOL> except ImportError : <EOL> class StringIO ( object ) : <EOL> pass <EOL> import numpy as np <EOL> from . extern import six <EOL> from . extern . six import ( iteritems , string_types , integer_types , text_type , <EOL> binary_type , next ) <EOL> from . extern . six . moves import zip , reduce <EOL> BLOCK_SIZE = <NUM_LIT> <EOL> if six . PY3 : <EOL> cmp = lambda a , b : ( a > b ) - ( a < b ) <EOL> elif six . PY2 : <EOL> cmp = cmp <EOL> class NotifierMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> _listeners = None <EOL> def _add_listener ( self , listener ) : <EOL> """<STR_LIT>""" <EOL> if self . _listeners is None : <EOL> self . _listeners = weakref . WeakValueDictionary ( ) <EOL> self . _listeners [ id ( listener ) ] = listener <EOL> def _remove_listener ( self , listener ) : <EOL> """<STR_LIT>""" <EOL> if self . _listeners is None : <EOL> return <EOL> try : <EOL> del self . _listeners [ id ( listener ) ] <EOL> except KeyError : <EOL> pass <EOL> def _notify ( self , notification , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . _listeners is None : <EOL> return <EOL> method_name = '<STR_LIT>' . format ( notification ) <EOL> for listener in self . _listeners . valuerefs ( ) : <EOL> listener = listener ( ) <EOL> if listener is None : <EOL> continue <EOL> if hasattr ( listener , method_name ) : <EOL> method = getattr ( listener , method_name ) <EOL> if callable ( method ) : <EOL> method ( * args , ** kwargs ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> state = super ( NotifierMixin , self ) . __getstate__ ( ) <EOL> except AttributeError : <EOL> state = self . __dict__ . copy ( ) <EOL> state [ '<STR_LIT>' ] = None <EOL> return state <EOL> def first ( iterable ) : <EOL> """<STR_LIT>""" <EOL> return next ( iter ( iterable ) ) <EOL> def itersubclasses ( cls , _seen = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( cls , type ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % cls ) <EOL> if _seen is None : <EOL> _seen = set ( ) <EOL> try : <EOL> subs = cls . __subclasses__ ( ) <EOL> except TypeError : <EOL> subs = cls . __subclasses__ ( cls ) <EOL> for sub in sorted ( subs , key = lambda s : s . __name__ ) : <EOL> if sub not in _seen : <EOL> _seen . add ( sub ) <EOL> yield sub <EOL> for sub in itersubclasses ( sub , _seen ) : <EOL> yield sub <EOL> class lazyproperty ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fget , fset = None , fdel = None , doc = None ) : <EOL> self . _fget = fget <EOL> self . _fset = fset <EOL> self . _fdel = fdel <EOL> if doc is None : <EOL> self . __doc__ = fget . __doc__ <EOL> else : <EOL> self . __doc__ = doc <EOL> self . _key = self . _fget . __name__ <EOL> def __get__ ( self , obj , owner = None ) : <EOL> if obj is None : <EOL> return self <EOL> try : <EOL> return obj . __dict__ [ self . _key ] <EOL> except KeyError : <EOL> val = self . _fget ( obj ) <EOL> obj . __dict__ [ self . _key ] = val <EOL> return val <EOL> def __set__ ( self , obj , val ) : <EOL> obj_dict = obj . __dict__ <EOL> if self . _fset : <EOL> ret = self . _fset ( obj , val ) <EOL> if ret is not None and obj_dict . get ( self . _key ) is ret : <EOL> return <EOL> obj_dict [ self . _key ] = val <EOL> def __delete__ ( self , obj ) : <EOL> if self . _fdel : <EOL> self . _fdel ( obj ) <EOL> if self . _key in obj . __dict__ : <EOL> del obj . __dict__ [ self . _key ] <EOL> def getter ( self , fget ) : <EOL> return self . __ter ( fget , <NUM_LIT:0> ) <EOL> def setter ( self , fset ) : <EOL> return self . __ter ( fset , <NUM_LIT:1> ) <EOL> def deleter ( self , fdel ) : <EOL> return self . __ter ( fdel , <NUM_LIT:2> ) <EOL> def __ter ( self , f , arg ) : <EOL> args = [ self . _fget , self . _fset , self . _fdel , self . __doc__ ] <EOL> args [ arg ] = f <EOL> cls_ns = sys . _getframe ( <NUM_LIT:1> ) . f_locals <EOL> for k , v in iteritems ( cls_ns ) : <EOL> if v is self : <EOL> property_name = k <EOL> break <EOL> cls_ns [ property_name ] = lazyproperty ( * args ) <EOL> return cls_ns [ property_name ] <EOL> class classproperty ( property ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , fget = None , doc = None , lazy = False ) : <EOL> if fget is None : <EOL> def wrapper ( func ) : <EOL> return cls ( func , lazy = lazy ) <EOL> return wrapper <EOL> return super ( classproperty , cls ) . __new__ ( cls ) <EOL> def __init__ ( self , fget , doc = None , lazy = False ) : <EOL> self . _lazy = lazy <EOL> if lazy : <EOL> self . _cache = { } <EOL> fget = self . _wrap_fget ( fget ) <EOL> super ( classproperty , self ) . __init__ ( fget = fget , doc = doc ) <EOL> if doc is not None : <EOL> self . __doc__ = doc <EOL> def __get__ ( self , obj , objtype = None ) : <EOL> if self . _lazy and objtype in self . _cache : <EOL> return self . _cache [ objtype ] <EOL> if objtype is not None : <EOL> val = self . fget . __wrapped__ ( objtype ) <EOL> else : <EOL> val = super ( classproperty , self ) . __get__ ( obj , objtype = objtype ) <EOL> if self . _lazy : <EOL> if objtype is None : <EOL> objtype = obj . __class__ <EOL> self . _cache [ objtype ] = val <EOL> return val <EOL> def getter ( self , fget ) : <EOL> return super ( classproperty , self ) . getter ( self . _wrap_fget ( fget ) ) <EOL> def setter ( self , fset ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def deleter ( self , fdel ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def _wrap_fget ( orig_fget ) : <EOL> if isinstance ( orig_fget , classmethod ) : <EOL> orig_fget = orig_fget . __func__ <EOL> @ functools . wraps ( orig_fget ) <EOL> def fget ( obj ) : <EOL> return orig_fget ( obj . __class__ ) <EOL> fget . __wrapped__ = orig_fget <EOL> return fget <EOL> class PyfitsDeprecationWarning ( UserWarning ) : <EOL> pass <EOL> class PyfitsPendingDeprecationWarning ( UserWarning ) : <EOL> pass <EOL> def deprecated ( since , message = '<STR_LIT>' , name = '<STR_LIT>' , alternative = '<STR_LIT>' , pending = False ) : <EOL> """<STR_LIT>""" <EOL> def deprecate ( func , message = message , name = name , alternative = alternative , <EOL> pending = pending ) : <EOL> if isinstance ( func , classmethod ) : <EOL> try : <EOL> func = func . __func__ <EOL> except AttributeError : <EOL> method = func . __get__ ( None , object ) <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> func = method . __func__ <EOL> elif hasattr ( method , '<STR_LIT>' ) : <EOL> func = method . im_func <EOL> else : <EOL> return func <EOL> is_classmethod = True <EOL> else : <EOL> is_classmethod = False <EOL> if not name : <EOL> name = func . __name__ <EOL> altmessage = '<STR_LIT>' <EOL> if not message or type ( message ) == type ( deprecate ) : <EOL> if pending : <EOL> message = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> message = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if alternative : <EOL> altmessage = '<STR_LIT>' % alternative <EOL> message = ( ( message % { '<STR_LIT>' : name , '<STR_LIT>' : alternative , <EOL> '<STR_LIT>' : since } ) + altmessage ) <EOL> @ functools . wraps ( func ) <EOL> def deprecated_func ( * args , ** kwargs ) : <EOL> if pending : <EOL> category = PyfitsPendingDeprecationWarning <EOL> else : <EOL> category = PyfitsDeprecationWarning <EOL> warnings . warn ( message , category , stacklevel = <NUM_LIT:2> ) <EOL> return func ( * args , ** kwargs ) <EOL> old_doc = deprecated_func . __doc__ <EOL> if not old_doc : <EOL> old_doc = '<STR_LIT>' <EOL> old_doc = textwrap . dedent ( old_doc ) . strip ( '<STR_LIT:\n>' ) <EOL> altmessage = altmessage . strip ( ) <EOL> if not altmessage : <EOL> altmessage = message . strip ( ) <EOL> new_doc = ( ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> { '<STR_LIT>' : since , '<STR_LIT:message>' : altmessage . strip ( ) } ) + old_doc ) <EOL> if not old_doc : <EOL> new_doc += r'<STR_LIT>' <EOL> deprecated_func . __doc__ = new_doc <EOL> if is_classmethod : <EOL> deprecated_func = classmethod ( deprecated_func ) <EOL> return deprecated_func <EOL> if type ( message ) == type ( deprecate ) : <EOL> return deprecate ( message ) <EOL> return deprecate <EOL> def ignore_sigint ( func ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> curr_thread = threading . currentThread ( ) <EOL> single_thread = ( threading . activeCount ( ) == <NUM_LIT:1> and <EOL> curr_thread . getName ( ) == '<STR_LIT>' ) <EOL> class SigintHandler ( object ) : <EOL> def __init__ ( self ) : <EOL> self . sigint_received = False <EOL> def __call__ ( self , signum , frame ) : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' % func . __name__ ) <EOL> self . sigint_received = True <EOL> sigint_handler = SigintHandler ( ) <EOL> if single_thread : <EOL> old_handler = signal . signal ( signal . SIGINT , sigint_handler ) <EOL> try : <EOL> func ( * args , ** kwargs ) <EOL> finally : <EOL> if single_thread : <EOL> if old_handler is not None : <EOL> signal . signal ( signal . SIGINT , old_handler ) <EOL> else : <EOL> signal . signal ( signal . SIGINT , signal . SIG_DFL ) <EOL> if sigint_handler . sigint_received : <EOL> raise KeyboardInterrupt <EOL> return wrapped <EOL> def pairwise ( iterable ) : <EOL> """<STR_LIT>""" <EOL> a , b = itertools . tee ( iterable ) <EOL> for _ in b : <EOL> break <EOL> return zip ( a , b ) <EOL> def isiterable ( obj ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from collections import Iterable <EOL> if isinstance ( obj , Iterable ) : <EOL> return True <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> iter ( obj ) <EOL> return True <EOL> except TypeError : <EOL> return False <EOL> def encode_ascii ( s ) : <EOL> """<STR_LIT>""" <EOL> return s <EOL> def decode_ascii ( s ) : <EOL> """<STR_LIT>""" <EOL> return s <EOL> def isreadable ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) and f . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> return False <EOL> if hasattr ( f , '<STR_LIT>' ) and not any ( ( c in f . mode for c in '<STR_LIT>' ) ) : <EOL> return False <EOL> return True <EOL> def iswritable ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) and f . closed : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> return False <EOL> if hasattr ( f , '<STR_LIT>' ) and not any ( ( c in f . mode for c in '<STR_LIT>' ) ) : <EOL> return False <EOL> return True <EOL> def isfile ( f ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( f , file ) <EOL> def fileobj_open ( filename , mode ) : <EOL> """<STR_LIT>""" <EOL> return open ( filename , mode ) <EOL> def fileobj_name ( f ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( f , string_types ) : <EOL> return f <EOL> elif isinstance ( f , gzip . GzipFile ) : <EOL> return fileobj_name ( f . fileobj ) <EOL> elif hasattr ( f , '<STR_LIT:name>' ) : <EOL> return f . name <EOL> elif hasattr ( f , '<STR_LIT:filename>' ) : <EOL> return f . filename <EOL> elif hasattr ( f , '<STR_LIT>' ) : <EOL> return str ( f . __class__ ) <EOL> else : <EOL> return str ( type ( f ) ) <EOL> def fileobj_closed ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) : <EOL> return f . closed <EOL> elif hasattr ( f , '<STR_LIT>' ) and hasattr ( f . fileobj , '<STR_LIT>' ) : <EOL> return f . fileobj . closed <EOL> elif hasattr ( f , '<STR_LIT>' ) and hasattr ( f . fp , '<STR_LIT>' ) : <EOL> return f . fp . closed <EOL> else : <EOL> return False <EOL> def fileobj_mode ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) and hasattr ( f . fileobj , '<STR_LIT>' ) : <EOL> fileobj = f . fileobj <EOL> elif hasattr ( f , '<STR_LIT>' ) and hasattr ( f . fp , '<STR_LIT>' ) : <EOL> fileobj = f . fp <EOL> elif hasattr ( f , '<STR_LIT>' ) : <EOL> fileobj = f <EOL> else : <EOL> return None <EOL> return _fileobj_normalize_mode ( fileobj ) <EOL> def _fileobj_normalize_mode ( f ) : <EOL> """<STR_LIT>""" <EOL> mode = f . mode <EOL> if isinstance ( f , gzip . GzipFile ) : <EOL> if mode == gzip . READ : <EOL> return '<STR_LIT:rb>' <EOL> elif mode == gzip . WRITE : <EOL> return '<STR_LIT:wb>' <EOL> else : <EOL> return None <EOL> if '<STR_LIT:+>' in mode : <EOL> mode = mode . replace ( '<STR_LIT:+>' , '<STR_LIT>' ) <EOL> mode += '<STR_LIT:+>' <EOL> if _fileobj_is_append_mode ( f ) and '<STR_LIT:a>' not in mode : <EOL> mode = mode . replace ( '<STR_LIT:r>' , '<STR_LIT:a>' ) . replace ( '<STR_LIT:w>' , '<STR_LIT:a>' ) <EOL> return mode <EOL> def _fileobj_is_append_mode ( f ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:a>' in f . mode : <EOL> return True <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> return False <EOL> try : <EOL> return _is_append_mode_platform ( f . fileno ( ) ) <EOL> except ( ValueError , IOError ) : <EOL> return False <EOL> if sys . platform . startswith ( '<STR_LIT:win32>' ) : <EOL> _sizeof_ioinfo = None <EOL> def _make_is_append_mode ( ) : <EOL> from ctypes import ( cdll , c_size_t , c_void_p , c_int , c_char , <EOL> Structure , POINTER , cast ) <EOL> from ctypes . util import find_msvcrt <EOL> def _dummy_is_append_mode ( fd ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return False <EOL> msvcrt_dll = find_msvcrt ( ) <EOL> if msvcrt_dll is None : <EOL> return _dummy_is_append_mode <EOL> msvcrt = cdll . LoadLibrary ( msvcrt_dll ) <EOL> IOINFO_L2E = <NUM_LIT:5> <EOL> IOINFO_ARRAY_ELTS = <NUM_LIT:1> << IOINFO_L2E <EOL> IOINFO_ARRAYS = <NUM_LIT:64> <EOL> FAPPEND = <NUM_LIT> <EOL> _NO_CONSOLE_FILENO = - <NUM_LIT:2> <EOL> intptr_t = POINTER ( c_int ) <EOL> class my_ioinfo ( Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , intptr_t ) , <EOL> ( '<STR_LIT>' , c_char ) ] <EOL> _msize = msvcrt . _msize <EOL> _msize . argtypes = ( c_void_p , ) <EOL> _msize . restype = c_size_t <EOL> __pioinfo = cast ( msvcrt . __pioinfo , POINTER ( POINTER ( c_char ) ) ) <EOL> global _sizeof_ioinfo <EOL> if __pioinfo [ <NUM_LIT:0> ] is not None : <EOL> _sizeof_ioinfo = _msize ( __pioinfo [ <NUM_LIT:0> ] ) // IOINFO_ARRAY_ELTS <EOL> if not _sizeof_ioinfo : <EOL> return _dummy_is_append_mode <EOL> def _is_append_mode ( fd ) : <EOL> global _sizeof_ioinfo <EOL> if fd != _NO_CONSOLE_FILENO : <EOL> idx1 = fd >> IOINFO_L2E <EOL> idx2 = fd & ( ( <NUM_LIT:1> << IOINFO_L2E ) - <NUM_LIT:1> ) <EOL> if <NUM_LIT:0> <= idx1 < IOINFO_ARRAYS and __pioinfo [ idx1 ] is not None : <EOL> pio = c_void_p ( cast ( __pioinfo [ idx1 ] , c_void_p ) . value + <EOL> idx2 * _sizeof_ioinfo ) <EOL> ioinfo = cast ( pio , POINTER ( my_ioinfo ) ) . contents <EOL> return bool ( ord ( ioinfo . osfile ) & FAPPEND ) <EOL> return False <EOL> return _is_append_mode <EOL> _is_append_mode_platform = _make_is_append_mode ( ) <EOL> del _make_is_append_mode <EOL> else : <EOL> import fcntl <EOL> def _is_append_mode_platform ( fd ) : <EOL> return bool ( fcntl . fcntl ( fd , fcntl . F_GETFL ) & os . O_APPEND ) <EOL> def fileobj_is_binary ( f ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( f , '<STR_LIT>' ) : <EOL> return f . binary <EOL> if io is not None and isinstance ( f , io . TextIOBase ) : <EOL> return False <EOL> mode = fileobj_mode ( f ) <EOL> if mode : <EOL> return '<STR_LIT:b>' in mode <EOL> else : <EOL> return True <EOL> def translate ( s , table , deletechars ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , str ) : <EOL> return s . translate ( table , deletechars ) <EOL> elif isinstance ( s , text_type ) : <EOL> table = dict ( ( x , ord ( table [ x ] ) ) for x in range ( <NUM_LIT> ) <EOL> if ord ( table [ x ] ) != x ) <EOL> for c in deletechars : <EOL> table [ ord ( c ) ] = None <EOL> return s . translate ( table ) <EOL> def indent ( s , shift = <NUM_LIT:1> , width = <NUM_LIT:4> ) : <EOL> indented = '<STR_LIT:\n>' . join ( '<STR_LIT:U+0020>' * ( width * shift ) + l if l else '<STR_LIT>' <EOL> for l in s . splitlines ( ) ) <EOL> if s [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> indented += '<STR_LIT:\n>' <EOL> return indented <EOL> def fill ( text , width , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> paragraphs = text . split ( '<STR_LIT>' ) <EOL> def maybe_fill ( t ) : <EOL> if all ( len ( l ) < width for l in t . splitlines ( ) ) : <EOL> return t <EOL> else : <EOL> return textwrap . fill ( t , width , * args , ** kwargs ) <EOL> return '<STR_LIT>' . join ( maybe_fill ( p ) for p in paragraphs ) <EOL> CHUNKED_FROMFILE = None <EOL> def _array_from_file ( infile , dtype , count , sep ) : <EOL> """<STR_LIT>""" <EOL> if isfile ( infile ) : <EOL> global CHUNKED_FROMFILE <EOL> if CHUNKED_FROMFILE is None : <EOL> if sys . platform == '<STR_LIT>' and LooseVersion ( platform . mac_ver ( ) [ <NUM_LIT:0> ] ) < LooseVersion ( '<STR_LIT>' ) : <EOL> CHUNKED_FROMFILE = True <EOL> else : <EOL> CHUNKED_FROMFILE = False <EOL> if CHUNKED_FROMFILE : <EOL> chunk_size = int ( <NUM_LIT> ** <NUM_LIT:3> / dtype . itemsize ) <EOL> if count < chunk_size : <EOL> return np . fromfile ( infile , dtype = dtype , count = count , sep = sep ) <EOL> else : <EOL> array = np . empty ( count , dtype = dtype ) <EOL> for beg in range ( <NUM_LIT:0> , count , chunk_size ) : <EOL> end = min ( count , beg + chunk_size ) <EOL> array [ beg : end ] = np . fromfile ( infile , dtype = dtype , count = end - beg , sep = sep ) <EOL> return array <EOL> else : <EOL> return np . fromfile ( infile , dtype = dtype , count = count , sep = sep ) <EOL> else : <EOL> read_size = np . dtype ( dtype ) . itemsize * count <EOL> s = infile . read ( read_size ) <EOL> return np . fromstring ( s , dtype = dtype , count = count , sep = sep ) <EOL> _OSX_WRITE_LIMIT = ( <NUM_LIT:2> ** <NUM_LIT:32> ) - <NUM_LIT:1> <EOL> _WIN_WRITE_LIMIT = ( <NUM_LIT:2> ** <NUM_LIT> ) - <NUM_LIT:1> <EOL> def _array_to_file ( arr , outfile ) : <EOL> """<STR_LIT>""" <EOL> if isfile ( outfile ) : <EOL> write = lambda a , f : a . tofile ( f ) <EOL> else : <EOL> write = _array_to_file_like <EOL> if ( sys . platform == '<STR_LIT>' and arr . nbytes >= _OSX_WRITE_LIMIT + <NUM_LIT:1> and <EOL> arr . nbytes % <NUM_LIT> == <NUM_LIT:0> ) : <EOL> chunksize = _OSX_WRITE_LIMIT // arr . itemsize <EOL> elif sys . platform . startswith ( '<STR_LIT>' ) : <EOL> chunksize = _WIN_WRITE_LIMIT // arr . itemsize <EOL> else : <EOL> return write ( arr , outfile ) <EOL> idx = <NUM_LIT:0> <EOL> arr = arr . view ( np . ndarray ) . flatten ( ) <EOL> while idx < arr . nbytes : <EOL> write ( arr [ idx : idx + chunksize ] , outfile ) <EOL> idx += chunksize <EOL> def _array_to_file_like ( arr , fileobj ) : <EOL> """<STR_LIT>""" <EOL> if arr . flags . contiguous : <EOL> fileobj . write ( arr . data ) <EOL> elif hasattr ( np , '<STR_LIT>' ) : <EOL> for item in np . nditer ( arr ) : <EOL> fileobj . write ( item . tostring ( ) ) <EOL> else : <EOL> byteorder = arr . dtype . byteorder <EOL> if ( ( sys . byteorder == '<STR_LIT>' and byteorder == '<STR_LIT:>>' ) <EOL> or ( sys . byteorder == '<STR_LIT>' and byteorder == '<STR_LIT:<>' ) ) : <EOL> for item in arr . flat : <EOL> fileobj . write ( item . byteswap ( ) . tostring ( ) ) <EOL> else : <EOL> for item in arr . flat : <EOL> fileobj . write ( item . tostring ( ) ) <EOL> def _write_string ( f , s ) : <EOL> """<STR_LIT>""" <EOL> binmode = fileobj_is_binary ( f ) <EOL> if binmode and isinstance ( s , text_type ) : <EOL> s = encode_ascii ( s ) <EOL> elif not binmode and not isinstance ( f , text_type ) : <EOL> s = decode_ascii ( s ) <EOL> elif isinstance ( f , StringIO ) and isinstance ( s , np . ndarray ) : <EOL> s = s . data <EOL> f . write ( s ) <EOL> def _convert_array ( array , dtype ) : <EOL> """<STR_LIT>""" <EOL> if array . dtype == dtype : <EOL> return array <EOL> elif ( array . dtype . itemsize == dtype . itemsize and not <EOL> ( np . issubdtype ( array . dtype , np . number ) and <EOL> np . issubdtype ( dtype , np . number ) ) ) : <EOL> return array . view ( dtype ) <EOL> else : <EOL> return array . astype ( dtype ) <EOL> def _unsigned_zero ( dtype ) : <EOL> """<STR_LIT>""" <EOL> assert dtype . kind == '<STR_LIT:u>' <EOL> return <NUM_LIT:1> << ( dtype . itemsize * <NUM_LIT:8> - <NUM_LIT:1> ) <EOL> def _is_pseudo_unsigned ( dtype ) : <EOL> return dtype . kind == '<STR_LIT:u>' and dtype . itemsize >= <NUM_LIT:2> <EOL> def _is_int ( val ) : <EOL> return isinstance ( val , integer_types + ( np . integer , ) ) <EOL> def _str_to_num ( val ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> num = int ( val ) <EOL> except ValueError : <EOL> num = float ( val ) <EOL> return num <EOL> def _pad_length ( stringlen ) : <EOL> """<STR_LIT>""" <EOL> return ( BLOCK_SIZE - ( stringlen % BLOCK_SIZE ) ) % BLOCK_SIZE <EOL> def _words_group ( input , strlen ) : <EOL> """<STR_LIT>""" <EOL> words = [ ] <EOL> nblanks = input . count ( '<STR_LIT:U+0020>' ) <EOL> nmax = max ( nblanks , len ( input ) // strlen + <NUM_LIT:1> ) <EOL> arr = np . fromstring ( ( input + '<STR_LIT:U+0020>' ) , dtype = ( binary_type , <NUM_LIT:1> ) ) <EOL> blank_loc = np . nonzero ( arr == '<STR_LIT:U+0020>' . encode ( '<STR_LIT>' ) ) [ <NUM_LIT:0> ] <EOL> offset = <NUM_LIT:0> <EOL> xoffset = <NUM_LIT:0> <EOL> for idx in range ( nmax ) : <EOL> try : <EOL> loc = np . nonzero ( blank_loc >= strlen + offset ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> offset = blank_loc [ loc - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> if loc == <NUM_LIT:0> : <EOL> offset = - <NUM_LIT:1> <EOL> except : <EOL> offset = len ( input ) <EOL> if offset <= xoffset : <EOL> offset = xoffset + strlen <EOL> words . append ( input [ xoffset : offset ] ) <EOL> if len ( input ) == offset : <EOL> break <EOL> xoffset = offset <EOL> return words <EOL> def _tmp_name ( input ) : <EOL> """<STR_LIT>""" <EOL> if input is not None : <EOL> input = os . path . dirname ( input ) <EOL> f , fn = tempfile . mkstemp ( dir = input ) <EOL> os . close ( f ) <EOL> return fn <EOL> def _get_array_mmap ( array ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( array , mmap . mmap ) : <EOL> return array <EOL> base = array <EOL> while hasattr ( base , '<STR_LIT>' ) and base . base is not None : <EOL> if isinstance ( base . base , mmap . mmap ) : <EOL> return base . base <EOL> base = base . base </s>
