本文整理汇总了Python中mmap.mmap方法的典型用法代码示例。如果您正苦于以下问题:Python mmap.mmap方法的具体用法?Python mmap.mmap怎么用?Python mmap.mmap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mmap
的用法示例。
在下文中一共展示了mmap.mmap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def __init__(self, path, mode):
assert mode in ("r", "w")
self.path = path
self.file = open(self.path, mode + "b")
self.mmfile = None
self.mode = mode
self.cnt = _CacheFileContent()
if mode == "r":
# Read the last part of the file which contains the contents of the
# cache.
self.cnt.read(self.file, self.path)
self.mmfile = mmap.mmap(self.file.fileno(), 0, access=mmap.ACCESS_READ)
else:
# The 8 bytes header contain the position of the content index.
# We fill this header with zeroes and write the actual position
# once all samples have been written to the cache
self.file.write(struct.pack('<Q', 0))
示例2: startFunction
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def startFunction(self):
""" Return start function, attempt to find it in vba files if _startFunction is not set """
result = None
if self._startFunction is not None:
result = self._startFunction
else:
vbaFiles = self.getVBAFiles()
for vbaFile in vbaFiles:
if os.stat(vbaFile).st_size != 0:
with open(vbaFile, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
for potentialStartFunction in self.potentialStartFunctions:
if s.find(potentialStartFunction.encode()) != -1:
self._startFunction = potentialStartFunction
if self._startFunction not in self.reservedFunctions:
self.reservedFunctions.append(self._startFunction)
result = potentialStartFunction
break
return result
示例3: getMainVBAFile
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def getMainVBAFile(self):
""" return main vba file (the one containing macro entry point) """
result = ""
vbaFiles = self.getVBAFiles()
if len(vbaFiles)==1:
result = vbaFiles[0]
else:
if self.startFunction is not None:
for vbaFile in vbaFiles:
if os.stat(vbaFile).st_size != 0:
with open(vbaFile, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(self.startFunction.encode()) != -1:
result = vbaFile
break
return result
示例4: _makefile
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def _makefile(self):
import tempfile
import mmap
if self.size == 0:
return bytearray()
# posix version
if self.location is None or not os.path.exists(self.location):
if self.location is None:
fd = tempfile.TemporaryFile()
self.blocks = set()
else:
fd = io.open(self.location, "wb+")
fd.seek(self.size - 1)
fd.write(b"1")
fd.flush()
else:
fd = io.open(self.location, "rb+")
return mmap.mmap(fd.fileno(), self.size)
示例5: remoteSceneChanged
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def remoteSceneChanged(self, data):
w, h, size, newfile = data
#self._sizeHint = (whint, hhint)
if self.shm is None or self.shm.size != size:
if self.shm is not None:
self.shm.close()
if sys.platform.startswith('win'):
self.shmtag = newfile ## on windows, we create a new tag for every resize
self.shm = mmap.mmap(-1, size, self.shmtag) ## can't use tmpfile on windows because the file can only be opened once.
elif sys.platform == 'darwin':
self.shmFile.close()
self.shmFile = open(self._view.shmFileName(), 'r')
self.shm = mmap.mmap(self.shmFile.fileno(), size, mmap.MAP_SHARED, mmap.PROT_READ)
else:
self.shm = mmap.mmap(self.shmFile.fileno(), size, mmap.MAP_SHARED, mmap.PROT_READ)
self.shm.seek(0)
data = self.shm.read(w*h*4)
self._img = QtGui.QImage(data, w, h, QtGui.QImage.Format_ARGB32)
self._img.data = data # data must be kept alive or PySide 1.2.1 (and probably earlier) will crash.
self.update()
示例6: __init__
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def __init__(self, *args, **kwds):
## Create shared memory for rendered image
#pg.dbg(namespace={'r': self})
if sys.platform.startswith('win'):
self.shmtag = "pyqtgraph_shmem_" + ''.join([chr((random.getrandbits(20)%25) + 97) for i in range(20)])
self.shm = mmap.mmap(-1, mmap.PAGESIZE, self.shmtag) # use anonymous mmap on windows
else:
self.shmFile = tempfile.NamedTemporaryFile(prefix='pyqtgraph_shmem_')
self.shmFile.write(b'\x00' * (mmap.PAGESIZE+1))
fd = self.shmFile.fileno()
self.shm = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
atexit.register(self.close)
GraphicsView.__init__(self, *args, **kwds)
self.scene().changed.connect(self.update)
self.img = None
self.renderTimer = QtCore.QTimer()
self.renderTimer.timeout.connect(self.renderView)
self.renderTimer.start(16)
示例7: test_ensure_contiguous_ndarray_shares_memory
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def test_ensure_contiguous_ndarray_shares_memory():
typed_bufs = [
('u', 1, b'adsdasdas'),
('u', 1, bytes(20)),
('i', 8, np.arange(100, dtype=np.int64)),
('f', 8, np.linspace(0, 1, 100, dtype=np.float64)),
('i', 4, array.array('i', b'qwertyuiqwertyui')),
('u', 4, array.array('I', b'qwertyuiqwertyui')),
('f', 4, array.array('f', b'qwertyuiqwertyui')),
('f', 8, array.array('d', b'qwertyuiqwertyui')),
('i', 1, array.array('b', b'qwertyuiqwertyui')),
('u', 1, array.array('B', b'qwertyuiqwertyui')),
('u', 1, mmap.mmap(-1, 10))
]
for expected_kind, expected_itemsize, buf in typed_bufs:
a = ensure_contiguous_ndarray(buf)
assert isinstance(a, np.ndarray)
assert expected_kind == a.dtype.kind
if isinstance(buf, array.array):
assert buf.itemsize == a.dtype.itemsize
else:
assert expected_itemsize == a.dtype.itemsize
assert np.shares_memory(a, memoryview(buf))
示例8: close
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def close(self):
"""Closes the NetCDF file."""
if hasattr(self, 'fp') and not self.fp.closed:
try:
self.flush()
finally:
self.variables = OrderedDict()
if self._mm_buf is not None:
ref = weakref.ref(self._mm_buf)
self._mm_buf = None
if ref() is None:
# self._mm_buf is gc'd, and we can close the mmap
self._mm.close()
else:
# we cannot close self._mm, since self._mm_buf is
# alive and there may still be arrays referring to it
warnings.warn((
"Cannot close a netcdf_file opened with mmap=True, when "
"netcdf_variables or arrays referring to its data still exist. "
"All data arrays obtained from such files refer directly to "
"data on disk, and must be copied before the file can be cleanly "
"closed. (See netcdf_file docstring for more information on mmap.)"
), category=RuntimeWarning)
self._mm = None
self.fp.close()
示例9: malloc
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def malloc(self, size):
# return a block of right size (possibly rounded up)
assert 0 <= size < sys.maxint
if os.getpid() != self._lastpid:
self.__init__() # reinitialize after fork
self._lock.acquire()
self._free_pending_blocks()
try:
size = self._roundup(max(size,1), self._alignment)
(arena, start, stop) = self._malloc(size)
new_stop = start + size
if new_stop < stop:
self._free((arena, new_stop, stop))
block = (arena, start, new_stop)
self._allocated_blocks.add(block)
return block
finally:
self._lock.release()
#
# Class representing a chunk of an mmap -- can be inherited
#
示例10: test_main_gh1081
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def test_main_gh1081(self):
"""https://github.com/IronLanguages/main/issues/1081"""
import io
import mmap
test_file_name = os.path.join(self.temporary_dir, "test_main_gh1081.bin")
with open(test_file_name, "wb") as f:
f.write(bytearray(range(256)))
try:
with io.open(test_file_name, "rb") as f:
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
try:
self.assertEqual(mm[:], bytearray(range(256)))
finally:
mm.close()
finally:
os.remove(test_file_name)
示例11: test_file_handles
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def test_file_handles(self):
# GH 14418 - don't close user provided file handles
fh = StringIO('a,b\n1,2')
self.read_csv(fh)
assert not fh.closed
with open(self.csv1, 'r') as f:
self.read_csv(f)
assert not f.closed
# mmap not working with python engine
if self.engine != 'python':
import mmap
with open(self.csv1, 'r') as f:
m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
self.read_csv(m)
# closed attribute new in python 3.2
if PY3:
assert not m.closed
m.close()
示例12: __init__
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def __init__(self, size):
""" This class allocates continuous memory with specified size, lock it
and provide access to it with Python's mmap. It uses RPi video
buffers to allocate it (/dev/vcio).
:param size: number of bytes to allocate
"""
size = (size + PAGE_SIZE - 1) // PAGE_SIZE * PAGE_SIZE
self._vcio_fd = self._open_dev("/dev/vcio")
# allocate memory
self._handle = self._send_data(0x3000c, [size, PAGE_SIZE, 0xC])
if self._handle == 0:
raise OSError("No memory to allocate with /dev/vcio")
# lock memory
self._bus_memory = self._send_data(0x3000d, [self._handle])
if self._bus_memory == 0:
# memory should be freed in __del__
raise OSError("Failed to lock memory with /dev/vcio")
# print("allocate {} at {} (bus {})".format(size,
# hex(self.get_phys_address()), hex(self.get_bus_address())))
super(CMAPhysicalMemory, self).__init__(self.get_phys_address(), size)
atexit.register(self.free)
示例13: save
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def save(self, fname='panns.idx', mmap=True):
"""
The function saves the index in a file using cPickle.
Parameters:
fname: the index file name.
mmap: enable mmap or not. Enable it if mem is small.
"""
f = open(fname, 'wb')
pickle.dump(self.get_basic_info(), f, -1)
logger.info('dump binary trees to %s ...' % fname)
for tree in self.btr:
pickle.dump(tree, f, -1)
logger.info('dump raw dataset to %s ...' % (fname+'.npy'))
if mmap:
make_mmap(self.mtx, (len(self.mtx),self.dim), self.typ, fname+'.npy')
else:
numpy.save(open(fname+'.npy', 'wb'), self.mtx)
pass
示例14: __init__
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def __init__(self, vocab_filename, rows_filename, cols_filename=None):
"""Initializes the vectors from a text vocabulary and binary data."""
with open(vocab_filename, 'r') as lines:
self.vocab = [line.split()[0] for line in lines]
self.word_to_idx = {word: idx for idx, word in enumerate(self.vocab)}
n = len(self.vocab)
with open(rows_filename, 'r') as rows_fh:
rows_fh.seek(0, os.SEEK_END)
size = rows_fh.tell()
# Make sure that the file size seems reasonable.
if size % (4 * n) != 0:
raise IOError(
'unexpected file size for binary vector file %s' % rows_filename)
# Memory map the rows.
dim = size / (4 * n)
rows_mm = mmap.mmap(rows_fh.fileno(), 0, prot=mmap.PROT_READ)
rows = np.matrix(
np.frombuffer(rows_mm, dtype=np.float32).reshape(n, dim))
# If column vectors were specified, then open them and add them to the row
# vectors.
if cols_filename:
with open(cols_filename, 'r') as cols_fh:
cols_mm = mmap.mmap(cols_fh.fileno(), 0, prot=mmap.PROT_READ)
cols_fh.seek(0, os.SEEK_END)
if cols_fh.tell() != size:
raise IOError('row and column vector files have different sizes')
cols = np.matrix(
np.frombuffer(cols_mm, dtype=np.float32).reshape(n, dim))
rows += cols
cols_mm.close()
# Normalize so that dot products are just cosine similarity.
self.vecs = rows / np.linalg.norm(rows, axis=1).reshape(n, 1)
rows_mm.close()
示例15: __init__
# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import mmap [as 别名]
def __init__(self, filename):
self._filename = filename
self._f = open(filename, "rb")
# memory-map the file, size 0 means whole file
self._mapped = mmap.mmap(self._f.fileno(), 0, access=mmap.ACCESS_COPY)
super(FileDataModel, self).__init__(self._mapped)