From 325460d8a6014fbf1aa74d522d51cdb942a76298 Mon Sep 17 00:00:00 2001 From: Minh Trinh <159905267+minhtien-trinh@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:54:59 +0200 Subject: [PATCH] Apply suggestions from code review improve readability Co-authored-by: Grzegorz Bokota --- src/napari_spatialdata/_interactive.py | 35 +++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/napari_spatialdata/_interactive.py b/src/napari_spatialdata/_interactive.py index 1dadfdb..389fd89 100644 --- a/src/napari_spatialdata/_interactive.py +++ b/src/napari_spatialdata/_interactive.py @@ -104,25 +104,26 @@ def screenshot(self) -> ArrayLike | Any: def get_layer(self, layer_name: str) -> Image | Labels | Points | Shapes | None: """Get a layer by name.""" - for layer in self._viewer.layers: - if layer.name == layer_name: - return layer - return None + try: + return self._viewer.layers[layer_name] + except KeyError: + return None def add_text_to_polygons(self, layer_name: str, text_annotations: list[str]) -> None: """Add text annotations to a polygon layer.""" polygon_layer = self.get_layer(layer_name) - if polygon_layer: - if len(text_annotations) != len(polygon_layer.data): - raise ValueError( - f"The number of text annotations must match the number of polygons. " - f"Polygons: {len(polygon_layer.data)}, Text: {len(text_annotations)}." - ) - polygon_layer.text = { - "string": text_annotations, - "size": 10, - "color": "red", - "anchor": "center", - } - else: + if not polygon_layer: raise ValueError(f"Polygon layer '{layer_name}' not found.") + if len(text_annotations) != len(polygon_layer.data): + raise ValueError( + f"The number of text annotations must match the number of polygons. " + f"Polygons: {len(polygon_layer.data)}, Text: {len(text_annotations)}." + ) + polygon_layer.text = { + "string": text_annotations, + "size": 10, + "color": "red", + "anchor": "center", + } + +