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

Get row method #557

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ Here is a list of past and present much-appreciated contributors:
Tommy Anthony
Tsuyoshi Hombashi
Tushar Makkar
Yunis Yilmaz
5 changes: 5 additions & 0 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ def pop(self):

return self.rpop()

def get(self, index):
"""Returns the row from the :class:`Dataset` at the given index."""

return self[index]

# -------
# Columns
# -------
Expand Down
14 changes: 14 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ def test_header_slicing(self):
self.assertEqual(self.founders['gpa'],
[self.john[2], self.george[2], self.tom[2]])

def test_get(self):
"""Verify getting rows by index"""

self.assertEqual(self.founders.get(0), self.john)
self.assertEqual(self.founders.get(1), self.george)
self.assertEqual(self.founders.get(2), self.tom)

self.assertEqual(self.founders.get(-1), self.tom)
self.assertEqual(self.founders.get(-2), self.george)
self.assertEqual(self.founders.get(-3), self.john)

with self.assertRaises(IndexError):
self.founders.get(3)

def test_get_col(self):
"""Verify getting columns by index"""

Expand Down