4
from numeric import uint8, ndarray, dtype
5
from numerictypes import nbytes
8
valid_filemodes = ["r", "c", "r+", "w+"]
9
writeable_filemodes = ["r+","w+"]
18
class memmap(ndarray):
19
__array_priority__ = -100.0
20
def __new__(subtype, name, dtype=uint8, mode='r+', offset=0,
23
mode = mode_equivalents[mode]
25
if mode not in valid_filemodes:
26
raise ValueError("mode must be one of %s" % \
27
(valid_filemodes + mode_equivalents.keys()))
29
fid = file(name, (mode == 'c' and 'r' or mode)+'b')
31
if (mode == 'w+') and shape is None:
32
raise ValueError, "shape must be given"
36
descr = dtypedescr(dtype)
37
_dbytes = descr.itemsize
43
raise ValueError, "Size of available data is not a "\
44
"multiple of data-type size."
45
size = bytes // _dbytes
48
if not isinstance(shape, tuple):
54
bytes = offset + size*_dbytes
56
if mode == 'w+' or (mode == 'r+' and flen < bytes):
62
acc = mmap.ACCESS_COPY
64
acc = mmap.ACCESS_READ
66
acc = mmap.ACCESS_WRITE
68
mm = mmap.mmap(fid.fileno(), bytes, access=acc)
70
self = ndarray.__new__(subtype, shape, dtype=descr, buffer=mm,
71
offset=offset, order=order)
80
def __array_finalize__(self, obj):
81
if obj is not None and not isinstance(obj, memmap):
82
raise ValueError, "Cannot create a memmap array that way"
89
if self._mmap is not None: