관리-도구
편집 파일: memmap.cpython-37.pyc
B ��Fd�, � @ s� d dl mZmZmZ d dlZddlmZmZm Z d dl mZmZm Z dgZe Zddd d gZd d gZddd d d�ZG dd� de�ZdS ) � )�division�absolute_import�print_functionN� )�uint8�ndarray�dtype)�long� basestring�is_pathlib_path�memmap�r�czr+zw+)�readonlyZcopyonwriteZ readwrite�writec sV e Zd ZdZdZeddddfdd�Zd d � Zdd� Zd� fd d� Z � fdd�Z � ZS )r a� Create a memory-map to an array stored in a *binary* file on disk. Memory-mapped files are used for accessing small segments of large files on disk, without reading the entire file into memory. NumPy's memmap's are array-like objects. This differs from Python's ``mmap`` module, which uses file-like objects. This subclass of ndarray has some unpleasant interactions with some operations, because it doesn't quite fit properly as a subclass. An alternative to using this subclass is to create the ``mmap`` object yourself, then create an ndarray with ndarray.__new__ directly, passing the object created in its 'buffer=' parameter. This class may at some point be turned into a factory function which returns a view into an mmap buffer. Delete the memmap instance to close. Parameters ---------- filename : str, file-like object, or pathlib.Path instance The file name or file object to be used as the array data buffer. dtype : data-type, optional The data-type used to interpret the file contents. Default is `uint8`. mode : {'r+', 'r', 'w+', 'c'}, optional The file is opened in this mode: +------+-------------------------------------------------------------+ | 'r' | Open existing file for reading only. | +------+-------------------------------------------------------------+ | 'r+' | Open existing file for reading and writing. | +------+-------------------------------------------------------------+ | 'w+' | Create or overwrite existing file for reading and writing. | +------+-------------------------------------------------------------+ | 'c' | Copy-on-write: assignments affect data in memory, but | | | changes are not saved to disk. The file on disk is | | | read-only. | +------+-------------------------------------------------------------+ Default is 'r+'. offset : int, optional In the file, array data starts at this offset. Since `offset` is measured in bytes, it should normally be a multiple of the byte-size of `dtype`. When ``mode != 'r'``, even positive offsets beyond end of file are valid; The file will be extended to accommodate the additional data. By default, ``memmap`` will start at the beginning of the file, even if ``filename`` is a file pointer ``fp`` and ``fp.tell() != 0``. shape : tuple, optional The desired shape of the array. If ``mode == 'r'`` and the number of remaining bytes after `offset` is not a multiple of the byte-size of `dtype`, you must specify `shape`. By default, the returned array will be 1-D with the number of elements determined by file size and data-type. order : {'C', 'F'}, optional Specify the order of the ndarray memory layout: :term:`row-major`, C-style or :term:`column-major`, Fortran-style. This only has an effect if the shape is greater than 1-D. The default order is 'C'. Attributes ---------- filename : str or pathlib.Path instance Path to the mapped file. offset : int Offset position in the file. mode : str File mode. Methods ------- flush Flush any changes in memory to file on disk. When you delete a memmap object, flush is called first to write changes to disk before removing the object. See also -------- lib.format.open_memmap : Create or load a memory-mapped ``.npy`` file. Notes ----- The memmap object can be used anywhere an ndarray is accepted. Given a memmap ``fp``, ``isinstance(fp, numpy.ndarray)`` returns ``True``. Memory-mapped files cannot be larger than 2GB on 32-bit systems. When a memmap causes a file to be created or extended beyond its current size in the filesystem, the contents of the new part are unspecified. On systems with POSIX filesystem semantics, the extended part will be filled with zero bytes. Examples -------- >>> data = np.arange(12, dtype='float32') >>> data.resize((3,4)) This example uses a temporary file so that doctest doesn't write files to your directory. You would use a 'normal' filename. >>> from tempfile import mkdtemp >>> import os.path as path >>> filename = path.join(mkdtemp(), 'newfile.dat') Create a memmap with dtype and shape that matches our data: >>> fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4)) >>> fp memmap([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]], dtype=float32) Write data to memmap array: >>> fp[:] = data[:] >>> fp memmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32) >>> fp.filename == path.abspath(filename) True Deletion flushes memory changes to disk before removing the object: >>> del fp Load the memmap and verify data was stored: >>> newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4)) >>> newfp memmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32) Read-only memmap: >>> fpr = np.memmap(filename, dtype='float32', mode='r', shape=(3,4)) >>> fpr.flags.writeable False Copy-on-write memmap: >>> fpc = np.memmap(filename, dtype='float32', mode='c', shape=(3,4)) >>> fpc.flags.writeable True It's possible to assign to copy-on-write array, but values are only written into the memory copy of the array, and not written to disk: >>> fpc memmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32) >>> fpc[0,:] = 0 >>> fpc memmap([[ 0., 0., 0., 0.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32) File on disk is unchanged: >>> fpr memmap([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]], dtype=float32) Offset into a memmap: >>> fpo = np.memmap(filename, dtype='float32', mode='r', offset=16) >>> fpo memmap([ 4., 5., 6., 7., 8., 9., 10., 11.], dtype=float32) g Y�zr+r N�Cc C s� dd l }dd l}yt| }W n4 tk rP |tkrLtdttt�� � ��Y nX t|d�rf|} d} nFt |�r�|� |dkr~dp�|d �} d} nt ||dkr�dp�|d �} d} |d kr�|d kr�td ��| �dd� | �� }t |�}|j} |d k�r"|| }|| �r| �� td��|| }|f}n.t|t��s4|f}d }x|D ]}||9 }�q>W t||| �}|d k�s~|dk�r�||k �r�| �|d d� | �d� | �� |dk�r�|j}n|dk�r�|j}n|j}|||j }||8 }|| }|j | �� |||d�}tj| |||||d�}||_||_||_t|t��rB|j �!|�|_"nHt |��rX|�#� |_"n2t|d��r�t|j$t��r�|j �!|j$�|_"nd |_"| �r�| �� |S )Nr zmode must be one of %s�readFr r �bTzw+zshape must be given� z?Size of available data is not a multiple of the data-type size.r zr+� )�access�offset)r �bufferr �order�name)%�mmap�os.path�mode_equivalents�KeyError�valid_filemodes� ValueError�list�keys�hasattrr �open�seek�tell� dtypedescr�itemsize�close� isinstance�tupler r �flushZACCESS_COPYZACCESS_READZACCESS_WRITEZALLOCATIONGRANULARITY�filenor �__new__�_mmapr �moder �path�abspath�filename�resolver )�subtyper3 r r0 r �shaper r �os�fidZown_fileZflen�descrZ_dbytes�bytes�size�k�acc�startZarray_offset�mm�self� rA �D/opt/alt/python37/lib64/python3.7/site-packages/numpy/core/memmap.pyr. � s� zmemmap.__new__c C sT t |d�r8t�| |�r8|j| _|j| _|j| _|j| _nd | _d | _d | _d | _d S )Nr/ )r# �npZmay_share_memoryr/ r3 r r0 )r@ �objrA rA rB �__array_finalize__! s zmemmap.__array_finalize__c C s$ | j dk r t| j d�r | j �� dS )z� Write any changes in the array to the file on disk. For further information, see `memmap`. Parameters ---------- None See Also -------- memmap Nr, )�baser# r, )r@ rA rA rB r, - s zmemmap.flushc sH t t| ��||�}| |ks&t| �tk r*|S |jdkr<|d S |�tj�S )NrA )�superr �__array_wrap__�typer6 �viewrC r )r@ Zarr�context)� __class__rA rB rH ? s zmemmap.__array_wrap__c s6 t t| ��|�}t|�tkr2|jd kr2|jtd�S |S )N)rI )rG r �__getitem__rI r/ rJ r )r@ �index�res)rL rA rB rM N s zmemmap.__getitem__)N)�__name__� __module__�__qualname__�__doc__Z__array_priority__r r. rE r, rH rM � __classcell__rA rA )rL rB r s 3V)� __future__r r r �numpyrC �numericr r r �numpy.compatr r r �__all__r'