Skip to content

Commit 62fb617

Browse files
authored
Allow string indexing in ItemizedResource.row (#649)
1 parent 62b94f0 commit 62fb617

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
6060
- `location` parameter of `assign_child_resource` is not optional (https://github.com/PyLabRobot/pylabrobot/pull/336)
6161
- `Resource.get_absolute_location` raises `NoLocationError` instead of `AssertionError` when absolute location is not defined (https://github.com/PyLabRobot/pylabrobot/pull/338)
6262
- `no_trash` and `no_teaching_rack` were renamed to `with_trash` and `with_teaching_rack` to avoid double negatives (https://github.com/PyLabRobot/pylabrobot/pull/347)
63+
- `ItemizedResource.row` accepts a string ("A"-"P") in addition to an integer index.
6364

6465
### Added
6566

pylabrobot/resources/itemized_resource.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,21 @@ def column(self, column: int) -> List[T]:
473473
"""Get all items in the given column."""
474474
return self[column * self.num_items_y : (column + 1) * self.num_items_y]
475475

476-
def row(self, row: int) -> List[T]:
477-
"""Get all items in the given row."""
476+
def row(self, row: Union[int, str]) -> List[T]:
477+
"""Get all items in the given row.
478+
479+
Args:
480+
row: The row index. Either an integer starting at ``0`` or a letter
481+
``"A"``-``"P"`` (case insensitive) corresponding to ``0``-``15``.
482+
483+
Raises:
484+
ValueError: If ``row`` is a string outside ``"A"``-``"P"``.
485+
"""
486+
487+
if isinstance(row, str):
488+
letter = row.upper()
489+
if letter not in LETTERS[:16]:
490+
raise ValueError("Row must be between 'A' and 'P'.")
491+
row = LETTERS.index(letter)
492+
478493
return self[row :: self.num_items_y]

pylabrobot/resources/itemized_resource_tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ def test_get_row(self):
183183
],
184184
)
185185

186+
def test_get_row_str(self):
187+
self.assertEqual(
188+
[w.name for w in self.plate.row("A")],
189+
[w.name for w in self.plate.row(0)],
190+
)
191+
self.assertEqual(
192+
[w.name for w in self.plate.row("d")],
193+
[w.name for w in self.plate.row(3)],
194+
)
195+
196+
def test_get_row_str_invalid(self):
197+
with self.assertRaises(ValueError):
198+
self.plate.row("Q")
199+
with self.assertRaises(ValueError):
200+
self.plate.row("AA")
201+
186202
def test_get_column(self):
187203
self.assertEqual(
188204
[w.name for w in self.plate.column(0)],

0 commit comments

Comments
 (0)