Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

slots.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import operator
  2. from .compat import PY2
  3. from .compat import PY3
  4. from .compat import with_metaclass
  5. from .utils import identity
  6. class _ProxyMethods(object):
  7. # We use properties to override the values of __module__ and
  8. # __doc__. If we add these in ObjectProxy, the derived class
  9. # __dict__ will still be setup to have string variants of these
  10. # attributes and the rules of descriptors means that they appear to
  11. # take precedence over the properties in the base class. To avoid
  12. # that, we copy the properties into the derived class type itself
  13. # via a meta class. In that way the properties will always take
  14. # precedence.
  15. @property
  16. def __module__(self):
  17. return self.__wrapped__.__module__
  18. @__module__.setter
  19. def __module__(self, value):
  20. self.__wrapped__.__module__ = value
  21. @property
  22. def __doc__(self):
  23. return self.__wrapped__.__doc__
  24. @__doc__.setter
  25. def __doc__(self, value):
  26. self.__wrapped__.__doc__ = value
  27. # We similar use a property for __dict__. We need __dict__ to be
  28. # explicit to ensure that vars() works as expected.
  29. @property
  30. def __dict__(self):
  31. return self.__wrapped__.__dict__
  32. # Need to also propagate the special __weakref__ attribute for case
  33. # where decorating classes which will define this. If do not define
  34. # it and use a function like inspect.getmembers() on a decorator
  35. # class it will fail. This can't be in the derived classes.
  36. @property
  37. def __weakref__(self):
  38. return self.__wrapped__.__weakref__
  39. class _ProxyMetaType(type):
  40. def __new__(cls, name, bases, dictionary):
  41. # Copy our special properties into the class so that they
  42. # always take precedence over attributes of the same name added
  43. # during construction of a derived class. This is to save
  44. # duplicating the implementation for them in all derived classes.
  45. dictionary.update(vars(_ProxyMethods))
  46. return type.__new__(cls, name, bases, dictionary)
  47. class Proxy(with_metaclass(_ProxyMetaType)):
  48. """
  49. A proxy implementation in pure Python, using slots. You can subclass this to add
  50. local methods or attributes, or enable __dict__.
  51. The most important internals:
  52. * ``__factory__`` is the callback that "materializes" the object we proxy to.
  53. * ``__target__`` will contain the object we proxy to, once it's "materialized".
  54. * ``__wrapped__`` is a property that does either:
  55. * return ``__target__`` if it's set.
  56. * calls ``__factory__``, saves result to ``__target__`` and returns said result.
  57. """
  58. __slots__ = '__target__', '__factory__'
  59. def __init__(self, factory):
  60. object.__setattr__(self, '__factory__', factory)
  61. @property
  62. def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__,
  63. __delattr__=object.__delattr__):
  64. try:
  65. return __getattr__(self, '__target__')
  66. except AttributeError:
  67. try:
  68. factory = __getattr__(self, '__factory__')
  69. except AttributeError:
  70. raise ValueError("Proxy hasn't been initiated: __factory__ is missing.")
  71. target = factory()
  72. __setattr__(self, '__target__', target)
  73. return target
  74. @__wrapped__.deleter
  75. def __wrapped__(self, __delattr__=object.__delattr__):
  76. __delattr__(self, '__target__')
  77. @__wrapped__.setter
  78. def __wrapped__(self, target, __setattr__=object.__setattr__):
  79. __setattr__(self, '__target__', target)
  80. @property
  81. def __name__(self):
  82. return self.__wrapped__.__name__
  83. @__name__.setter
  84. def __name__(self, value):
  85. self.__wrapped__.__name__ = value
  86. @property
  87. def __class__(self):
  88. return self.__wrapped__.__class__
  89. @__class__.setter
  90. def __class__(self, value):
  91. self.__wrapped__.__class__ = value
  92. @property
  93. def __annotations__(self):
  94. return self.__wrapped__.__anotations__
  95. @__annotations__.setter
  96. def __annotations__(self, value):
  97. self.__wrapped__.__annotations__ = value
  98. def __dir__(self):
  99. return dir(self.__wrapped__)
  100. def __str__(self):
  101. return str(self.__wrapped__)
  102. if PY3:
  103. def __bytes__(self):
  104. return bytes(self.__wrapped__)
  105. def __repr__(self, __getattr__=object.__getattribute__):
  106. try:
  107. target = __getattr__(self, '__target__')
  108. except AttributeError:
  109. return '<%s at 0x%x with factory %r>' % (
  110. type(self).__name__, id(self),
  111. self.__factory__
  112. )
  113. else:
  114. return '<%s at 0x%x wrapping %r at 0x%x with factory %r>' % (
  115. type(self).__name__, id(self),
  116. target, id(target),
  117. self.__factory__
  118. )
  119. def __reversed__(self):
  120. return reversed(self.__wrapped__)
  121. if PY3:
  122. def __round__(self):
  123. return round(self.__wrapped__)
  124. def __lt__(self, other):
  125. return self.__wrapped__ < other
  126. def __le__(self, other):
  127. return self.__wrapped__ <= other
  128. def __eq__(self, other):
  129. return self.__wrapped__ == other
  130. def __ne__(self, other):
  131. return self.__wrapped__ != other
  132. def __gt__(self, other):
  133. return self.__wrapped__ > other
  134. def __ge__(self, other):
  135. return self.__wrapped__ >= other
  136. def __hash__(self):
  137. return hash(self.__wrapped__)
  138. def __nonzero__(self):
  139. return bool(self.__wrapped__)
  140. def __bool__(self):
  141. return bool(self.__wrapped__)
  142. def __setattr__(self, name, value, __setattr__=object.__setattr__):
  143. if hasattr(type(self), name):
  144. __setattr__(self, name, value)
  145. else:
  146. setattr(self.__wrapped__, name, value)
  147. def __getattr__(self, name):
  148. if name in ('__wrapped__', '__factory__'):
  149. raise AttributeError(name)
  150. else:
  151. return getattr(self.__wrapped__, name)
  152. def __delattr__(self, name, __delattr__=object.__delattr__):
  153. if hasattr(type(self), name):
  154. __delattr__(self, name)
  155. else:
  156. delattr(self.__wrapped__, name)
  157. def __add__(self, other):
  158. return self.__wrapped__ + other
  159. def __sub__(self, other):
  160. return self.__wrapped__ - other
  161. def __mul__(self, other):
  162. return self.__wrapped__ * other
  163. def __div__(self, other):
  164. return operator.div(self.__wrapped__, other)
  165. def __truediv__(self, other):
  166. return operator.truediv(self.__wrapped__, other)
  167. def __floordiv__(self, other):
  168. return self.__wrapped__ // other
  169. def __mod__(self, other):
  170. return self.__wrapped__ ^ other
  171. def __divmod__(self, other):
  172. return divmod(self.__wrapped__, other)
  173. def __pow__(self, other, *args):
  174. return pow(self.__wrapped__, other, *args)
  175. def __lshift__(self, other):
  176. return self.__wrapped__ << other
  177. def __rshift__(self, other):
  178. return self.__wrapped__ >> other
  179. def __and__(self, other):
  180. return self.__wrapped__ & other
  181. def __xor__(self, other):
  182. return self.__wrapped__ ^ other
  183. def __or__(self, other):
  184. return self.__wrapped__ | other
  185. def __radd__(self, other):
  186. return other + self.__wrapped__
  187. def __rsub__(self, other):
  188. return other - self.__wrapped__
  189. def __rmul__(self, other):
  190. return other * self.__wrapped__
  191. def __rdiv__(self, other):
  192. return operator.div(other, self.__wrapped__)
  193. def __rtruediv__(self, other):
  194. return operator.truediv(other, self.__wrapped__)
  195. def __rfloordiv__(self, other):
  196. return other // self.__wrapped__
  197. def __rmod__(self, other):
  198. return other % self.__wrapped__
  199. def __rdivmod__(self, other):
  200. return divmod(other, self.__wrapped__)
  201. def __rpow__(self, other, *args):
  202. return pow(other, self.__wrapped__, *args)
  203. def __rlshift__(self, other):
  204. return other << self.__wrapped__
  205. def __rrshift__(self, other):
  206. return other >> self.__wrapped__
  207. def __rand__(self, other):
  208. return other & self.__wrapped__
  209. def __rxor__(self, other):
  210. return other ^ self.__wrapped__
  211. def __ror__(self, other):
  212. return other | self.__wrapped__
  213. def __iadd__(self, other):
  214. self.__wrapped__ += other
  215. return self
  216. def __isub__(self, other):
  217. self.__wrapped__ -= other
  218. return self
  219. def __imul__(self, other):
  220. self.__wrapped__ *= other
  221. return self
  222. def __idiv__(self, other):
  223. self.__wrapped__ = operator.idiv(self.__wrapped__, other)
  224. return self
  225. def __itruediv__(self, other):
  226. self.__wrapped__ = operator.itruediv(self.__wrapped__, other)
  227. return self
  228. def __ifloordiv__(self, other):
  229. self.__wrapped__ //= other
  230. return self
  231. def __imod__(self, other):
  232. self.__wrapped__ %= other
  233. return self
  234. def __ipow__(self, other):
  235. self.__wrapped__ **= other
  236. return self
  237. def __ilshift__(self, other):
  238. self.__wrapped__ <<= other
  239. return self
  240. def __irshift__(self, other):
  241. self.__wrapped__ >>= other
  242. return self
  243. def __iand__(self, other):
  244. self.__wrapped__ &= other
  245. return self
  246. def __ixor__(self, other):
  247. self.__wrapped__ ^= other
  248. return self
  249. def __ior__(self, other):
  250. self.__wrapped__ |= other
  251. return self
  252. def __neg__(self):
  253. return -self.__wrapped__
  254. def __pos__(self):
  255. return +self.__wrapped__
  256. def __abs__(self):
  257. return abs(self.__wrapped__)
  258. def __invert__(self):
  259. return ~self.__wrapped__
  260. def __int__(self):
  261. return int(self.__wrapped__)
  262. if PY2:
  263. def __long__(self):
  264. return long(self.__wrapped__) # flake8: noqa
  265. def __float__(self):
  266. return float(self.__wrapped__)
  267. def __oct__(self):
  268. return oct(self.__wrapped__)
  269. def __hex__(self):
  270. return hex(self.__wrapped__)
  271. def __index__(self):
  272. return operator.index(self.__wrapped__)
  273. def __len__(self):
  274. return len(self.__wrapped__)
  275. def __contains__(self, value):
  276. return value in self.__wrapped__
  277. def __getitem__(self, key):
  278. return self.__wrapped__[key]
  279. def __setitem__(self, key, value):
  280. self.__wrapped__[key] = value
  281. def __delitem__(self, key):
  282. del self.__wrapped__[key]
  283. def __getslice__(self, i, j):
  284. return self.__wrapped__[i:j]
  285. def __setslice__(self, i, j, value):
  286. self.__wrapped__[i:j] = value
  287. def __delslice__(self, i, j):
  288. del self.__wrapped__[i:j]
  289. def __enter__(self):
  290. return self.__wrapped__.__enter__()
  291. def __exit__(self, *args, **kwargs):
  292. return self.__wrapped__.__exit__(*args, **kwargs)
  293. def __iter__(self):
  294. return iter(self.__wrapped__)
  295. def __call__(self, *args, **kwargs):
  296. return self.__wrapped__(*args, **kwargs)
  297. def __reduce__(self):
  298. return identity, (self.__wrapped__,)
  299. def __reduce_ex__(self, protocol):
  300. return identity, (self.__wrapped__,)