From eeb157377f2da90185119d473ba19bbc4a29961a Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Tue, 18 Oct 2022 10:06:26 -0700 Subject: [PATCH] Remove hardcoded properties list from DeferredOperand; use a c'tor param instead. --- src/pyutils/parallelize/deferred_operand.py | 8 +++++--- src/pyutils/parallelize/smart_future.py | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/pyutils/parallelize/deferred_operand.py b/src/pyutils/parallelize/deferred_operand.py index 5bb823f..2260b29 100644 --- a/src/pyutils/parallelize/deferred_operand.py +++ b/src/pyutils/parallelize/deferred_operand.py @@ -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 diff --git a/src/pyutils/parallelize/smart_future.py b/src/pyutils/parallelize/smart_future.py index 27cbabe..3661520 100644 --- a/src/pyutils/parallelize/smart_future.py +++ b/src/pyutils/parallelize/smart_future.py @@ -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") -- 2.45.2