Repository: jazzband/tablib
Pull request #557 (2023-07-19 15:57:31)

Get row method
Fixes #24 get_row, get_col methods

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/jazzband/tablib/pull/557?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) Report
> Merging [#557](https://app.codecov.io/gh/jazzband/tablib/pull/557?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) (c45c50c) into [master](https://app.codecov.io/gh/jazzband/tablib/commit/f3ef2e94b40478521b646b58a25dd1b3c5b300f1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) (f3ef2e9) will **increase** coverage by `0.25%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #557      +/-   ##
==========================================
+ Coverage   91.37%   91.63%   +0.25%     
==========================================
  Files          28       28              
  Lines        2713     2796      +83     
==========================================
+ Hits         2479     2562      +83     
  Misses        234      234              
```


| [Impacted Files](https://app.codecov.io/gh/jazzband/tablib/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) | Coverage Δ | |
|---|---|---|
| [src/tablib/core.py](https://app.codecov.io/gh/jazzband/tablib/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-c3JjL3RhYmxpYi9jb3JlLnB5) | `85.08% <100.00%> (+0.06%)` | :arrow_up: |
| [src/tablib/formats/\_html.py](https://app.codecov.io/gh/jazzband/tablib/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-c3JjL3RhYmxpYi9mb3JtYXRzL19odG1sLnB5) | `100.00% <100.00%> (ø)` | |
| [tests/test\_tablib.py](https://app.codecov.io/gh/jazzband/tablib/pull/557?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-dGVzdHMvdGVzdF90YWJsaWIucHk=) | `98.77% <100.00%> (+0.04%)` | :arrow_up: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband)

yoonthegoon: Would one of you please take a look when you get the chance?
I'm looking to try to clean out some old issues on here when I get time. @hugovk @claudep 
hugovk: Thanks for the PR.

https://github.com/jazzband/tablib/pull/28 added `get_col`, should this be named `get_row` instead of `get` (as suggested in https://github.com/jazzband/tablib/issues/24)?
yoonthegoon: > Thanks for the PR.
> 
> 
> 
> https://github.com/jazzband/tablib/pull/28 added `get_col`, should this be named `get_row` instead of `get` (as suggested in https://github.com/jazzband/tablib/issues/24)?

i was originally gonna, but all the row methods in the class don't have `row` in their names while all the column methods do have `col`. kenneth also commented on the original issue that the methods he wanted were `get` to return a row by index and `get_col` for column by index.
claudep: Did you check if the docs should be updated somewhere?
yoonthegoon: I built the docs locally and it looks like it added it by me, so the existing rst files appear to pick it up.
`docs/_build/*` is in the `.gitignore` so I don't think there's any need for me to commit a build.
Built docs are [here](https://tablib--557.org.readthedocs.build/en/557/api.html#tablib.Dataset.get) and look good to me.
hugovk: We could add an example to the quickstart guide:

https://tablib.readthedocs.io/en/stable/tutorial.html#selecting-rows-columns 
claudep: The "add type validation" commit should not be part of this PR.
yoonthegoon: > The "add type validation" commit should not be part of this PR.

Yeah I just noticed it changed more than I intended. 
Lemme fix that real quick

Patch:
diff --git a/AUTHORS b/AUTHORS
index b040803..8b2336b 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -32,3 +32,4 @@ Here is a list of past and present much-appreciated contributors:
     Tommy Anthony
     Tsuyoshi Hombashi
     Tushar Makkar
+    Yunis Yilmaz
diff --git a/docs/tutorial.rst b/docs/tutorial.rst
index 23f4828..e5cd12b 100644
--- a/docs/tutorial.rst
+++ b/docs/tutorial.rst
@@ -167,7 +167,13 @@ You can slice and dice your data, just like a standard Python list. ::
 
     >>> data[0]
     ('Kenneth', 'Reitz', 22)
+    >>> data[0:2]
+    [('Kenneth', 'Reitz', 22), ('Bessie', 'Monke', 20)]
 
+You can also access a row using its index without slicing. ::
+
+    >>> data.get(0)
+    ('Kenneth', 'Reitz', 22)
 
 If we had a set of data consisting of thousands of rows,
 it could be useful to get a list of values in a column.
diff --git a/src/tablib/core.py b/src/tablib/core.py
index 61fce9d..252da78 100644
--- a/src/tablib/core.py
+++ b/src/tablib/core.py
@@ -501,6 +501,14 @@ class Dataset:
 
         return self.rpop()
 
+    def get(self, index):
+        """Returns the row from the :class:`Dataset` at the given index."""
+
+        if isinstance(index, int):
+            return self[index]
+
+        raise TypeError('Row indices must be integers.')
+
     # -------
     # Columns
     # -------


Test patch:
diff --git a/tests/test_tablib.py b/tests/test_tablib.py
index 0837a76..03ac58b 100755
--- a/tests/test_tablib.py
+++ b/tests/test_tablib.py
@@ -195,6 +195,23 @@ class TablibTestCase(BaseTestCase):
         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)
+
+        with self.assertRaises(TypeError):
+            self.founders.get('first_name')
+
     def test_get_col(self):
         """Verify getting columns by index"""