Buffer
Buffer¶
zarr.testing.buffer ¶
__all__
module-attribute
¶
NDBufferUsingTestNDArrayLike ¶
Bases: NDBuffer
Example of a custom NDBuffer that handles MyNDArrayLike
Source code in zarr/testing/buffer.py
__getitem__ ¶
__init__ ¶
__init__(array: NDArrayLike) -> None
__setitem__ ¶
all_equal ¶
Compare to other using np.array_equal.
Source code in zarr/core/buffer/core.py
as_ndarray_like ¶
as_ndarray_like() -> NDArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying array such as a NumPy or CuPy array.–
Source code in zarr/core/buffer/core.py
as_numpy_array ¶
Returns the buffer as a NumPy array (host memory).
Warnings
Might have to copy data, consider using .as_ndarray_like() instead.
Returns:
-
NumPy array of this buffer (might be a data copy)–
Source code in zarr/core/buffer/cpu.py
as_scalar ¶
Returns the buffer as a scalar value
astype ¶
create
classmethod
¶
create(
*,
shape: Iterable[int],
dtype: DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self
Overwrite NDBuffer.create to create a TestNDArrayLike instance
Source code in zarr/testing/buffer.py
empty
classmethod
¶
Create an empty buffer with the given shape, dtype, and order.
This method can be faster than NDBuffer.create because it doesn't
have to initialize the memory used by the underlying ndarray-like
object.
Parameters:
-
shape(tuple[int, ...]) –The shape of the buffer and its underlying ndarray-like object
-
dtype(DTypeLike) –The datatype of the buffer and its underlying ndarray-like object
-
order(Literal['C', 'F'], default:'C') –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
Returns:
-
buffer–New buffer representing a new ndarray_like object with empty data.
See Also
NDBuffer.create Create a new buffer with some initial fill value.
from_ndarray_like
classmethod
¶
from_ndarray_like(ndarray_like: NDArrayLike) -> Self
Create a new buffer of an ndarray-like object
Parameters:
-
ndarray_like(NDArrayLike) –ndarray-like object
Returns:
-
New buffer representing `ndarray_like`–
Source code in zarr/core/buffer/core.py
from_numpy_array
classmethod
¶
Create a new buffer of Numpy array-like object
Parameters:
-
array_like(ArrayLike) –Object that can be coerced into a Numpy array
Returns:
-
New buffer representing `array_like`–
reshape ¶
squeeze ¶
transpose ¶
transpose(
axes: SupportsIndex | Sequence[SupportsIndex] | None,
) -> Self
StoreExpectingTestBuffer ¶
Bases: MemoryStore
Example of a custom Store that expect MyBuffer for all its non-metadata
We assume that keys containing "json" is metadata
Source code in zarr/testing/buffer.py
supports_consolidated_metadata
property
¶
supports_consolidated_metadata: bool
Does the store support consolidated metadata?.
If it doesn't an error will be raised on requests to consolidate the metadata.
Returning False can be useful for stores which implement their own
consolidation mechanism outside of the zarr-python implementation.
supports_deletes
class-attribute
instance-attribute
¶
supports_deletes: bool = True
Does the store support deletes?
supports_listing
class-attribute
instance-attribute
¶
supports_listing: bool = True
Does the store support listing?
supports_partial_writes
property
¶
supports_partial_writes: Literal[False]
Does the store support partial writes?
Partial writes are no longer used by Zarr, so this is always false.
supports_writes
class-attribute
instance-attribute
¶
supports_writes: bool = True
Does the store support writes?
__eq__ ¶
__exit__ ¶
__exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None
__init__ ¶
__init__(
store_dict: MutableMapping[str, Buffer] | None = None,
*,
read_only: bool = False,
) -> None
clear
async
¶
close ¶
delete_dir
async
¶
delete_dir(prefix: str) -> None
Remove all keys and prefixes in the store that begin with a given prefix.
Source code in zarr/abc/store.py
exists
async
¶
get
async
¶
get(
key: str,
prototype: BufferPrototype,
byte_range: tuple[int, int | None] | None = None,
) -> Buffer | None
Retrieve the value associated with a given key.
Parameters:
-
key(str) – -
prototype(BufferPrototype) –The prototype of the output buffer. Stores may support a default buffer prototype.
-
byte_range(ByteRequest, default:None) –ByteRequest may be one of the following. If not provided, all data associated with the key is retrieved. - RangeByteRequest(int, int): Request a specific range of bytes in the form (start, end). The end is exclusive. If the given range is zero-length or starts after the end of the object, an error will be returned. Additionally, if the range ends after the end of the object, the entire remainder of the object will be returned. Otherwise, the exact requested range will be returned. - OffsetByteRequest(int): Request all bytes starting from a given byte offset. This is equivalent to bytes={int}- as an HTTP header. - SuffixByteRequest(int): Request the last int bytes. Note that here, int is the size of the request, not the byte offset. This is equivalent to bytes=-{int} as an HTTP header.
Returns:
-
Buffer–
Source code in zarr/testing/buffer.py
get_partial_values
async
¶
get_partial_values(
prototype: BufferPrototype,
key_ranges: Iterable[tuple[str, ByteRequest | None]],
) -> list[Buffer | None]
Retrieve possibly partial values from given key_ranges.
Parameters:
-
prototype(BufferPrototype) –The prototype of the output buffer. Stores may support a default buffer prototype.
-
key_ranges(Iterable[tuple[str, tuple[int | None, int | None]]]) –Ordered set of key, range pairs, a key may occur multiple times with different ranges
Returns:
-
list of values, in the order of the key_ranges, may contain null/none for missing keys–
Source code in zarr/storage/_memory.py
getsize
async
¶
Return the size, in bytes, of a value in a Store.
Parameters:
-
key(str) –
Returns:
-
nbytes(int) –The size of the value (in bytes).
Raises:
-
FileNotFoundError–When the given key does not exist in the store.
Source code in zarr/abc/store.py
getsize_prefix
async
¶
Return the size, in bytes, of all values under a prefix.
Parameters:
-
prefix(str) –The prefix of the directory to measure.
Returns:
-
nbytes(int) –The sum of the sizes of the values in the directory (in bytes).
See Also
zarr.Array.nbytes_stored Store.getsize
Notes
getsize_prefix is just provided as a potentially faster alternative to
listing all the keys under a prefix calling Store.getsize on each.
In general, prefix should be the path of an Array or Group in the Store.
Implementations may differ on the behavior when some other prefix
is provided.
Source code in zarr/abc/store.py
is_empty
async
¶
Check if the directory is empty.
Parameters:
-
prefix(str) –Prefix of keys to check.
Returns:
-
bool–True if the store is empty, False otherwise.
Source code in zarr/abc/store.py
list
async
¶
list() -> AsyncIterator[str]
list_dir
async
¶
list_dir(prefix: str) -> AsyncIterator[str]
Retrieve all keys and prefixes with a given prefix and which do not contain the character “/” after the given prefix.
Parameters:
-
prefix(str) –
Returns:
-
AsyncIterator[str]–
Source code in zarr/storage/_memory.py
list_prefix
async
¶
list_prefix(prefix: str) -> AsyncIterator[str]
Retrieve all keys in the store that begin with a given prefix. Keys are returned relative to the root of the store.
Parameters:
-
prefix(str) –
Returns:
-
AsyncIterator[str]–
Source code in zarr/storage/_memory.py
open
async
classmethod
¶
Create and open the store.
Parameters:
-
*args(Any, default:()) –Positional arguments to pass to the store constructor.
-
**kwargs(Any, default:{}) –Keyword arguments to pass to the store constructor.
Returns:
-
Store–The opened store instance.
Source code in zarr/abc/store.py
set
async
¶
Store a (key, value) pair.
Parameters:
set_if_not_exists
async
¶
Store a key to value if the key is not already present.
Parameters:
with_read_only ¶
with_read_only(read_only: bool = False) -> MemoryStore
Return a new store with a new read_only setting.
The new store points to the same location with the specified new read_only state. The returned Store is not automatically opened, and this store is not automatically closed.
Parameters:
-
read_only(bool, default:False) –If True, the store will be created in read-only mode. Defaults to False.
Returns:
-
A new store of the same type with the new read only attribute.–
TestBuffer ¶
Bases: Buffer
Example of a custom Buffer that handles ArrayLike
Source code in zarr/testing/buffer.py
__add__ ¶
__eq__ ¶
__getitem__ ¶
__setitem__ ¶
as_array_like ¶
as_array_like() -> ArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying 1d array such as a NumPy or CuPy array.–
Source code in zarr/core/buffer/core.py
as_buffer_like ¶
Returns the buffer as an object that implements the Python buffer protocol.
Notes
Might have to copy data, since the implementation uses .as_numpy_array().
Returns:
-
An object that implements the Python buffer protocol–
Source code in zarr/core/buffer/core.py
as_numpy_array ¶
Returns the buffer as a NumPy array (host memory).
Notes
Might have to copy data, consider using .as_array_like() instead.
Returns:
-
NumPy array of this buffer (might be a data copy)–
Source code in zarr/core/buffer/cpu.py
combine ¶
Concatenate many buffers
Source code in zarr/core/buffer/cpu.py
from_array_like
classmethod
¶
Create a new buffer of an array-like object
Parameters:
-
array_like(ArrayLike) –array-like object that must be 1-dim, contiguous, and byte dtype.
Returns:
-
New buffer representing `array_like`–
Source code in zarr/core/buffer/core.py
from_buffer
classmethod
¶
Create a new buffer of an existing Buffer
This is useful if you want to ensure that an existing buffer is of the correct subclass of Buffer. E.g., MemoryStore uses this to return a buffer instance of the subclass specified by its BufferPrototype argument.
Typically, this only copies data if the data has to be moved between memory types, such as from host to device memory.
Parameters:
-
buffer(Buffer) –buffer object.
Returns:
-
A new buffer representing the content of the input buffer–
Notes
Subclasses of Buffer must override this method to implement
more optimal conversions that avoid copies where possible
Source code in zarr/core/buffer/cpu.py
from_bytes
classmethod
¶
from_bytes(bytes_like: BytesLike) -> Self
Create a new buffer of a bytes-like object (host memory)
Parameters:
-
bytes_like(BytesLike) –bytes-like object
Returns:
-
New buffer representing `bytes_like`–
Source code in zarr/core/buffer/cpu.py
to_bytes ¶
to_bytes() -> bytes
Returns the buffer as bytes (host memory).
Warnings
Will always copy data, only use this method for small buffers such as metadata
buffers. If possible, use .as_numpy_array() or .as_array_like() instead.
Returns:
-
`bytes` of this buffer (data copy)–