From bf9a1e358ec4276d7dfbd5a0d23fc1c4e8edb246 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 14 Jan 2020 15:29:35 +0000 Subject: [PATCH] Fixed finding captures by UUID --- openflexure_microscope/camera/base.py | 6 +++--- openflexure_microscope/utilities.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index 1bdf33bf..ff1c3527 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -9,7 +9,7 @@ import logging from abc import ABCMeta, abstractmethod from .capture import CaptureObject -from openflexure_microscope.utilities import entry_by_id +from openflexure_microscope.utilities import entry_by_uuid from openflexure_microscope.common.labthings_core.lock import StrictLock @@ -250,11 +250,11 @@ class BaseCamera(metaclass=ABCMeta): def image_from_id(self, image_id): """Return an image StreamObject with a matching ID.""" - return entry_by_id(image_id, self.images) + return entry_by_uuid(image_id, self.images) def video_from_id(self, video_id): """Return a video StreamObject with a matching ID.""" - return entry_by_id(video_id, self.videos) + return entry_by_uuid(video_id, self.videos) # CREATING NEW CAPTURES diff --git a/openflexure_microscope/utilities.py b/openflexure_microscope/utilities.py index 64379f4d..bd88664e 100644 --- a/openflexure_microscope/utilities.py +++ b/openflexure_microscope/utilities.py @@ -2,6 +2,7 @@ import re import copy import operator import base64 +from uuid import UUID import numpy as np from collections import abc from functools import reduce @@ -78,11 +79,20 @@ def filter_dict(dictionary: dict, keys: list): return out -def entry_by_id(entry_id: str, object_list: list): +def entry_by_uuid(entry_id: str, object_list: list): """Return an object from a list, if .id matches id argument.""" found = None + if type(entry_id) == str: + converter = str + elif type(entry_id) == int: + converter = int + elif isinstance(entry_id, UUID): + converter = int + else: + raise TypeError("Argument entry_id must be a string, integer, or UUID object.") for o in object_list: - if o.id == entry_id: + # Convert to strings (in case of UUID objects, for example) + if converter(o.id) == converter(entry_id): found = o return found