Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/4000.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run all doctests via pytest and fix all broken doctests.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ ignore_errors = true

[tool.pytest.ini_options]
minversion = "7"
testpaths = ["tests", "docs/user-guide"]
testpaths = ["src", "tests", "docs/user-guide"]
log_cli_level = "INFO"
log_level = "INFO"
xfail_strict = true
Expand All @@ -430,6 +430,9 @@ addopts = [
"--benchmark-disable", # benchmark routines run as tests without benchmarking instrumentation
"--durations", "10",
"-ra", "--strict-config", "--strict-markers",
"--doctest-modules",
"--ignore=tests/test_regression/scripts",
"--ignore=src/zarr/_cli",
]
filterwarnings = [
"error",
Expand Down
47 changes: 32 additions & 15 deletions src/zarr/abc/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,20 @@ async def _get_bytes(

Examples
--------
>>> store = await MemoryStore.open()
>>> await store.set("data", Buffer.from_bytes(b"hello world"))
>>> data = await store.get_bytes("data", prototype=default_buffer_prototype())
>>> print(data)
>>> async def example():
... from zarr.core.buffer.cpu import Buffer
... from zarr.storage import MemoryStore
...
... store = await MemoryStore.open()
... await store.set("data", Buffer.from_bytes(b"hello world"))
... # No need to specify prototype for MemoryStore
... return await store._get_bytes("data")

>>> import asyncio
>>> asyncio.run(example())
b'hello world'
"""

buffer = await self.get(key, prototype, byte_range)
if buffer is None:
raise FileNotFoundError(key)
Expand Down Expand Up @@ -309,10 +317,11 @@ def _get_bytes_sync(

Examples
--------
>>> from zarr.core.buffer.cpu import Buffer
>>> from zarr.storage import MemoryStore
>>> store = MemoryStore()
>>> await store.set("data", Buffer.from_bytes(b"hello world"))
>>> data = store.get_bytes_sync("data", prototype=default_buffer_prototype())
>>> print(data)
>>> store.set_sync("data", Buffer.from_bytes(b"hello world"))
>>> store._get_bytes_sync("data") # No need to specify prototype for MemoryStore
b'hello world'
"""

Expand Down Expand Up @@ -358,11 +367,18 @@ async def _get_json(

Examples
--------
>>> store = await MemoryStore.open()
>>> metadata = {"zarr_format": 3, "node_type": "array"}
>>> await store.set("zarr.json", Buffer.from_bytes(json.dumps(metadata).encode()))
>>> data = await store.get_json("zarr.json", prototype=default_buffer_prototype())
>>> print(data)
>>> async def example():
... from zarr.core.buffer.cpu import Buffer
... from zarr.storage import MemoryStore
...
... store = await MemoryStore.open()
... metadata = {"zarr_format": 3, "node_type": "array"}
... await store.set("zarr.json", Buffer.from_bytes(json.dumps(metadata).encode()))
... # No need to specify prototype for MemoryStore
... return await store._get_json("zarr.json")

>>> import asyncio
>>> asyncio.run(example())
{'zarr_format': 3, 'node_type': 'array'}
"""

Expand Down Expand Up @@ -414,11 +430,12 @@ def _get_json_sync(

Examples
--------
>>> from zarr.core.buffer.cpu import Buffer
>>> from zarr.storage import MemoryStore
>>> store = MemoryStore()
>>> metadata = {"zarr_format": 3, "node_type": "array"}
>>> store.set("zarr.json", Buffer.from_bytes(json.dumps(metadata).encode()))
>>> data = store.get_json_sync("zarr.json", prototype=default_buffer_prototype())
>>> print(data)
>>> store.set_sync("zarr.json", Buffer.from_bytes(json.dumps(metadata).encode()))
>>> store._get_json_sync("zarr.json") # No need to specify prototype for MemoryStore
{'zarr_format': 3, 'node_type': 'array'}
"""

Expand Down
76 changes: 29 additions & 47 deletions src/zarr/api/synchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,63 +1126,45 @@ def from_array(
--------
Create an array from an existing Array:

```python
import zarr
store = zarr.storage.MemoryStore()
store2 = zarr.storage.LocalStore('example_from_array.zarr')
arr = zarr.create_array(
store=store,
shape=(100,100),
chunks=(10,10),
dtype='int32',
fill_value=0)
arr2 = zarr.from_array(store2, data=arr, overwrite=True)
# <Array file://example_from_array.zarr shape=(100, 100) dtype=int32>
```
>>> import asyncio
>>> import zarr
>>> store = zarr.storage.LocalStore("example_from_array.zarr")
>>> arr = zarr.create_array(
... store={},
... shape=(100,100),
... chunks=(10,10),
... dtype="int32",
... fill_value=0
... )
>>> arr2 = zarr.from_array(store, data=arr, overwrite=True)
>>> arr2
<Array file://example_from_array.zarr shape=(100, 100) dtype=int32>
>>> asyncio.run(store.clear()) # Remove files generated by test

Create an array from an existing NumPy array:

```python
import zarr
import numpy as np
arr3 = zarr.from_array(
zarr.storage.MemoryStore(),
data=np.arange(10000, dtype='i4').reshape(100, 100),
)
# <Array memory://... shape=(100, 100) dtype=int32>
```
>>> import numpy as np
>>> zarr.from_array({}, data=np.arange(10000, dtype="i4").reshape(100, 100))
<Array memory://... shape=(100, 100) dtype=int32>

Create an array from any array-like object:

```python
import zarr
arr4 = zarr.from_array(
zarr.storage.MemoryStore(),
data=[[1, 2], [3, 4]],
)
# <Array memory://... shape=(2, 2) dtype=int64>
arr4[...]
# array([[1, 2],[3, 4]])
```
>>> arr3 = zarr.from_array({}, data=[[1, 2], [3, 4]])
>>> arr3
<Array memory://... shape=(2, 2) dtype=int64>
>>> arr3[...]
array([[1, 2], [3, 4]])

Create an array from an existing Array without copying the data:

```python
import zarr
arr4 = zarr.from_array(
zarr.storage.MemoryStore(),
data=[[1, 2], [3, 4]],
)
arr5 = zarr.from_array(
zarr.storage.MemoryStore(),
data=arr4,
write_data=False,
)
# <Array memory://... shape=(2, 2) dtype=int64>
arr5[...]
# array([[0, 0],[0, 0]])
```
>>> arr4 = zarr.from_array({}, data=[[1, 2], [3, 4]])
>>> arr5 = zarr.from_array({}, data=arr4, write_data=False)
>>> arr5
<Array memory://... shape=(2, 2) dtype=int64>
>>> arr5[...]
array([[0, 0], [0, 0]])
"""

return Array(
sync(
zarr.core.array.from_array(
Expand Down
16 changes: 9 additions & 7 deletions src/zarr/codecs/numcodecs/_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import zarr
import zarr.codecs.numcodecs as numcodecs

store = zarr.storage.MemoryStore()
array = zarr.create_array(
store="data_numcodecs.zarr",
shape=(1024, 1024),
chunks=(64, 64),
dtype="uint32",
filters=[numcodecs.Delta(dtype="uint32")],
compressors=[numcodecs.BZ2(level=5)],
overwrite=True)
store=store,
shape=(1024, 1024),
chunks=(64, 64),
dtype="uint32",
filters=[numcodecs.Delta(dtype="uint32")],
compressors=[numcodecs.BZ2(level=5)],
overwrite=True
)
array[:] = np.arange(np.prod(array.shape), dtype=array.dtype).reshape(*array.shape)
```

Expand Down
Loading
Loading