MCPcopy Create free account
hub / github.com/numpy/numpy / memmap

Class memmap

numpy/core/memmap.py:23–338  ·  view source on GitHub ↗

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, wh

Source from the content-addressed store, hash-verified

21
22@set_module('numpy')
23class memmap(ndarray):
24 """Create a memory-map to an array stored in a *binary* file on disk.
25
26 Memory-mapped files are used for accessing small segments of large files
27 on disk, without reading the entire file into memory. NumPy's
28 memmap's are array-like objects. This differs from Python's ``mmap``
29 module, which uses file-like objects.
30
31 This subclass of ndarray has some unpleasant interactions with
32 some operations, because it doesn't quite fit properly as a subclass.
33 An alternative to using this subclass is to create the ``mmap``
34 object yourself, then create an ndarray with ndarray.__new__ directly,
35 passing the object created in its 'buffer=' parameter.
36
37 This class may at some point be turned into a factory function
38 which returns a view into an mmap buffer.
39
40 Flush the memmap instance to write the changes to the file. Currently there
41 is no API to close the underlying ``mmap``. It is tricky to ensure the
42 resource is actually closed, since it may be shared between different
43 memmap instances.
44
45
46 Parameters
47 ----------
48 filename : str, file-like object, or pathlib.Path instance
49 The file name or file object to be used as the array data buffer.
50 dtype : data-type, optional
51 The data-type used to interpret the file contents.
52 Default is `uint8`.
53 mode : {'r+', 'r', 'w+', 'c'}, optional
54 The file is opened in this mode:
55
56 +------+-------------------------------------------------------------+
57 | 'r' | Open existing file for reading only. |
58 +------+-------------------------------------------------------------+
59 | 'r+' | Open existing file for reading and writing. |
60 +------+-------------------------------------------------------------+
61 | 'w+' | Create or overwrite existing file for reading and writing. |
62 | | If ``mode == 'w+'`` then `shape` must also be specified. |
63 +------+-------------------------------------------------------------+
64 | 'c' | Copy-on-write: assignments affect data in memory, but |
65 | | changes are not saved to disk. The file on disk is |
66 | | read-only. |
67 +------+-------------------------------------------------------------+
68
69 Default is 'r+'.
70 offset : int, optional
71 In the file, array data starts at this offset. Since `offset` is
72 measured in bytes, it should normally be a multiple of the byte-size
73 of `dtype`. When ``mode != 'r'``, even positive offsets beyond end of
74 file are valid; The file will be extended to accommodate the
75 additional data. By default, ``memmap`` will start at the beginning of
76 the file, even if ``filename`` is a file pointer ``fp`` and
77 ``fp.tell() != 0``.
78 shape : tuple, optional
79 The desired shape of the array. If ``mode == 'r'`` and the number
80 of remaining bytes after `offset` is not a multiple of the byte-size

Callers 15

test_roundtripMethod · 0.90
test_unnamed_fileMethod · 0.90
test_attributesMethod · 0.90
test_filenameMethod · 0.90
test_pathMethod · 0.90
test_filename_fileobjMethod · 0.90
test_flushMethod · 0.90
test_delMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_roundtripMethod · 0.72
test_unnamed_fileMethod · 0.72
test_attributesMethod · 0.72
test_filenameMethod · 0.72
test_pathMethod · 0.72
test_filename_fileobjMethod · 0.72
test_flushMethod · 0.72
test_delMethod · 0.72