Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(weave): Make Dataset support len and limited indexing #3365

Merged
merged 4 commits into from
Jan 15, 2025
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
12 changes: 12 additions & 0 deletions tests/trace/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import weave


Expand All @@ -21,3 +23,13 @@ def test_dataset_iteration(client):
# Test that we can iterate multiple times
rows2 = list(dataset)
assert rows2 == rows


def test_pythonic_access(client):
rows = [{"a": 1}, {"a": 2}, {"a": 3}, {"a": 4}, {"a": 5}]
ds = weave.Dataset(rows=rows)
assert len(ds) == 5
assert ds[0] == {"a": 1}

with pytest.raises(IndexError):
ds[-1]
9 changes: 9 additions & 0 deletions weave/flow/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ def convert_to_table(cls, rows: Any) -> weave.Table:

def __iter__(self) -> Iterator[dict]:
return iter(self.rows)

def __len__(self) -> int:
# TODO: This can be slow for large datasets...
return len(list(self.rows))

def __getitem__(self, key: int) -> dict:
if key < 0:
raise IndexError("Negative indexing is not supported")
return self.rows[key]
Loading