import hashlib from input.input_validator import generate_random_md5 # autogenerated class to hold the input data class InputObservation: """ A class to hold an input observation and associated metadata Attributes: image (Any): The image associated with the observation. latitude (float): The latitude where the observation was made. longitude (float): The longitude where the observation was made. author_email (str): The email of the author of the observation. date (str): The date when the observation was made. time (str): The time when the observation was made. date_option (str): Additional date option for the observation. time_option (str): Additional time option for the observation. uploaded_filename (Any): The uploaded filename associated with the observation. Methods: __str__(): Returns a string representation of the observation. __repr__(): Returns a string representation of the observation. __eq__(other): Checks if two observations are equal. __ne__(other): Checks if two observations are not equal. __hash__(): Returns the hash of the observation. to_dict(): Converts the observation to a dictionary. from_dict(data): Creates an observation from a dictionary. from_input(input): Creates an observation from another input observation. """ def __init__(self, image=None, latitude=None, longitude=None, author_email=None, date=None, time=None, date_option=None, time_option=None, uploaded_filename=None): self.image = image self.latitude = latitude self.longitude = longitude self.author_email = author_email self.date = date self.time = time self.date_option = date_option self.time_option = time_option self.uploaded_filename = uploaded_filename self._top_predictions = [] def set_top_predictions(self, top_predictions:list): self._top_predictions = top_predictions # add a method to get the top predictions (property?) @property def top_predictions(self): return self._top_predictions def __str__(self): return f"Observation: {self.image}, {self.latitude}, {self.longitude}, {self.author_email}, {self.date}, {self.time}, {self.date_option}, {self.time_option}, {self.uploaded_filename}" def __repr__(self): return f"Observation: {self.image}, {self.latitude}, {self.longitude}, {self.author_email}, {self.date}, {self.time}, {self.date_option}, {self.time_option}, {self.uploaded_filename}" def __eq__(self, other): return (self.image == other.image and self.latitude == other.latitude and self.longitude == other.longitude and self.author_email == other.author_email and self.date == other.date and self.time == other.time and self.date_option == other.date_option and self.time_option == other.time_option and self.uploaded_filename == other.uploaded_filename) def __ne__(self, other): return not self.__eq__(other) def __hash__(self): return hash((self.image, self.latitude, self.longitude, self.author_email, self.date, self.time, self.date_option, self.time_option, self.uploaded_filename)) def to_dict(self): return { #"image": self.image, "image_filename": self.uploaded_filename.name if self.uploaded_filename else None, "image_md5": hashlib.md5(self.uploaded_filename.read()).hexdigest() if self.uploaded_filename else generate_random_md5(), "latitude": self.latitude, "longitude": self.longitude, "author_email": self.author_email, "date": self.date, "time": self.time, "date_option": str(self.date_option), "time_option": str(self.time_option), "uploaded_filename": self.uploaded_filename } @classmethod def from_dict(cls, data): return cls(data["image"], data["latitude"], data["longitude"], data["author_email"], data["date"], data["time"], data["date_option"], data["time_option"], data["uploaded_filename"]) @classmethod def from_input(cls, input): return cls(input.image, input.latitude, input.longitude, input.author_email, input.date, input.time, input.date_option, input.time_option, input.uploaded_filename) @staticmethod def from_input(input): return InputObservation(input.image, input.latitude, input.longitude, input.author_email, input.date, input.time, input.date_option, input.time_option, input.uploaded_filename) @staticmethod def from_dict(data): return InputObservation(data["image"], data["latitude"], data["longitude"], data["author_email"], data["date"], data["time"], data["date_option"], data["time_option"], data["uploaded_filename"])