L i!dZddlZddlZddlZddlZddlmZddlmZm Z m Z m Z dZ dZ ee dZd Zd Zdd ZGd d eZGddeZGdde ZdZy)z%Numpy pickle compatibility functions.N)BytesIO) _ZFILE_PREFIX Unpickler_ensure_native_byte_order _reconstructc$dj|S)z(Convert an int to an hexadecimal string.z{:#x})format)an_ints `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/joblib/numpy_pickle_compat.pyhex_strr s >>& !!cHt|tr|S|jdS)Nlatin1) isinstancebytesencode)ss r asbytesrs !U 88H rlicz|jdtttz}|j |}|ttd}t |d}|j d}|dk7r|j|t j|j d|}t||k(s Jd|z|S)zRead the z-file and return the content as a string. Z-files are raw data compressed with zlib used internally by joblib for persistence. Backward compatibility is not guaranteed. Do not use for external purposes. rNr zIIncorrect data length while decompressing %s.The file could be corrupted.)seeklenr_MAX_LENreadintzlib decompress) file_handle header_lengthlength next_bytedatas r read_zfiler& sQ &1M   m ,F C &( )F _F  #ID ' ??;++-r6 :D t9  ')4 5  Krc |jttt|}|jt |j t |jtjt ||y)zWrite the data in the given file as a Z-file. Z-files are raw data compressed with zlib used internally by joblib for persistence. Backward compatibility is not guaranteed. Do not use for external purposes. N) writerr rrljustrrcompress)r!r%r*r#s r write_zfiler+BsYm$ SY Fgfll8456dmmGDM8<=rceZdZdZddZdZy)NDArrayWrapperzAn object to be persisted instead of numpy arrays. The only thing this object does, is to carry the filename in which the array has been persisted, and the array subclass. c.||_||_||_yz4Constructor. Store the useful information for later.N)filenamesubclass allow_mmap)selfr0r1r2s r __init__zNDArrayWrapper.__init__[s    $rcJtjj|j|j}t |dd}i}|r|j |d<dtj|jjjvrd|d<|jj|fi|}t|}t|dr`|j|jj|jj fvr(t#|jdd}|j%|S|S)zReconstruct the array.r2T mmap_mode allow_pickle__array_prepare__)rb)ospathjoin_dirnamer0getattrr6inspect signaturenpload parametersrhasattrr1ndarraymemmaprr8)r3 unpicklerr0r2kwargsarray new_arrays r rzNDArrayWrapper.readas77<< 2 2DMMBT<6  "+"5"5F;  W..y||/@/@ALL L&*F> "! !!(5f5*%0 5- .4== LL LL  I 4 %T]]D#>I..u5 5LrN)T__name__ __module__ __qualname____doc__r4rrr r-r-Ts % rr-ceZdZdZdZdZy)ZNDArrayWrapperaTAn object to be persisted instead of numpy arrays. This object store the Zfile filename in which the data array has been persisted, and the meta information to retrieve it. The reason that we store the raw buffer data of the array and the meta information, rather than array representation routine (tobytes) is that it enables us to use completely the strided model to avoid memory copies (a and a.T store as fast). In addition saving the heavy information separately can avoid creating large temporary buffers when unpickling data with large arrays. c.||_||_||_yr/)r0state init_args)r3r0rUrTs r r4zZNDArrayWrapper.__init__s   "rc,tjj|j|j}t |j }t|d5}t|}ddd|jfz}|j||S#1swY,xYw)z?Reconstruct the array from the meta-information and the z-file.rbN) r:r;r<r=r0rrUopenr&rT __setstate__)r3rGr0rIfr%rTs r rzZNDArrayWrapper.reads}77<< 2 2DMMBdnn- (D ! !Qa=D ! dW$ 5!  ! !s  B  BNrKrPrr rRrRs # rrRc|eZdZdZej j ZddZdZdZ e ee jd<y)ZipNumpyUnpicklerz:A subclass of the Unpickler to unpickle our numpy pickles.NcVtjj||_tjj ||_||_|j||_tj||j ddl }||_ y#t$r d}Y||_ ywxYw)z Constructor.rN)r:r;basename _filenamedirnamer=r6 _open_pickler!rr4numpy ImportErrorrA)r3r0r!r6rAs r r4zZipNumpyUnpickler.__init__s))(31 ",,[94!1!12   B sB B('B(c*tt|SN)rr&)r3r!s r razZipNumpyUnpickler._open_picklesz+.//rc$tj|t|jdtr^|j t d|jj}|j|}|jj|yy)zSet the state of a newly created object. We capture it to replace our place-holder objects, NDArrayWrapper, by the array we are interested in. We replace them directly in the stack of pickler. Nz@Trying to unpickle an ndarray, but numpy didn't import correctly) r load_buildrstackr-rArcpoprappend)r3nd_array_wrapperrIs r rhzZipNumpyUnpickler.load_buildsw T" djjnn 5ww!V $zz~~/ $))$/E JJ  e $ 6rrre) rLrMrNrOrdispatchcopyr4rarhpickleBUILDrPrr r\r\s<D!!&&(H 0%"!+HV\\!_rr\cxt|d5}t||} |j} t |dr|jj |cdddS#t$r}t d}||_|d}~wwxYw#t |dr|jjwwxYw#1swYyxYw)a&Reconstruct a Python object from a file persisted with joblib.dump. This function ensures the compatibility with joblib old persistence format (<= 0.9.3). Parameters ---------- filename: string The name of the file from which to load the object Returns ------- result: any Python object The object stored in the file. See Also -------- joblib.dump : function to save an object Notes ----- This function can load numpy array files saved separately during the dump. rW)r!zyYou may be trying to read with python 3 a joblib pickle generated with python 2. This feature is not supported by joblib.Nr!) rXr\rBUnicodeDecodeError ValueError __cause__rDr!close)r0r!rGobjexcnew_excs r load_compatibilityrys4 h  &hKH  ..."Cy-0%%++-)"  ;G !$G M y-0%%++-1%s9B0A(B0 B(A<<BB)B--B00B9)r)rOr?r:roriornumpy_pickle_utilsrrrrr rrr _CHUNK_SIZEr&r+objectr-rRr\ryrPrr r~sw+ "  wu~  D >$*V*ZnD&+ &+R.r