-
-
Notifications
You must be signed in to change notification settings - Fork 329
FSStore: use ensure_bytes()
#1285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3213876
setitem: use ensure_bytes
madsbk 4811181
test
madsbk 5caca83
impl. and use ensure_contiguous_ndarray_or_bytes()
madsbk f983814
Merge branch 'main' of github.com:zarr-developers/zarr-python into FS…
madsbk 3afda2b
Merge branch 'main' into FSStore_ensure_bytes
madsbk cfab20c
Merge branch 'main' into FSStore_ensure_bytes
madsbk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,22 @@ | |
from textwrap import TextWrapper | ||
import mmap | ||
import time | ||
from typing import Any, Callable, Dict, Optional, Tuple, Union | ||
|
||
import numpy as np | ||
from asciitree import BoxStyle, LeftAligned | ||
from asciitree.traversal import Traversal | ||
from collections.abc import Iterable | ||
from numcodecs.compat import ensure_text, ensure_ndarray_like | ||
from numcodecs.compat import ( | ||
ensure_text, | ||
ensure_ndarray_like, | ||
ensure_bytes, | ||
ensure_contiguous_ndarray_like | ||
) | ||
from numcodecs.ndarray_like import NDArrayLike | ||
from numcodecs.registry import codec_registry | ||
from numcodecs.blosc import cbuffer_sizes, cbuffer_metainfo | ||
|
||
from typing import Any, Callable, Dict, Optional, Tuple, Union | ||
|
||
|
||
def flatten(arg: Iterable) -> Iterable: | ||
for element in arg: | ||
|
@@ -696,3 +701,28 @@ def all_equal(value: Any, array: Any): | |
# using == raises warnings from numpy deprecated pattern, but | ||
# using np.equal() raises type errors for structured dtypes... | ||
return np.all(value == array) | ||
|
||
|
||
def ensure_contiguous_ndarray_or_bytes(buf) -> Union[NDArrayLike, bytes]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to move this to |
||
"""Convenience function to coerce `buf` to ndarray-like array or bytes. | ||
|
||
First check if `buf` can be zero-copy converted to a contiguous array. | ||
If not, `buf` will be copied to a newly allocated `bytes` object. | ||
|
||
Parameters | ||
---------- | ||
buf : ndarray-like, array-like, or bytes-like | ||
A numpy array like object such as numpy.ndarray, cupy.ndarray, or | ||
any object exporting a buffer interface. | ||
|
||
Returns | ||
------- | ||
arr : NDArrayLike or bytes | ||
A ndarray-like or bytes object | ||
""" | ||
|
||
try: | ||
return ensure_contiguous_ndarray_like(buf) | ||
except TypeError: | ||
# An error is raised if `buf` couldn't be zero-copy converted | ||
return ensure_bytes(buf) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something we may consider is delaying the instantiation of the
dict
so these copies occur as these values are requested. This can be a follow-on though