From 2159fcbe3d66f9b274b4093358ab85d7bbbbded6 Mon Sep 17 00:00:00 2001 From: Subhransu Bhattacharjee Date: Mon, 11 Sep 2023 12:31:07 +1000 Subject: [PATCH] Update mangrove.py Signed-off-by: Subhransu Bhattacharjee --- mangroves/mangrove.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/mangroves/mangrove.py b/mangroves/mangrove.py index f4a4ed6..aba2c58 100644 --- a/mangroves/mangrove.py +++ b/mangroves/mangrove.py @@ -1,16 +1,33 @@ +import weakref import torch from typing import List, Union, Type, Any, Dict, Optional, Tuple class Mangrove: __slots__ = ["depths", "data", "types", "levels", "inosculations"] + _instances = weakref.WeakValueDictionary() # WeakValueDictionary to keep track of instances def __init__(self) -> None: + instance_id = id(self) + if instance_id in Mangrove._instances: + raise Exception("MangroveException: Cannot reuse variable name for this instance unless deleted.") + + Mangrove._instances[instance_id] = self # Register the new instance + self.depths = {0: [int, float, str, torch.Tensor]} self.data = {} self.types = {} self.levels = {} self.inosculations = {} + def deleter(self) -> None: + """Remove the instance from the WeakValueDictionary.""" + instance_id = id(self) + if instance_id in Mangrove._instances: + del Mangrove._instances[instance_id] + else: + existing_instances = list(Mangrove._instances.keys()) + self._raise_exception(f"Instance ID {instance_id} does not exist. Existing instances: {existing_instances}") + def _raise_exception(self, message: str) -> None: """Raise an exception with a custom message.""" raise Exception(f"MangroveException: {message}")