IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
22.3 Slicing a Memmap


22.3 Slicing a Memmap

Once opened, a Memmap object can be sliced into regions.

# Slice m into the buffers "n" and "p" which will correspond to numarray:

>>> n = m[0:16]
>>> n
<MemmapSlice of length:16 writable>

>>> p = m[24:48]
>>> p
<MemmapSlice of length:24 writable>

NOTE: You cannot make overlapping slices of a Memmap:

>>> q = m[20:28]
Traceback (most recent call last):
...
IndexError: Slice overlaps prior slice of same file.

Deletion of a slice is possible once all other references to it are forgotten, e.g. all arrays that used it have themselves been deleted. Deletion of a slice of a Memmap "un-registers" the slice, making that region of the Memmap available for reallocation. Delete directly from the Memmap without referring to the MemmapSlice:

>>> m = Memmap("memmap.tst",mode="w+",len=100)
>>> m1 = m[0:50]
>>> del m[0:50]      # note: delete from m, not m1
>>> m2 = m[0:70]

Note that since the region of m1 was deleted, there is no overlap when m2 is created. However, deleting the region of m1 has invalidated it:

>>> m1
Traceback (most recent call last):
...
RuntimeError: A deleted MemmapSlice has been used.

Don't mix operations on a Memmap which modify its data or file structure with slice deletions. In this case, the status of the modifications is undefined; the underlying map may or may not reflect the modifications after the deletion.

Send comments to the NumArray community.