davidtran999 commited on
Commit
02848da
·
verified ·
1 Parent(s): e0413d9

Upload backend/venv/lib/python3.10/site-packages/proxytypes.py with huggingface_hub

Browse files
backend/venv/lib/python3.10/site-packages/proxytypes.py ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Based on the implementation here by Phillip J. Eby:
3
+ https://pypi.python.org/pypi/ProxyTypes
4
+ """
5
+
6
+ import operator
7
+ from functools import wraps
8
+
9
+ OPERATORS = [
10
+ # Unary
11
+ "pos",
12
+ "neg",
13
+ "abs",
14
+ "invert",
15
+ # Comparison
16
+ "eq",
17
+ "ne",
18
+ "lt",
19
+ "gt",
20
+ "le",
21
+ "ge",
22
+ # Container
23
+ "getitem",
24
+ "setitem",
25
+ "delitem",
26
+ "contains",
27
+ # In-place operators
28
+ "iadd",
29
+ "isub",
30
+ "imul",
31
+ "ifloordiv",
32
+ "itruediv",
33
+ "imod",
34
+ "ipow",
35
+ "ilshift",
36
+ "irshift",
37
+ "iand",
38
+ "ior",
39
+ "ixor",
40
+ ]
41
+ REFLECTED_OPERATORS = [
42
+ "add",
43
+ "sub",
44
+ "mul",
45
+ "floordiv",
46
+ "truediv",
47
+ "mod",
48
+ "pow",
49
+ "and",
50
+ "or",
51
+ "xor",
52
+ "lshift",
53
+ "rshift",
54
+ ]
55
+ # These functions all have magic methods named after them
56
+ MAGIC_FUNCS = [
57
+ divmod,
58
+ round,
59
+ repr,
60
+ str,
61
+ hash,
62
+ len,
63
+ abs,
64
+ complex,
65
+ bool,
66
+ int,
67
+ float,
68
+ iter,
69
+ bytes,
70
+ ]
71
+
72
+ _oga = object.__getattribute__
73
+ _osa = object.__setattr__
74
+
75
+
76
+ class ProxyMetaClass(type):
77
+ def __new__(mcs, name, bases, dct):
78
+ newcls = super(ProxyMetaClass, mcs).__new__(mcs, name, bases, dct)
79
+ newcls.__notproxied__ = set(dct.pop("__notproxied__", ()))
80
+ # Add all the non-proxied attributes from base classes
81
+ for base in bases:
82
+ if hasattr(base, "__notproxied__"):
83
+ newcls.__notproxied__.update(base.__notproxied__)
84
+ for key, val in dct.items():
85
+ setattr(newcls, key, val)
86
+ return newcls
87
+
88
+ def __setattr__(cls, attr, value):
89
+ # Don't do any magic on the methods of the base Proxy class or the
90
+ # __new__ static method
91
+ if cls.__bases__[0].__name__ == "_ProxyBase" or attr == "__new__":
92
+ pass
93
+ elif callable(value):
94
+ if getattr(value, "__notproxied__", False):
95
+ cls.__notproxied__.add(attr)
96
+ # Don't wrap staticmethods or classmethods
97
+ if not isinstance(value, (staticmethod, classmethod)):
98
+ value = cls._no_proxy(value)
99
+ elif isinstance(value, property):
100
+ if getattr(value.fget, "__notproxied__", False):
101
+ cls.__notproxied__.add(attr)
102
+ # Remake properties, with the underlying functions wrapped
103
+ fset = cls._no_proxy(value.fset) if value.fset else value.fset
104
+ fdel = cls._no_proxy(value.fdel) if value.fdel else value.fdel
105
+ value = property(cls._no_proxy(value.fget), fset, fdel)
106
+ super(ProxyMetaClass, cls).__setattr__(attr, value)
107
+
108
+ @staticmethod
109
+ def _no_proxy(method):
110
+ """
111
+ Returns a wrapped version of `method`, such that proxying is turned off
112
+ during the method call.
113
+
114
+ """
115
+
116
+ @wraps(method)
117
+ def wrapper(self, *args, **kwargs):
118
+ notproxied = _oga(self, "__notproxied__")
119
+ _osa(self, "__notproxied__", True)
120
+ try:
121
+ return method(self, *args, **kwargs)
122
+ finally:
123
+ _osa(self, "__notproxied__", notproxied)
124
+
125
+ return wrapper
126
+
127
+
128
+ # Since python 2 and 3 metaclass syntax aren't compatible, create an instance
129
+ # of our metaclass which our Proxy class can inherit from
130
+ _ProxyBase = ProxyMetaClass("_ProxyBase", (), {})
131
+
132
+
133
+ class Proxy(_ProxyBase):
134
+ """
135
+ Proxy for any python object. Base class for other proxies.
136
+
137
+ :attr:`__subject__` is the only non-proxied attribute, and contains the
138
+ proxied object
139
+
140
+ """
141
+
142
+ __notproxied__ = ("__subject__",)
143
+
144
+ def __init__(self, subject):
145
+ self.__subject__ = subject
146
+
147
+ @staticmethod
148
+ def _should_proxy(self, attr):
149
+ """
150
+ Determines whether `attr` should be looked up on the proxied object, or
151
+ the proxy itself.
152
+
153
+ """
154
+ if attr in type(self).__notproxied__:
155
+ return False
156
+ if _oga(self, "__notproxied__") is True:
157
+ return False
158
+ return True
159
+
160
+ def __getattribute__(self, attr):
161
+ if Proxy._should_proxy(self, attr):
162
+ return getattr(self.__subject__, attr)
163
+ return _oga(self, attr)
164
+
165
+ def __setattr__(self, attr, val):
166
+ if Proxy._should_proxy(self, attr):
167
+ setattr(self.__subject__, attr, val)
168
+ _osa(self, attr, val)
169
+
170
+ def __delattr__(self, attr):
171
+ if Proxy._should_proxy(self, attr):
172
+ delattr(self.__subject__, attr)
173
+ object.__delattr__(self, attr)
174
+
175
+ def __call__(self, *args, **kw):
176
+ return self.__subject__(*args, **kw)
177
+
178
+ @classmethod
179
+ def add_proxy_meth(cls, name, func, arg_pos=0):
180
+ """
181
+ Add a method `name` to the class, which returns the value of `func`,
182
+ called with the proxied value inserted at `arg_pos`
183
+
184
+ """
185
+
186
+ @wraps(func)
187
+ def proxied(self, *args, **kwargs):
188
+ args = list(args)
189
+ args.insert(arg_pos, self.__subject__)
190
+ result = func(*args, **kwargs)
191
+ return result
192
+
193
+ setattr(cls, name, proxied)
194
+
195
+
196
+ for func in MAGIC_FUNCS:
197
+ Proxy.add_proxy_meth("__%s__" % func.__name__, func)
198
+
199
+ for op in OPERATORS + REFLECTED_OPERATORS:
200
+ magic_meth = "__%s__" % op
201
+ Proxy.add_proxy_meth(magic_meth, getattr(operator, magic_meth))
202
+
203
+ # Reflected operators
204
+ for op in REFLECTED_OPERATORS:
205
+ Proxy.add_proxy_meth("__r%s__" % op, getattr(operator, "__%s__" % op), arg_pos=1)
206
+
207
+ # One offs
208
+ # Only non-operator that needs a reflected version
209
+ Proxy.add_proxy_meth("__rdivmod__", divmod, arg_pos=1)
210
+ # pypy is missing __index__ in operator module
211
+ Proxy.add_proxy_meth("__index__", operator.index)
212
+ # For python 2.6
213
+ Proxy.__nonzero__ = Proxy.__bool__
214
+
215
+
216
+ class CallbackProxy(Proxy):
217
+ """
218
+ Proxy for a callback result. Callback is called on each use.
219
+
220
+ """
221
+
222
+ def __init__(self, callback):
223
+ self.callback = callback
224
+
225
+ @property
226
+ def __subject__(self):
227
+ return self.callback()
228
+
229
+
230
+ class LazyProxy(CallbackProxy):
231
+ """
232
+ Proxy for a callback result, that is cached on first use.
233
+
234
+ """
235
+
236
+ @property
237
+ def __subject__(self):
238
+ try:
239
+ return self.cache
240
+ except AttributeError:
241
+ pass
242
+
243
+ self.cache = super(LazyProxy, self).__subject__
244
+ return self.cache
245
+
246
+ @__subject__.setter
247
+ def __subject__(self, value):
248
+ self.cache = value
249
+
250
+
251
+ def notproxied(func):
252
+ """
253
+ Decorator to add methods to the __notproxied__ list
254
+
255
+ """
256
+ func.__notproxied__ = True
257
+ return func