Skip to content

Commit 2172ffe

Browse files
committed
docstrings
1 parent 8a33df7 commit 2172ffe

File tree

1 file changed

+238
-0
lines changed

1 file changed

+238
-0
lines changed

src/zarr/core/group.py

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,24 @@ async def tree(self, expand: bool = False, level: int | None = None) -> Any:
12761276
async def empty(
12771277
self, *, name: str, shape: ChunkCoords, **kwargs: Any
12781278
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1279+
"""Create an empty array in this Group.
1280+
1281+
Parameters
1282+
----------
1283+
name : str
1284+
Name of the array.
1285+
shape : int or tuple of int
1286+
Shape of the empty array.
1287+
**kwargs
1288+
Keyword arguments passed to :func:`zarr.api.asynchronous.create`.
1289+
1290+
Notes
1291+
-----
1292+
The contents of an empty Zarr array are not defined. On attempting to
1293+
retrieve data from an empty Zarr array, any values may be returned,
1294+
and these are not guaranteed to be stable from one access to the next.
1295+
"""
1296+
12791297
return await async_api.empty(shape=shape, store=self.store_path, path=name, **kwargs)
12801298

12811299
async def zeros(
@@ -1336,6 +1354,29 @@ def from_store(
13361354
zarr_format: ZarrFormat = 3,
13371355
exists_ok: bool = False,
13381356
) -> Group:
1357+
"""Instantiate a group from an initialized store.
1358+
1359+
Parameters
1360+
----------
1361+
store : StoreLike
1362+
StoreLike containing the Group.
1363+
attributes : dict, optional
1364+
A dictionary of JSON-serializable values with user-defined attributes.
1365+
zarr_format : {2, 3}, optional
1366+
Zarr storage format version.
1367+
exists_ok : bool, optional
1368+
If True, do not raise an error if the group already exists.
1369+
1370+
Returns
1371+
-------
1372+
Group
1373+
Group instantiated from the store.
1374+
1375+
Raises
1376+
------
1377+
ContainsArrayError, ContainsGroupError, ContainsArrayAndGroupError
1378+
1379+
"""
13391380
attributes = attributes or {}
13401381
obj = sync(
13411382
AsyncGroup.from_store(
@@ -1354,10 +1395,48 @@ def open(
13541395
store: StoreLike,
13551396
zarr_format: Literal[2, 3, None] = 3,
13561397
) -> Group:
1398+
"""Open a group from an initialized store.
1399+
Parameters
1400+
----------
1401+
store : StoreLike
1402+
Store containing the Group.
1403+
zarr_format : {2, 3, None}, optional
1404+
Zarr storage format version.
1405+
1406+
Returns
1407+
-------
1408+
Group
1409+
Group instantiated from the store.
1410+
"""
13571411
obj = sync(AsyncGroup.open(store, zarr_format=zarr_format))
13581412
return cls(obj)
13591413

13601414
def __getitem__(self, path: str) -> Array | Group:
1415+
"""Obtain a group member.
1416+
Parameters
1417+
----------
1418+
path : str
1419+
Group member name.
1420+
1421+
Returns
1422+
-------
1423+
Array | Group
1424+
Group member (Array or Group) at the specified key
1425+
1426+
Examples
1427+
--------
1428+
>>> import zarr
1429+
>>> group = Group.from_store(zarr.storage.MemoryStore(mode="w"))
1430+
>>> group.create_array(name="subarray", shape=(10,), chunk_shape=(10,))
1431+
>>> group.create_group(name="subgroup").create_array(name="subarray", shape=(10,), chunk_shape=(10,))
1432+
>>> group["subarray"]
1433+
<Array memory://132270269438272/subarray shape=(10,) dtype=float64>
1434+
>>> group["subgroup"]
1435+
<Group memory://132270269438272/subgroup>
1436+
>>> group["subgroup"]["subarray"]
1437+
<Array memory://132270269438272/subgroup/subarray shape=(10,) dtype=float64>
1438+
1439+
"""
13611440
obj = self._sync(self._async_group.getitem(path))
13621441
if isinstance(obj, AsyncArray):
13631442
return Array(obj)
@@ -1378,19 +1457,50 @@ def get(self, path: str, default: DefaultT | None = None) -> Array | Group | Def
13781457
-------
13791458
object
13801459
Group member (Array or Group) or default if not found.
1460+
1461+
Examples
1462+
--------
1463+
>>> import zarr
1464+
>>> group = Group.from_store(zarr.storage.MemoryStore(mode="w"))
1465+
>>> group.create_array(name="subarray", shape=(10,), chunk_shape=(10,))
1466+
>>> group.create_group(name="subgroup")
1467+
>>> group.get("subarray")
1468+
<Array memory://132270269438272/subarray shape=(10,) dtype=float64>
1469+
>>> group.get("subgroup")
1470+
<Group memory://132270269438272/subgroup>
1471+
>>> group.get("nonexistent", None)
1472+
13811473
"""
13821474
try:
13831475
return self[path]
13841476
except KeyError:
13851477
return default
13861478

13871479
def __delitem__(self, key: str) -> None:
1480+
"""Delete a group member."""
13881481
self._sync(self._async_group.delitem(key))
13891482

13901483
def __iter__(self) -> Iterator[str]:
1484+
"""Return an iterator over group member names.
1485+
Examples
1486+
--------
1487+
>>> import zarr
1488+
>>> g1 = zarr.group()
1489+
>>> g2 = g1.create_group('foo')
1490+
>>> g3 = g1.create_group('bar')
1491+
>>> d1 = g1.create_array('baz', shape=(10,), chunk_shape=(10,))
1492+
>>> d2 = g1.create_array('quux', shape=(10,), chunk_shape=(10,))
1493+
>>> for name in g1:
1494+
... print(name)
1495+
baz
1496+
bar
1497+
foo
1498+
quux
1499+
"""
13911500
yield from self.keys()
13921501

13931502
def __len__(self) -> int:
1503+
"""Number of members."""
13941504
return self.nmembers()
13951505

13961506
def __setitem__(self, key: str, value: Any) -> None:
@@ -1401,6 +1511,15 @@ def __repr__(self) -> str:
14011511
return f"<Group {self.store_path}>"
14021512

14031513
async def update_attributes_async(self, new_attributes: dict[str, Any]) -> Group:
1514+
"""Update the attributes of this group.
1515+
Example
1516+
-------
1517+
>>> import zarr
1518+
>>> group = zarr.group()
1519+
>>> await group.update_attributes_async({"foo": "bar"})
1520+
>>> group.attrs.asdict()
1521+
{'foo': 'bar'}
1522+
"""
14041523
new_metadata = replace(self.metadata, attributes=new_attributes)
14051524

14061525
# Write new metadata
@@ -1413,10 +1532,12 @@ async def update_attributes_async(self, new_attributes: dict[str, Any]) -> Group
14131532

14141533
@property
14151534
def store_path(self) -> StorePath:
1535+
"""Path-like interface for the Store."""
14161536
return self._async_group.store_path
14171537

14181538
@property
14191539
def metadata(self) -> GroupMetadata:
1540+
"""Group metadata."""
14201541
return self._async_group.metadata
14211542

14221543
@property
@@ -1436,6 +1557,7 @@ def basename(self) -> str:
14361557

14371558
@property
14381559
def attrs(self) -> Attributes:
1560+
"""Attributes of this Group"""
14391561
return Attributes(self)
14401562

14411563
@property
@@ -1459,10 +1581,32 @@ def synchronizer(self) -> None:
14591581
return self._async_group.synchronizer
14601582

14611583
def update_attributes(self, new_attributes: dict[str, Any]) -> Group:
1584+
"""Update the attributes of this group.
1585+
Example
1586+
-------
1587+
>>> import zarr
1588+
>>> group = zarr.group()
1589+
>>> group.update_attributes({"foo": "bar"})
1590+
>>> group.attrs.asdict()
1591+
{'foo': 'bar'}
1592+
"""
14621593
self._sync(self._async_group.update_attributes(new_attributes))
14631594
return self
14641595

14651596
def nmembers(self, max_depth: int | None = 0) -> int:
1597+
"""Count the number of members in this group.
1598+
Parameters
1599+
----------
1600+
max_depth : int, default 0
1601+
The maximum number of levels of the hierarchy to include. By
1602+
default, (``max_depth=0``) only immediate children are included. Set
1603+
``max_depth=None`` to include all nodes, and some positive integer
1604+
to consider children within that many levels of the root Group.
1605+
Returns
1606+
-------
1607+
count : int
1608+
"""
1609+
14661610
return self._sync(self._async_group.nmembers(max_depth=max_depth))
14671611

14681612
def members(self, max_depth: int | None = 0) -> tuple[tuple[str, Array | Group], ...]:
@@ -1475,32 +1619,126 @@ def members(self, max_depth: int | None = 0) -> tuple[tuple[str, Array | Group],
14751619
return tuple((kv[0], _parse_async_node(kv[1])) for kv in _members)
14761620

14771621
def keys(self) -> Generator[str, None]:
1622+
"""Return an iterator over group member names.
1623+
Examples
1624+
--------
1625+
>>> import zarr
1626+
>>> g1 = zarr.group()
1627+
>>> g2 = g1.create_group('foo')
1628+
>>> g3 = g1.create_group('bar')
1629+
>>> d1 = g1.create_array('baz', shape=(10,), chunk_shape=(10,))
1630+
>>> d2 = g1.create_array('quux', shape=(10,), chunk_shape=(10,))
1631+
>>> for name in g1.keys():
1632+
... print(name)
1633+
baz
1634+
bar
1635+
foo
1636+
quux
1637+
"""
14781638
yield from self._sync_iter(self._async_group.keys())
14791639

14801640
def __contains__(self, member: str) -> bool:
1641+
"""Test for group membership.
1642+
1643+
Examples
1644+
--------
1645+
>>> import zarr
1646+
>>> g1 = zarr.group()
1647+
>>> g2 = g1.create_group('foo')
1648+
>>> d1 = g1.create_array('bar', shape=(10,), chunk_shape=(10,))
1649+
>>> 'foo' in g1
1650+
True
1651+
>>> 'bar' in g1
1652+
True
1653+
>>> 'baz' in g1
1654+
False
1655+
1656+
"""
14811657
return self._sync(self._async_group.contains(member))
14821658

14831659
def groups(self) -> Generator[tuple[str, Group], None]:
1660+
"""Return the sub-groups of this group as a generator of (name, group) pairs.
1661+
Example
1662+
-------
1663+
>>> import zarr
1664+
>>> group = zarr.group()
1665+
>>> group.create_group("subgroup")
1666+
>>> for name, subgroup in group.groups():
1667+
... print(name, subgroup)
1668+
subgroup <Group memory://132270269438272/subgroup>
1669+
"""
14841670
for name, async_group in self._sync_iter(self._async_group.groups()):
14851671
yield name, Group(async_group)
14861672

14871673
def group_keys(self) -> Generator[str, None]:
1674+
"""Return an iterator over group member names.
1675+
Examples
1676+
--------
1677+
>>> import zarr
1678+
>>> group = zarr.group()
1679+
>>> group.create_group("subgroup")
1680+
>>> for name in group.group_keys():
1681+
... print(name)
1682+
subgroup
1683+
"""
14881684
for name, _ in self.groups():
14891685
yield name
14901686

14911687
def group_values(self) -> Generator[Group, None]:
1688+
"""Return an iterator over group members.
1689+
Examples
1690+
--------
1691+
>>> import zarr
1692+
>>> group = zarr.group()
1693+
>>> group.create_group("subgroup")
1694+
>>> for subgroup in group.group_values():
1695+
... print(subgroup)
1696+
<Group memory://132270269438272/subgroup>
1697+
"""
14921698
for _, group in self.groups():
14931699
yield group
14941700

14951701
def arrays(self) -> Generator[tuple[str, Array], None]:
1702+
"""
1703+
Return the sub-arrays of this group as a generator of (name, array) pairs
1704+
Examples
1705+
--------
1706+
>>> import zarr
1707+
>>> group = zarr.group()
1708+
>>> group.create_array("subarray", shape=(10,), chunk_shape=(10,))
1709+
>>> for name, subarray in group.arrays():
1710+
... print(name, subarray)
1711+
subarray <Array memory://140198565357056/subarray shape=(10,) dtype=float64>
1712+
"""
14961713
for name, async_array in self._sync_iter(self._async_group.arrays()):
14971714
yield name, Array(async_array)
14981715

14991716
def array_keys(self) -> Generator[str, None]:
1717+
"""Return an iterator over group member names.
1718+
Examples
1719+
--------
1720+
>>> import zarr
1721+
>>> group = zarr.group()
1722+
>>> group.create_array("subarray", shape=(10,), chunk_shape=(10,))
1723+
>>> for name in group.array_keys():
1724+
... print(name)
1725+
subarray
1726+
"""
1727+
15001728
for name, _ in self.arrays():
15011729
yield name
15021730

15031731
def array_values(self) -> Generator[Array, None]:
1732+
"""Return an iterator over group members.
1733+
Examples
1734+
--------
1735+
>>> import zarr
1736+
>>> group = zarr.group()
1737+
>>> group.create_array("subarray", shape=(10,), chunk_shape=(10,))
1738+
>>> for subarray in group.array_values():
1739+
... print(subarray)
1740+
<Array memory://140198565357056/subarray shape=(10,) dtype=float64>
1741+
"""
15041742
for _, array in self.arrays():
15051743
yield array
15061744

0 commit comments

Comments
 (0)