From 2d69b51ecd8f0f481f473b020ab5d0cb0e8502a5 Mon Sep 17 00:00:00 2001 From: caviere Date: Wed, 2 Nov 2022 20:18:29 +0300 Subject: [PATCH 1/5] Add documentation for find/findall using visit --- zarr/hierarchy.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/zarr/hierarchy.py b/zarr/hierarchy.py index 8131cb71aa..3af58c1295 100644 --- a/zarr/hierarchy.py +++ b/zarr/hierarchy.py @@ -720,6 +720,76 @@ def visit(self, func): baz quux + Search for members matching some name query can be implemented using + ``visit`` that is, ``find`` and ``findall``. Consider the following + tree:: + / + ├── aaa + │ └── bbb + │ └── ccc + │ └── aaa + ├── bar + └── foo + + It is created as follows: + + >>> root = zarr.group() + >>> foo = root.create_group("foo") + >>> bar = root.create_group("bar") + >>> root.create_group("aaa").create_group("bbb").create_group("ccc").create_group("aaa") + + + For ``find``, the first path that matches a given pattern (for example + "aaa") is returned. Note that a non-None value is returned in the visit + function to stop further iteration. + + >>> import re + >>> pattern = re.compile("aaa") + >>> found = None + >>> def find(path): + ... global found + ... if pattern.search(path) is not None: + ... found = path + ... return True + ... + >>> root.visit(find) + True + >>> print(found) + 'aaa' + + For ``findall``, all the results are gathered into a list + + >>> pattern = re.compile("aaa") + >>> found = [] + >>> def findall(path): + ... if pattern.search(path) is not None: + ... found.append(path) + ... + >>> root.visit(findall) + >>> print(found) + ['aaa', 'aaa/bbb', 'aaa/bbb/ccc', 'aaa/bbb/ccc/aaa'] + + To match only on the last part of the path, use a greedy regex to filter + out the prefix: + + >>> prefix_pattern = re.compile(r".*/") + >>> pattern = re.compile("aaa") + >>> found = [] + >>> def findall(path): + ... match = prefix_pattern.match(path) + ... if match is None: + ... name = path + ... else: + ... _, end = match.span() + ... name = path[end:] + ... if pattern.search(name) is not None: + ... found.append(path) + ... return None + ... + >>> root.visit(findall) + >>> print(found) + ['aaa', 'aaa/bbb/ccc/aaa'] + """ base_len = len(self.name) From 192fb02ef8c347dc4deb1397c1f4b1ecddacb2ca Mon Sep 17 00:00:00 2001 From: caviere Date: Wed, 2 Nov 2022 20:35:34 +0300 Subject: [PATCH 2/5] Remove whitespace --- zarr/hierarchy.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/zarr/hierarchy.py b/zarr/hierarchy.py index 3af58c1295..009be99be6 100644 --- a/zarr/hierarchy.py +++ b/zarr/hierarchy.py @@ -720,8 +720,8 @@ def visit(self, func): baz quux - Search for members matching some name query can be implemented using - ``visit`` that is, ``find`` and ``findall``. Consider the following + Search for members matching some name query can be implemented using + ``visit`` that is, ``find`` and ``findall``. Consider the following tree:: / ├── aaa @@ -739,7 +739,7 @@ def visit(self, func): >>> root.create_group("aaa").create_group("bbb").create_group("ccc").create_group("aaa") - For ``find``, the first path that matches a given pattern (for example + For ``find``, the first path that matches a given pattern (for example "aaa") is returned. Note that a non-None value is returned in the visit function to stop further iteration. @@ -751,7 +751,7 @@ def visit(self, func): ... if pattern.search(path) is not None: ... found = path ... return True - ... + ... >>> root.visit(find) True >>> print(found) @@ -764,12 +764,12 @@ def visit(self, func): >>> def findall(path): ... if pattern.search(path) is not None: ... found.append(path) - ... + ... >>> root.visit(findall) >>> print(found) ['aaa', 'aaa/bbb', 'aaa/bbb/ccc', 'aaa/bbb/ccc/aaa'] - To match only on the last part of the path, use a greedy regex to filter + To match only on the last part of the path, use a greedy regex to filter out the prefix: >>> prefix_pattern = re.compile(r".*/") @@ -785,11 +785,11 @@ def visit(self, func): ... if pattern.search(name) is not None: ... found.append(path) ... return None - ... + ... >>> root.visit(findall) >>> print(found) ['aaa', 'aaa/bbb/ccc/aaa'] - + """ base_len = len(self.name) From ca3e99d0ba1555ea635d191b31fa6b830549218e Mon Sep 17 00:00:00 2001 From: caviere Date: Wed, 2 Nov 2022 20:55:41 +0300 Subject: [PATCH 3/5] Fix print result --- zarr/hierarchy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zarr/hierarchy.py b/zarr/hierarchy.py index 009be99be6..7ab08ac840 100644 --- a/zarr/hierarchy.py +++ b/zarr/hierarchy.py @@ -755,7 +755,7 @@ def visit(self, func): >>> root.visit(find) True >>> print(found) - 'aaa' + aaa For ``findall``, all the results are gathered into a list From 6eec2e26e643841b9ac5ef1d6e5454f939db2ade Mon Sep 17 00:00:00 2001 From: caviere Date: Thu, 3 Nov 2022 15:30:24 +0300 Subject: [PATCH 4/5] Fix indentation issue in the docstring --- zarr/hierarchy.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/zarr/hierarchy.py b/zarr/hierarchy.py index 7ab08ac840..630c15800a 100644 --- a/zarr/hierarchy.py +++ b/zarr/hierarchy.py @@ -723,13 +723,13 @@ def visit(self, func): Search for members matching some name query can be implemented using ``visit`` that is, ``find`` and ``findall``. Consider the following tree:: - / - ├── aaa - │ └── bbb - │ └── ccc - │ └── aaa - ├── bar - └── foo + / + ├── aaa + │ └── bbb + │ └── ccc + │ └── aaa + ├── bar + └── foo It is created as follows: @@ -789,7 +789,6 @@ def visit(self, func): >>> root.visit(findall) >>> print(found) ['aaa', 'aaa/bbb/ccc/aaa'] - """ base_len = len(self.name) From 0b8ba76e5f93a2d4e7405eeeea03fae240edbb7b Mon Sep 17 00:00:00 2001 From: caviere Date: Thu, 3 Nov 2022 16:08:28 +0300 Subject: [PATCH 5/5] Indent literal block --- zarr/hierarchy.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/zarr/hierarchy.py b/zarr/hierarchy.py index 630c15800a..42e81b060a 100644 --- a/zarr/hierarchy.py +++ b/zarr/hierarchy.py @@ -723,13 +723,14 @@ def visit(self, func): Search for members matching some name query can be implemented using ``visit`` that is, ``find`` and ``findall``. Consider the following tree:: - / - ├── aaa - │ └── bbb - │ └── ccc - │ └── aaa - ├── bar - └── foo + + / + ├── aaa + │ └── bbb + │ └── ccc + │ └── aaa + ├── bar + └── foo It is created as follows: