Skip to content

Commit 584acdd

Browse files
committed
make docstring code examples executible, and fix errors
1 parent ef6303e commit 584acdd

File tree

12 files changed

+289
-205
lines changed

12 files changed

+289
-205
lines changed

src/zarr/api/synchronous.py

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -946,15 +946,17 @@ def create_array(
946946
947947
Examples
948948
--------
949-
>>> import zarr
950-
>>> store = zarr.storage.MemoryStore()
951-
>>> arr = await zarr.create_array(
952-
>>> store=store,
953-
>>> shape=(100,100),
954-
>>> chunks=(10,10),
955-
>>> dtype='i4',
956-
>>> fill_value=0)
957-
<Array memory://140349042942400 shape=(100, 100) dtype=int32>
949+
```python
950+
import zarr
951+
store = zarr.storage.MemoryStore()
952+
arr = zarr.create_array(
953+
store=store,
954+
shape=(100,100),
955+
chunks=(10,10),
956+
dtype='i4',
957+
fill_value=0)
958+
# <Array memory://... shape=(100, 100) dtype=int32>
959+
```
958960
"""
959961
return Array(
960962
sync(
@@ -1132,49 +1134,64 @@ def from_array(
11321134
11331135
Examples
11341136
--------
1135-
Create an array from an existing Array::
1136-
1137-
>>> import zarr
1138-
>>> store = zarr.storage.MemoryStore()
1139-
>>> store2 = zarr.storage.LocalStore('example.zarr')
1140-
>>> arr = zarr.create_array(
1141-
>>> store=store,
1142-
>>> shape=(100,100),
1143-
>>> chunks=(10,10),
1144-
>>> dtype='int32',
1145-
>>> fill_value=0)
1146-
>>> arr2 = zarr.from_array(store2, data=arr)
1147-
<Array file://example.zarr shape=(100, 100) dtype=int32>
1148-
1149-
Create an array from an existing NumPy array::
1150-
1151-
>>> import numpy as np
1152-
>>> arr3 = zarr.from_array(
1153-
zarr.storage.MemoryStore(),
1154-
>>> data=np.arange(10000, dtype='i4').reshape(100, 100),
1155-
>>> )
1156-
<Array memory://125477403529984 shape=(100, 100) dtype=int32>
1157-
1158-
Create an array from any array-like object::
1159-
1160-
>>> arr4 = zarr.from_array(
1161-
>>> zarr.storage.MemoryStore(),
1162-
>>> data=[[1, 2], [3, 4]],
1163-
>>> )
1164-
<Array memory://125477392154368 shape=(2, 2) dtype=int64>
1165-
>>> arr4[...]
1166-
array([[1, 2],[3, 4]])
1167-
1168-
Create an array from an existing Array without copying the data::
1169-
1170-
>>> arr5 = zarr.from_array(
1171-
>>> zarr.storage.MemoryStore(),
1172-
>>> data=arr4,
1173-
>>> write_data=False,
1174-
>>> )
1175-
<Array memory://140678602965568 shape=(2, 2) dtype=int64>
1176-
>>> arr5[...]
1177-
array([[0, 0],[0, 0]])
1137+
Create an array from an existing Array:
1138+
1139+
```python
1140+
import zarr
1141+
store = zarr.storage.MemoryStore()
1142+
store2 = zarr.storage.LocalStore('example_from_array.zarr')
1143+
arr = zarr.create_array(
1144+
store=store,
1145+
shape=(100,100),
1146+
chunks=(10,10),
1147+
dtype='int32',
1148+
fill_value=0)
1149+
arr2 = zarr.from_array(store2, data=arr, overwrite=True)
1150+
# <Array file://example_from_array.zarr shape=(100, 100) dtype=int32>
1151+
```
1152+
1153+
Create an array from an existing NumPy array:
1154+
1155+
```python
1156+
import zarr
1157+
import numpy as np
1158+
arr3 = zarr.from_array(
1159+
zarr.storage.MemoryStore(),
1160+
data=np.arange(10000, dtype='i4').reshape(100, 100),
1161+
)
1162+
# <Array memory://... shape=(100, 100) dtype=int32>
1163+
```
1164+
1165+
Create an array from any array-like object:
1166+
1167+
```python
1168+
import zarr
1169+
arr4 = zarr.from_array(
1170+
zarr.storage.MemoryStore(),
1171+
data=[[1, 2], [3, 4]],
1172+
)
1173+
# <Array memory://... shape=(2, 2) dtype=int64>
1174+
arr4[...]
1175+
# array([[1, 2],[3, 4]])
1176+
```
1177+
1178+
Create an array from an existing Array without copying the data:
1179+
1180+
```python
1181+
import zarr
1182+
arr4 = zarr.from_array(
1183+
zarr.storage.MemoryStore(),
1184+
data=[[1, 2], [3, 4]],
1185+
)
1186+
arr5 = zarr.from_array(
1187+
zarr.storage.MemoryStore(),
1188+
data=arr4,
1189+
write_data=False,
1190+
)
1191+
# <Array memory://... shape=(2, 2) dtype=int64>
1192+
arr5[...]
1193+
# array([[0, 0],[0, 0]])
1194+
```
11781195
"""
11791196
return Array(
11801197
sync(

src/zarr/codecs/numcodecs/_codecs.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
44
These codecs were previously defined in [numcodecs][], and have now been moved to `zarr`.
55
6-
>>> import numpy as np
7-
>>> import zarr
8-
>>> import zarr.codecs.numcodecs as numcodecs
9-
>>>
10-
>>> array = zarr.create_array(
11-
... store="data.zarr",
12-
... shape=(1024, 1024),
13-
... chunks=(64, 64),
14-
... dtype="uint32",
15-
... filters=[numcodecs.Delta(dtype="uint32")],
16-
... compressors=[numcodecs.BZ2(level=5)])
17-
>>> array[:] = np.arange(np.prod(array.shape), dtype=array.dtype).reshape(*array.shape)
6+
```python
7+
import numpy as np
8+
import zarr
9+
import zarr.codecs.numcodecs as numcodecs
10+
11+
array = zarr.create_array(
12+
store="data_numcodecs.zarr",
13+
shape=(1024, 1024),
14+
chunks=(64, 64),
15+
dtype="uint32",
16+
filters=[numcodecs.Delta(dtype="uint32")],
17+
compressors=[numcodecs.BZ2(level=5)],
18+
overwrite=True)
19+
array[:] = np.arange(np.prod(array.shape), dtype=array.dtype).reshape(*array.shape)
20+
```
1821
1922
!!! note
2023
Please note that the codecs in [zarr.codecs.numcodecs][] are not part of the Zarr version

src/zarr/core/array.py

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -982,10 +982,22 @@ async def open(
982982
983983
Examples
984984
--------
985-
>>> import zarr
986-
>>> store = zarr.storage.MemoryStore()
987-
>>> async_arr = await AsyncArray.open(store) # doctest: +ELLIPSIS
988-
<AsyncArray memory://... shape=(100, 100) dtype=int32>
985+
```python
986+
import zarr
987+
988+
async def example():
989+
store = zarr.storage.MemoryStore()
990+
# First create an array to open
991+
await zarr.api.asynchronous.create_array(
992+
store=store, shape=(100, 100), dtype="int32"
993+
)
994+
# Now open it
995+
async_arr = await AsyncArray.open(store)
996+
return async_arr
997+
998+
# async_arr = await example()
999+
# AsyncArray(...)
1000+
```
9891001
"""
9901002
store_path = await make_store_path(store)
9911003
metadata_dict = await get_array_metadata(store_path, zarr_format=zarr_format)
@@ -1300,12 +1312,20 @@ async def nchunks_initialized(self) -> int:
13001312
13011313
Examples
13021314
--------
1303-
>>> arr = await zarr.api.asynchronous.create(shape=(10,), chunks=(1,), shards=(2,))
1304-
>>> await arr.nchunks_initialized()
1305-
0
1306-
>>> await arr.setitem(slice(5), 1)
1307-
>>> await arr.nchunks_initialized()
1308-
6
1315+
```python
1316+
import zarr.api.asynchronous
1317+
1318+
async def example():
1319+
arr = await zarr.api.asynchronous.create(shape=(10,), chunks=(1,), shards=(2,))
1320+
count = await arr.nchunks_initialized()
1321+
# 0
1322+
await arr.setitem(slice(5), 1)
1323+
count = await arr.nchunks_initialized()
1324+
# 6
1325+
return count
1326+
1327+
# result = await example()
1328+
```
13091329
"""
13101330
if self.shards is None:
13111331
chunks_per_shard = 1
@@ -1333,12 +1353,20 @@ async def _nshards_initialized(self) -> int:
13331353
13341354
Examples
13351355
--------
1336-
>>> arr = await zarr.api.asynchronous.create(shape=(10,), chunks=(2,))
1337-
>>> await arr._nshards_initialized()
1338-
0
1339-
>>> await arr.setitem(slice(5), 1)
1340-
>>> await arr._nshards_initialized()
1341-
3
1356+
```python
1357+
import zarr.api.asynchronous
1358+
1359+
async def example():
1360+
arr = await zarr.api.asynchronous.create(shape=(10,), chunks=(2,))
1361+
count = await arr._nshards_initialized()
1362+
# 0
1363+
await arr.setitem(slice(5), 1)
1364+
count = await arr._nshards_initialized()
1365+
# 3
1366+
return count
1367+
1368+
# result = await example()
1369+
```
13421370
"""
13431371
return len(await _shards_initialized(self))
13441372

@@ -1566,18 +1594,23 @@ async def getitem(
15661594
15671595
Examples
15681596
--------
1569-
>>> import zarr
1570-
>>> store = zarr.storage.MemoryStore()
1571-
>>> async_arr = await zarr.api.asynchronous.create_array(
1572-
... store=store,
1573-
... shape=(100,100),
1574-
... chunks=(10,10),
1575-
... dtype='i4',
1576-
... fill_value=0)
1577-
<AsyncArray memory://... shape=(100, 100) dtype=int32>
1578-
>>> await async_arr.getitem((0,1)) # doctest: +ELLIPSIS
1579-
array(0, dtype=int32)
1580-
1597+
```python
1598+
import zarr.api.asynchronous
1599+
1600+
async def example():
1601+
store = zarr.storage.MemoryStore()
1602+
async_arr = await zarr.api.asynchronous.create_array(
1603+
store=store,
1604+
shape=(100,100),
1605+
chunks=(10,10),
1606+
dtype='i4',
1607+
fill_value=0)
1608+
result = await async_arr.getitem((0,1))
1609+
# array(0, dtype=int32)
1610+
return result
1611+
1612+
# value = await example()
1613+
```
15811614
"""
15821615
if prototype is None:
15831616
prototype = default_buffer_prototype()

src/zarr/core/dtype/__init__.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,16 @@ def parse_data_type(
213213
214214
Examples
215215
--------
216-
>>> from zarr.dtype import parse_data_type
217-
>>> import numpy as np
218-
>>> parse_data_type("int32", zarr_format=2)
219-
Int32(endianness='little')
220-
>>> parse_data_type(np.dtype('S10'), zarr_format=2)
221-
NullTerminatedBytes(length=10)
222-
>>> parse_data_type({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3)
223-
DateTime64(endianness='little', scale_factor=10, unit='s')
216+
```python
217+
from zarr.dtype import parse_data_type
218+
import numpy as np
219+
parse_data_type("int32", zarr_format=2)
220+
# Int32(endianness='little')
221+
parse_data_type(np.dtype('S10'), zarr_format=2)
222+
# NullTerminatedBytes(length=10)
223+
parse_data_type({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3)
224+
# DateTime64(endianness='little', scale_factor=10, unit='s')
225+
```
224226
"""
225227
return parse_dtype(dtype_spec, zarr_format=zarr_format)
226228

@@ -251,14 +253,16 @@ def parse_dtype(
251253
252254
Examples
253255
--------
254-
>>> from zarr.dtype import parse_dtype
255-
>>> import numpy as np
256-
>>> parse_dtype("int32", zarr_format=2)
257-
Int32(endianness='little')
258-
>>> parse_dtype(np.dtype('S10'), zarr_format=2)
259-
NullTerminatedBytes(length=10)
260-
>>> parse_dtype({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3)
261-
DateTime64(endianness='little', scale_factor=10, unit='s')
256+
```python
257+
from zarr.dtype import parse_dtype
258+
import numpy as np
259+
parse_dtype("int32", zarr_format=2)
260+
# Int32(endianness='little')
261+
parse_dtype(np.dtype('S10'), zarr_format=2)
262+
# NullTerminatedBytes(length=10)
263+
parse_dtype({"name": "numpy.datetime64", "configuration": {"unit": "s", "scale_factor": 10}}, zarr_format=3)
264+
# DateTime64(endianness='little', scale_factor=10, unit='s')
265+
```
262266
"""
263267
if isinstance(dtype_spec, ZDType):
264268
return dtype_spec

0 commit comments

Comments
 (0)