3 from abc import ABC, abstractmethod
4 from typing import Any, Generic, TypeVar
6 # This module is commonly used by others in here and should avoid
7 # taking any unnecessary dependencies back on them.
12 class DeferredOperand(ABC, Generic[T]):
13 """A wrapper around an operand whose value is deferred until it is
14 needed. See subclass SmartFuture for an example usage.
18 def _resolve(self) -> T:
22 def resolve(x: Any) -> Any:
23 while isinstance(x, DeferredOperand):
27 def __lt__(self, other: Any) -> bool:
28 return DeferredOperand.resolve(self) < DeferredOperand.resolve(other)
30 def __le__(self, other: Any) -> bool:
31 return DeferredOperand.resolve(self) <= DeferredOperand.resolve(other)
33 def __eq__(self, other: Any) -> bool:
34 return DeferredOperand.resolve(self) == DeferredOperand.resolve(other)
36 def __ne__(self, other: Any) -> bool:
37 return DeferredOperand.resolve(self) != DeferredOperand.resolve(other)
39 def __gt__(self, other: Any) -> bool:
40 return DeferredOperand.resolve(self) > DeferredOperand.resolve(other)
42 def __ge__(self, other: Any) -> bool:
43 return DeferredOperand.resolve(self) >= DeferredOperand.resolve(other)
45 def __not__(self) -> bool:
46 return not DeferredOperand.resolve(self)
48 def bool(self) -> bool:
49 return DeferredOperand.resolve(self)
51 def __add__(self, other: Any) -> T:
52 return DeferredOperand.resolve(self) + DeferredOperand.resolve(other)
54 def __iadd__(self, other: Any) -> T:
55 return DeferredOperand.resolve(self) + DeferredOperand.resolve(other)
57 def __radd__(self, other: Any) -> T:
58 return DeferredOperand.resolve(self) + DeferredOperand.resolve(other)
60 def __sub__(self, other: Any) -> T:
61 return DeferredOperand.resolve(self) - DeferredOperand.resolve(other)
63 def __mul__(self, other: Any) -> T:
64 return DeferredOperand.resolve(self) * DeferredOperand.resolve(other)
66 def __pow__(self, other: Any) -> T:
67 return DeferredOperand.resolve(self) ** DeferredOperand.resolve(other)
69 def __truediv__(self, other: Any) -> Any:
70 return DeferredOperand.resolve(self) / DeferredOperand.resolve(other)
72 def __floordiv__(self, other: Any) -> T:
73 return DeferredOperand.resolve(self) // DeferredOperand.resolve(other)
75 def __contains__(self, other):
76 return DeferredOperand.resolve(other) in DeferredOperand.resolve(self)
78 def and_(self, other):
79 return DeferredOperand.resolve(self) & DeferredOperand.resolve(other)
82 return DeferredOperand.resolve(self) & DeferredOperand.resolve(other)
85 return DeferredOperand.resolve(self) & DeferredOperand.resolve(other)
88 return ~(DeferredOperand.resolve(self))
91 return DeferredOperand.resolve(self) is DeferredOperand.resolve(other)
93 def is_not(self, other):
94 return DeferredOperand.resolve(self) is not DeferredOperand.resolve(other)
97 return abs(DeferredOperand.resolve(self))
99 def setitem(self, k, v):
100 DeferredOperand.resolve(self)[DeferredOperand.resolve(k)] = v
102 def delitem(self, k):
103 del DeferredOperand.resolve(self)[DeferredOperand.resolve(k)]
105 def getitem(self, k):
106 return DeferredOperand.resolve(self)[DeferredOperand.resolve(k)]
108 def lshift(self, other):
109 return DeferredOperand.resolve(self) << DeferredOperand.resolve(other)
111 def rshift(self, other):
112 return DeferredOperand.resolve(self) >> DeferredOperand.resolve(other)
114 def mod(self, other):
115 return DeferredOperand.resolve(self) % DeferredOperand.resolve(other)
117 def matmul(self, other):
118 return DeferredOperand.resolve(self) @ DeferredOperand.resolve(other)
121 return -(DeferredOperand.resolve(self))
124 return +(DeferredOperand.resolve(self))
127 return DeferredOperand.resolve(self)
130 return DeferredOperand.resolve(self).__hash__()
133 return DeferredOperand.resolve(self)()
136 return DeferredOperand.resolve(self).__iter__()
138 def __repr__(self) -> str:
139 return DeferredOperand.resolve(self).__repr__()
141 def __bytes__(self) -> bytes:
142 return DeferredOperand.resolve(self).__bytes__()
144 def __int__(self) -> int:
145 return int(DeferredOperand.resolve(self))
147 def __float__(self) -> float:
148 return float(DeferredOperand.resolve(self))
150 def __getattr__(self, method_name):
151 def method(*args, **kwargs):
152 return getattr(DeferredOperand.resolve(self), method_name)(