本文整理匯總了Python中numpy.compat.isfileobj方法的典型用法代碼示例。如果您正苦於以下問題:Python compat.isfileobj方法的具體用法?Python compat.isfileobj怎麽用?Python compat.isfileobj使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.compat
的用法示例。
在下文中一共展示了compat.isfileobj方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_isfileobj
# 需要導入模塊: from numpy import compat [as 別名]
# 或者: from numpy.compat import isfileobj [as 別名]
def test_isfileobj():
with tempdir(prefix="numpy_test_compat_") as folder:
filename = join(folder, 'a.bin')
with open(filename, 'wb') as f:
assert_(isfileobj(f))
with open(filename, 'ab') as f:
assert_(isfileobj(f))
with open(filename, 'rb') as f:
assert_(isfileobj(f))
示例2: write_array
# 需要導入模塊: from numpy import compat [as 別名]
# 或者: from numpy.compat import isfileobj [as 別名]
def write_array(fp, array, version=(1, 0)):
"""
Write an array to an NPY file, including a header.
If the array is neither C-contiguous nor Fortran-contiguous AND the
file_like object is not a real file object, this function will have to
copy data in memory.
Parameters
----------
fp : file_like object
An open, writable file object, or similar object with a ``.write()``
method.
array : ndarray
The array to write to disk.
version : (int, int), optional
The version number of the format. Default: (1, 0)
Raises
------
ValueError
If the array cannot be persisted.
Various other errors
If the array contains Python objects as part of its dtype, the
process of pickling them may raise various errors if the objects
are not picklable.
"""
if version != (1, 0):
msg = "we only support format version (1,0), not %s"
raise ValueError(msg % (version,))
fp.write(magic(*version))
write_array_header_1_0(fp, header_data_from_array_1_0(array))
if array.dtype.hasobject:
# We contain Python objects so we cannot write out the data directly.
# Instead, we will pickle it out with version 2 of the pickle protocol.
pickle.dump(array, fp, protocol=2)
elif array.flags.f_contiguous and not array.flags.c_contiguous:
if isfileobj(fp):
array.T.tofile(fp)
else:
fp.write(array.T.tostring('C'))
else:
if isfileobj(fp):
array.tofile(fp)
else:
# XXX: We could probably chunk this using something like
# arrayterator.
fp.write(array.tostring('C'))
示例3: write_array
# 需要導入模塊: from numpy import compat [as 別名]
# 或者: from numpy.compat import isfileobj [as 別名]
def write_array(fp, array, version=None):
"""
Write an array to an NPY file, including a header.
If the array is neither C-contiguous nor Fortran-contiguous AND the
file_like object is not a real file object, this function will have to
copy data in memory.
Parameters
----------
fp : file_like object
An open, writable file object, or similar object with a
``.write()`` method.
array : ndarray
The array to write to disk.
version : (int, int) or None, optional
The version number of the format. None means use the oldest
supported version that is able to store the data. Default: None
Raises
------
ValueError
If the array cannot be persisted.
Various other errors
If the array contains Python objects as part of its dtype, the
process of pickling them may raise various errors if the objects
are not picklable.
"""
_check_version(version)
used_ver = _write_array_header(fp, header_data_from_array_1_0(array),
version)
# this warning can be removed when 1.9 has aged enough
if version != (2, 0) and used_ver == (2, 0):
warnings.warn("Stored array in format 2.0. It can only be"
"read by NumPy >= 1.9", UserWarning)
# Set buffer size to 16 MiB to hide the Python loop overhead.
buffersize = max(16 * 1024 ** 2 // array.itemsize, 1)
if array.dtype.hasobject:
# We contain Python objects so we cannot write out the data
# directly. Instead, we will pickle it out with version 2 of the
# pickle protocol.
pickle.dump(array, fp, protocol=2)
elif array.flags.f_contiguous and not array.flags.c_contiguous:
if isfileobj(fp):
array.T.tofile(fp)
else:
for chunk in numpy.nditer(
array, flags=['external_loop', 'buffered', 'zerosize_ok'],
buffersize=buffersize, order='F'):
fp.write(chunk.tobytes('C'))
else:
if isfileobj(fp):
array.tofile(fp)
else:
for chunk in numpy.nditer(
array, flags=['external_loop', 'buffered', 'zerosize_ok'],
buffersize=buffersize, order='C'):
fp.write(chunk.tobytes('C'))