本文整理汇总了Python中pyarrow.py_buffer函数的典型用法代码示例。如果您正苦于以下问题:Python py_buffer函数的具体用法?Python py_buffer怎么用?Python py_buffer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了py_buffer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_buffer_equals
def test_buffer_equals():
# Buffer.equals() returns true iff the buffers have the same contents
def eq(a, b):
assert a.equals(b)
assert a == b
assert not (a != b)
def ne(a, b):
assert not a.equals(b)
assert not (a == b)
assert a != b
b1 = b'some data!'
b2 = bytearray(b1)
b3 = bytearray(b1)
b3[0] = 42
buf1 = pa.py_buffer(b1)
buf2 = pa.py_buffer(b2)
buf3 = pa.py_buffer(b2)
buf4 = pa.py_buffer(b3)
buf5 = pa.py_buffer(np.frombuffer(b2, dtype=np.int16))
eq(buf1, buf1)
eq(buf1, buf2)
eq(buf2, buf3)
ne(buf2, buf4)
# Data type is indifferent
eq(buf2, buf5)
示例2: test_buffer_slicing
def test_buffer_slicing():
data = b'some data!'
buf = pa.py_buffer(data)
sliced = buf.slice(2)
expected = pa.py_buffer(b'me data!')
assert sliced.equals(expected)
sliced2 = buf.slice(2, 4)
expected2 = pa.py_buffer(b'me d')
assert sliced2.equals(expected2)
# 0 offset
assert buf.slice(0).equals(buf)
# Slice past end of buffer
assert len(buf.slice(len(buf))) == 0
with pytest.raises(IndexError):
buf.slice(-1)
# Test slice notation
assert buf[2:].equals(buf.slice(2))
assert buf[2:5].equals(buf.slice(2, 3))
assert buf[-5:].equals(buf.slice(len(buf) - 5))
with pytest.raises(IndexError):
buf[::-1]
with pytest.raises(IndexError):
buf[::2]
n = len(buf)
for start in range(-n * 2, n * 2):
for stop in range(-n * 2, n * 2):
assert buf[start:stop].to_pybytes() == buf.to_pybytes()[start:stop]
示例3: test_buffer_from_numpy
def test_buffer_from_numpy():
# C-contiguous
arr = np.arange(12, dtype=np.int8).reshape((3, 4))
buf = pa.py_buffer(arr)
assert buf.to_pybytes() == arr.tobytes()
# F-contiguous; note strides informations is lost
buf = pa.py_buffer(arr.T)
assert buf.to_pybytes() == arr.tobytes()
# Non-contiguous
with pytest.raises(ValueError, match="not contiguous"):
buf = pa.py_buffer(arr.T[::2])
示例4: test_compress_decompress
def test_compress_decompress():
INPUT_SIZE = 10000
test_data = (np.random.randint(0, 255, size=INPUT_SIZE)
.astype(np.uint8)
.tostring())
test_buf = pa.py_buffer(test_data)
codecs = ['lz4', 'snappy', 'gzip', 'zstd', 'brotli']
for codec in codecs:
compressed_buf = pa.compress(test_buf, codec=codec)
compressed_bytes = pa.compress(test_data, codec=codec, asbytes=True)
assert isinstance(compressed_bytes, bytes)
decompressed_buf = pa.decompress(compressed_buf, INPUT_SIZE,
codec=codec)
decompressed_bytes = pa.decompress(compressed_bytes, INPUT_SIZE,
codec=codec, asbytes=True)
assert isinstance(decompressed_bytes, bytes)
assert decompressed_buf.equals(test_buf)
assert decompressed_bytes == test_data
with pytest.raises(ValueError):
pa.decompress(compressed_bytes, codec=codec)
示例5: test_context_from_object
def test_context_from_object(size):
ctx = global_context
arr, cbuf = make_random_buffer(size, target='device')
dtype = arr.dtype
# Creating device buffer from a CUDA host buffer
hbuf = cuda.new_host_buffer(size * arr.dtype.itemsize)
np.frombuffer(hbuf, dtype=dtype)[:] = arr
cbuf2 = ctx.buffer_from_object(hbuf)
assert cbuf2.size == cbuf.size
arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype)
np.testing.assert_equal(arr, arr2)
# Creating device buffer from a device buffer
cbuf2 = ctx.buffer_from_object(cbuf2)
assert cbuf2.size == cbuf.size
arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype)
np.testing.assert_equal(arr, arr2)
# Trying to create a device buffer from a Buffer
with pytest.raises(pa.ArrowTypeError,
match=('buffer is not backed by a CudaBuffer')):
ctx.buffer_from_object(pa.py_buffer(b"123"))
# Trying to create a device buffer from numpy.array
with pytest.raises(pa.ArrowTypeError,
match=('cannot create device buffer view from'
' `<class \'numpy.ndarray\'>` object')):
ctx.buffer_from_object(np.array([1, 2, 3]))
示例6: test_buffer_address
def test_buffer_address():
b1 = b'some data!'
b2 = bytearray(b1)
b3 = bytearray(b1)
buf1 = pa.py_buffer(b1)
buf2 = pa.py_buffer(b1)
buf3 = pa.py_buffer(b2)
buf4 = pa.py_buffer(b3)
assert buf1.address > 0
assert buf1.address == buf2.address
assert buf3.address != buf2.address
assert buf4.address != buf3.address
arr = np.arange(5)
buf = pa.py_buffer(arr)
assert buf.address == arr.ctypes.data
示例7: test_buffer_protocol_respects_immutability
def test_buffer_protocol_respects_immutability():
# ARROW-3228; NumPy's frombuffer ctor determines whether a buffer-like
# object is mutable by first attempting to get a mutable buffer using
# PyObject_FromBuffer. If that fails, it assumes that the object is
# immutable
a = b'12345'
arrow_ref = pa.py_buffer(a)
numpy_ref = np.frombuffer(arrow_ref, dtype=np.uint8)
assert not numpy_ref.flags.writeable
示例8: test_array_from_buffers
def test_array_from_buffers():
values_buf = pa.py_buffer(np.int16([4, 5, 6, 7]))
nulls_buf = pa.py_buffer(np.uint8([0b00001101]))
arr = pa.Array.from_buffers(pa.int16(), 4, [nulls_buf, values_buf])
assert arr.type == pa.int16()
assert arr.to_pylist() == [4, None, 6, 7]
arr = pa.Array.from_buffers(pa.int16(), 4, [None, values_buf])
assert arr.type == pa.int16()
assert arr.to_pylist() == [4, 5, 6, 7]
arr = pa.Array.from_buffers(pa.int16(), 3, [nulls_buf, values_buf],
offset=1)
assert arr.type == pa.int16()
assert arr.to_pylist() == [None, 6, 7]
with pytest.raises(TypeError):
pa.Array.from_buffers(pa.int16(), 3, [u'', u''], offset=1)
示例9: test_buffer_bytes
def test_buffer_bytes():
val = b'some data'
buf = pa.py_buffer(val)
assert isinstance(buf, pa.Buffer)
assert not buf.is_mutable
result = buf.to_pybytes()
assert result == val
示例10: test_buffer_memoryview
def test_buffer_memoryview():
val = b'some data'
buf = pa.py_buffer(val)
assert isinstance(buf, pa.Buffer)
assert not buf.is_mutable
result = memoryview(buf)
assert result == val
示例11: test_buffer_to_numpy
def test_buffer_to_numpy():
# Make sure creating a numpy array from an arrow buffer works
byte_array = bytearray(20)
byte_array[0] = 42
buf = pa.py_buffer(byte_array)
array = np.frombuffer(buf, dtype="uint8")
assert array[0] == byte_array[0]
byte_array[0] += 1
assert array[0] == byte_array[0]
assert array.base == buf
示例12: test_buffer_bytearray
def test_buffer_bytearray():
val = bytearray(b'some data')
buf = pa.py_buffer(val)
assert isinstance(buf, pa.Buffer)
assert buf.is_mutable
result = bytearray(buf)
assert result == val
示例13: test_input_stream_buffer
def test_input_stream_buffer():
data = b"some test data\n" * 10 + b"eof\n"
for arg in [pa.py_buffer(data), memoryview(data)]:
stream = pa.input_stream(arg)
assert stream.read() == data
gz_data = gzip_compress(data)
stream = pa.input_stream(memoryview(gz_data))
assert stream.read() == gz_data
stream = pa.input_stream(memoryview(gz_data), compression='gzip')
assert stream.read() == data
示例14: test_output_stream_buffer
def test_output_stream_buffer():
data = b"some test data\n" * 10 + b"eof\n"
buf = bytearray(len(data))
stream = pa.output_stream(pa.py_buffer(buf))
stream.write(data)
assert buf == data
buf = bytearray(len(data))
stream = pa.output_stream(memoryview(buf))
stream.write(data)
assert buf == data
示例15: test_uninitialized_buffer
def test_uninitialized_buffer():
# ARROW-2039: calling Buffer() directly creates an uninitialized object
check_uninitialized = partial(pytest.raises,
ReferenceError, match="uninitialized")
buf = pa.Buffer()
with check_uninitialized():
buf.size
with check_uninitialized():
len(buf)
with check_uninitialized():
buf.is_mutable
with check_uninitialized():
buf.parent
with check_uninitialized():
buf.to_pybytes()
with check_uninitialized():
memoryview(buf)
with check_uninitialized():
buf.equals(pa.py_buffer(b''))
with check_uninitialized():
pa.py_buffer(b'').equals(buf)