`Block.tree()` doesn't support depth limit

Open Beginner friendly
#28 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python

Research direction

Start in dag/block.py at Block.tree() and the _walk_tree() helper, then inspect how the current depth-first paths are produced. Verify the existing unlimited behavior and the requested depth cases from the issue: depth 0 yields nothing, depth 1 yields top-level paths, and the default remains unlimited.

Written by the indexing model from the issue text.

Description

Block.tree() always traverses the entire tree. Go's Tree(path, depth) supports a depth parameter to limit traversal. This is useful for large DAGs where you only want to see the top-level structure.

Problem

In dag/block.py:

def tree(self) -> Iterator[str]:
    """Yield every path segment in this block's value (depth-first)."""
    yield from _walk_tree(self._value, "")

There's no way to limit the depth:

block = Block.encode(
    value={"a": {"b": {"c": "deep"}}},
    codec=dag_cbor.codec,
)

list(block.tree())  # → ["a", "a/b", "a/b/c"]
# No way to get just: ["a"]
Proposed Solution

Add an optional depth parameter:

def tree(self, depth: int = -1) -> Iterator[str]:
    """Yield every path segment in this block's value (depth-first).

    Parameters
    ----------
    depth:
        Maximum depth to traverse. -1 means unlimited (default).
        0 yields nothing. 1 yields only top-level keys.
    """
    if depth == 0:
        return
    yield from _walk_tree(self._value, "", max_depth=depth, current_depth=0)

Update _walk_tree():

def _walk_tree(node, prefix, max_depth=-1, current_depth=0):
    if max_depth >= 0 and current_depth >= max_depth:
        return
    if isinstance(node, dict):
        for k, v in node.items():
            child = f"{prefix}/{k}" if prefix else k
            yield child
            yield from _walk_tree(v, child, max_depth, current_depth + 1)
    elif isinstance(node, list):
        for i, v in enumerate(node):
            child = f"{prefix}/{i}" if prefix else str(i)
            yield child
            yield from _walk_tree(v, child, max_depth, current_depth + 1)
Related
  • Go implementation: go-ipld-prime Node.Tree() method
  • File: dag/block.py
Dominant language
Python
Stars
13
Forks
9
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ipld/py-ipld-dag

All issues in ipld/py-ipld-dag

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.