Remove hardcoded properties list from DeferredOperand; use a c'tor
authorScott Gasch <[email protected]>
Tue, 18 Oct 2022 17:06:26 +0000 (10:06 -0700)
committerScott Gasch <[email protected]>
Tue, 18 Oct 2022 17:06:26 +0000 (10:06 -0700)
param instead.

src/pyutils/parallelize/deferred_operand.py
src/pyutils/parallelize/smart_future.py

index 5bb823f746dfbde100cbad148ef4da5ed9f579b8..2260b29d1eaf6d3172f601fc7ff9cc921e3c6b21 100644 (file)
@@ -20,7 +20,7 @@ information.
 """
 
 from abc import ABC, abstractmethod
-from typing import Any, Generic, TypeVar
+from typing import Any, Generic, Set, TypeVar
 
 # This module is commonly used by others in here and should avoid
 # taking any unnecessary dependencies back on them.
@@ -33,9 +33,11 @@ class DeferredOperand(ABC, Generic[T]):
     needed (i.e. accessed).  See the subclass
     :class:`pyutils.parallelize.smart_future.SmartFuture` for an
     example usage and/or a more useful patten.
-
     """
 
+    def __init__(self, local_attributes: Set[str] = None):
+        self.__dict__['local_attributes'] = local_attributes
+
     @abstractmethod
     def _resolve(self, timeout=None) -> T:
         pass
@@ -186,7 +188,7 @@ class DeferredOperand(ABC, Generic[T]):
 
     def __setattr__(self, name, value):
         # subclass setting its own properties
-        if name in set(['id', 'wrapped_future']):
+        if name in self.local_attributes:
             object.__setattr__(self, name, value)
             return
 
index 27cbabef13668e5682a2905cfbd3c91b8a3b9b88..3661520effb28d68fe26aaae090a0697ee090058 100644 (file)
@@ -149,6 +149,7 @@ class SmartFuture(DeferredOperand):
             wrapped_future: a normal Python :class:`concurrent.Future`
                 object that we are wrapping.
         """
+        super().__init__(set(['id', 'wrapped_future', 'get_id', 'is_ready']))
         assert isinstance(wrapped_future, fut.Future)
         self.wrapped_future = wrapped_future
         self.id = id_generator.get("smart_future_id")