3 # © Copyright 2021-2022, Scott Gasch
5 """This is a helper class that tries to define every __dunder__ method
6 so as to defer that evaluation of an object as long as possible. It
7 is used by smart_future.py as a base class.
11 from abc import ABC, abstractmethod
12 from typing import Any, Generic, TypeVar
14 # This module is commonly used by others in here and should avoid
15 # taking any unnecessary dependencies back on them.
20 class DeferredOperand(ABC, Generic[T]):
21 """A wrapper around an operand whose value is deferred until it is
22 needed. See subclass SmartFuture for an example usage.
26 def _resolve(self, timeout=None) -> T:
30 def resolve(x: Any) -> Any:
31 while isinstance(x, DeferredOperand):
35 def __lt__(self, other: Any) -> bool:
36 return DeferredOperand.resolve(self) < DeferredOperand.resolve(other)
38 def __le__(self, other: Any) -> bool:
39 return DeferredOperand.resolve(self) <= DeferredOperand.resolve(other)
41 def __eq__(self, other: Any) -> bool:
42 return DeferredOperand.resolve(self) == DeferredOperand.resolve(other)
44 def __ne__(self, other: Any) -> bool:
45 return DeferredOperand.resolve(self) != DeferredOperand.resolve(other)
47 def __gt__(self, other: Any) -> bool:
48 return DeferredOperand.resolve(self) > DeferredOperand.resolve(other)
50 def __ge__(self, other: Any) -> bool:
51 return DeferredOperand.resolve(self) >= DeferredOperand.resolve(other)
53 def __not__(self) -> bool:
54 return not DeferredOperand.resolve(self)
56 def bool(self) -> bool:
57 return DeferredOperand.resolve(self)
59 def __add__(self, other: Any) -> T:
60 return DeferredOperand.resolve(self) + DeferredOperand.resolve(other)
62 def __iadd__(self, other: Any) -> T:
63 return DeferredOperand.resolve(self) + DeferredOperand.resolve(other)
65 def __radd__(self, other: Any) -> T:
66 return DeferredOperand.resolve(self) + DeferredOperand.resolve(other)
68 def __sub__(self, other: Any) -> T:
69 return DeferredOperand.resolve(self) - DeferredOperand.resolve(other)
71 def __mul__(self, other: Any) -> T:
72 return DeferredOperand.resolve(self) * DeferredOperand.resolve(other)
74 def __pow__(self, other: Any) -> T:
75 return DeferredOperand.resolve(self) ** DeferredOperand.resolve(other)
77 def __truediv__(self, other: Any) -> Any:
78 return DeferredOperand.resolve(self) / DeferredOperand.resolve(other)
80 def __floordiv__(self, other: Any) -> T:
81 return DeferredOperand.resolve(self) // DeferredOperand.resolve(other)
83 def __contains__(self, other):
84 return DeferredOperand.resolve(other) in DeferredOperand.resolve(self)
86 def and_(self, other):
87 return DeferredOperand.resolve(self) & DeferredOperand.resolve(other)
90 return DeferredOperand.resolve(self) & DeferredOperand.resolve(other)
93 return DeferredOperand.resolve(self) & DeferredOperand.resolve(other)
96 return ~(DeferredOperand.resolve(self))
99 return DeferredOperand.resolve(self) is DeferredOperand.resolve(other)
101 def is_not(self, other):
102 return DeferredOperand.resolve(self) is not DeferredOperand.resolve(other)
105 return abs(DeferredOperand.resolve(self))
107 def setitem(self, k, v):
108 DeferredOperand.resolve(self)[DeferredOperand.resolve(k)] = v
110 def delitem(self, k):
111 del DeferredOperand.resolve(self)[DeferredOperand.resolve(k)]
113 def getitem(self, k):
114 return DeferredOperand.resolve(self)[DeferredOperand.resolve(k)]
116 def lshift(self, other):
117 return DeferredOperand.resolve(self) << DeferredOperand.resolve(other)
119 def rshift(self, other):
120 return DeferredOperand.resolve(self) >> DeferredOperand.resolve(other)
122 def mod(self, other):
123 return DeferredOperand.resolve(self) % DeferredOperand.resolve(other)
125 def matmul(self, other):
126 return DeferredOperand.resolve(self) @ DeferredOperand.resolve(other)
129 return -(DeferredOperand.resolve(self))
132 return +(DeferredOperand.resolve(self))
135 return DeferredOperand.resolve(self)
138 return DeferredOperand.resolve(self).__hash__()
141 return DeferredOperand.resolve(self)()
144 return DeferredOperand.resolve(self).__iter__()
146 def __repr__(self) -> str:
147 return DeferredOperand.resolve(self).__repr__()
149 def __bytes__(self) -> bytes:
150 return DeferredOperand.resolve(self).__bytes__()
152 def __int__(self) -> int:
153 return int(DeferredOperand.resolve(self))
155 def __float__(self) -> float:
156 return float(DeferredOperand.resolve(self))
158 def __getattr__(self, method_name):
159 def method(*args, **kwargs):
160 return getattr(DeferredOperand.resolve(self), method_name)(*args, **kwargs)