| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2add352 commit 93fa608
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,111 @@ | |||
| 1 | + # Access WeakSet through the weakref module. | ||
| 2 | + # This code is separated-out because it is needed | ||
| 3 | + # by abc.py to load everything else at startup. | ||
| 4 | + | ||
| 5 | + from _weakref import ref | ||
| 6 | + | ||
| 7 | + __all__ = ['WeakSet'] | ||
| 8 | + | ||
| 9 | + class WeakSet: | ||
| 10 | + def __init__(self, data=None): | ||
| 11 | + self.data = set() | ||
| 12 | + def _remove(item, selfref=ref(self)): | ||
| 13 | + self = selfref() | ||
| 14 | + if self is not None: | ||
| 15 | + self.data.discard(item) | ||
| 16 | + self._remove = _remove | ||
| 17 | + if data is not None: | ||
| 18 | + self.update(data) | ||
| 19 | + | ||
| 20 | + def __iter__(self): | ||
| 21 | + for itemref in self.data: | ||
| 22 | + item = itemref() | ||
| 23 | + if item is not None: | ||
| 24 | + yield item | ||
| 25 | + | ||
| 26 | + def __contains__(self, item): | ||
| 27 | + return ref(item) in self.data | ||
| 28 | + | ||
| 29 | + def __reduce__(self): | ||
| 30 | + return (self.__class__, (list(self),), | ||
| 31 | + getattr(self, '__dict__', None)) | ||
| 32 | + | ||
| 33 | + def add(self, item): | ||
| 34 | + self.data.add(ref(item, self._remove)) | ||
| 35 | + | ||
| 36 | + def clear(self): | ||
| 37 | + self.data.clear() | ||
| 38 | + | ||
| 39 | + def copy(self): | ||
| 40 | + return self.__class__(self) | ||
| 41 | + | ||
| 42 | + def pop(self): | ||
| 43 | + while True: | ||
| 44 | + itemref = self.data.pop() | ||
| 45 | + item = itemref() | ||
| 46 | + if item is not None: | ||
| 47 | + return item | ||
| 48 | + | ||
| 49 | + def remove(self, item): | ||
| 50 | + self.data.remove(ref(item)) | ||
| 51 | + | ||
| 52 | + def discard(self, item): | ||
| 53 | + self.data.discard(ref(item)) | ||
| 54 | + | ||
| 55 | + def update(self, other): | ||
| 56 | + if isinstance(other, self.__class__): | ||
| 57 | + self.data.update(other.data) | ||
| 58 | + else: | ||
| 59 | + for element in other: | ||
| 60 | + self.add(element) | ||
| 61 | + __ior__ = update | ||
| 62 | + | ||
| 63 | + # Helper functions for simple delegating methods. | ||
| 64 | + def _apply(self, other, method): | ||
| 65 | + if not isinstance(other, self.__class__): | ||
| 66 | + other = self.__class__(other) | ||
| 67 | + newdata = method(other.data) | ||
| 68 | + newset = self.__class__() | ||
| 69 | + newset.data = newdata | ||
| 70 | + return newset | ||
| 71 | + | ||
| 72 | + def _apply_mutate(self, other, method): | ||
| 73 | + if not isinstance(other, self.__class__): | ||
| 74 | + other = self.__class__(other) | ||
| 75 | + method(other) | ||
| 76 | + | ||
| 77 | + def difference(self, other): | ||
| 78 | + return self._apply(other, self.data.difference) | ||
| 79 | + __sub__ = difference | ||
| 80 | + | ||
| 81 | + def difference_update(self, other): | ||
| 82 | + self._apply_mutate(self, self.data.difference_update) | ||
| 83 | + __isub__ = difference_update | ||
| 84 | + | ||
| 85 | + def intersection(self, other): | ||
| 86 | + return self._apply(other, self.data.intersection) | ||
| 87 | + __and__ = intersection | ||
| 88 | + | ||
| 89 | + def intersection_update(self, other): | ||
| 90 | + self._apply_mutate(self, self.data.intersection_update) | ||
| 91 | + __iand__ = intersection_update | ||
| 92 | + | ||
| 93 | + def issubset(self, other): | ||
| 94 | + return self.data.issubset(ref(item) for item in other) | ||
| 95 | + __lt__ = issubset | ||
| 96 | + | ||
| 97 | + def issuperset(self, other): | ||
| 98 | + return self.data.issuperset(ref(item) for item in other) | ||
| 99 | + __gt__ = issuperset | ||
| 100 | + | ||
| 101 | + def symmetric_difference(self, other): | ||
| 102 | + return self._apply(other, self.data.symmetric_difference) | ||
| 103 | + __xor__ = symmetric_difference | ||
| 104 | + | ||
| 105 | + def symmetric_difference_update(self, other): | ||
| 106 | + self._apply_mutate(other, self.data.symmetric_difference_update) | ||
| 107 | + __ixor__ = symmetric_difference_update | ||
| 108 | + | ||
| 109 | + def union(self, other): | ||
| 110 | + self._apply_mutate(other, self.data.union) | ||
| 111 | + __or__ = union | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | 4 | """Abstract Base Classes (ABCs) according to PEP 3119.""" | |
| 5 | 5 | ||
| 6 | - from weakref import WeakSet | ||
| 6 | + from _weakrefset import WeakSet | ||
| 7 | 7 | ||
| 8 | 8 | def abstractmethod(funcobj): | |
| 9 | 9 | """A decorator indicating abstract methods. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,12 +20,14 @@ | |||
| 20 | 20 | ProxyType, | |
| 21 | 21 | ReferenceType) | |
| 22 | 22 | ||
| 23 | + from _weakrefset import WeakSet | ||
| 23 | 24 | ||
| 24 | 25 | ProxyTypes = (ProxyType, CallableProxyType) | |
| 25 | 26 | ||
| 26 | 27 | __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs", | |
| 27 | 28 | "WeakKeyDictionary", "ReferenceType", "ProxyType", | |
| 28 | - "CallableProxyType", "ProxyTypes", "WeakValueDictionary"] | ||
| 29 | + "CallableProxyType", "ProxyTypes", "WeakValueDictionary", | ||
| 30 | + "WeakSet"] | ||
| 29 | 31 | ||
| 30 | 32 | ||
| 31 | 33 | class WeakValueDictionary(UserDict.UserDict): | |
@@ -337,108 +339,3 @@ def update(self, dict=None, **kwargs): | |||
| 337 | 339 | d[ref(key, self._remove)] = value | |
| 338 | 340 | if len(kwargs): | |
| 339 | 341 | self.update(kwargs) | |
| 340 | - | ||
| 341 | - | ||
| 342 | - class WeakSet: | ||
| 343 | - def __init__(self, data=None): | ||
| 344 | - self.data = set() | ||
| 345 | - def _remove(item, selfref=ref(self)): | ||
| 346 | - self = selfref() | ||
| 347 | - if self is not None: | ||
| 348 | - self.data.discard(item) | ||
| 349 | - self._remove = _remove | ||
| 350 | - if data is not None: | ||
| 351 | - self.update(data) | ||
| 352 | - | ||
| 353 | - def __iter__(self): | ||
| 354 | - for itemref in self.data: | ||
| 355 | - item = itemref() | ||
| 356 | - if item is not None: | ||
| 357 | - yield item | ||
| 358 | - | ||
| 359 | - def __contains__(self, item): | ||
| 360 | - return ref(item) in self.data | ||
| 361 | - | ||
| 362 | - def __reduce__(self): | ||
| 363 | - return (self.__class__, (list(self),), | ||
| 364 | - getattr(self, '__dict__', None)) | ||
| 365 | - | ||
| 366 | - def add(self, item): | ||
| 367 | - self.data.add(ref(item, self._remove)) | ||
| 368 | - | ||
| 369 | - def clear(self): | ||
| 370 | - self.data.clear() | ||
| 371 | - | ||
| 372 | - def copy(self): | ||
| 373 | - return self.__class__(self) | ||
| 374 | - | ||
| 375 | - def pop(self): | ||
| 376 | - while True: | ||
| 377 | - itemref = self.data.pop() | ||
| 378 | - item = itemref() | ||
| 379 | - if item is not None: | ||
| 380 | - return item | ||
| 381 | - | ||
| 382 | - def remove(self, item): | ||
| 383 | - self.data.remove(ref(item)) | ||
| 384 | - | ||
| 385 | - def discard(self, item): | ||
| 386 | - self.data.discard(ref(item)) | ||
| 387 | - | ||
| 388 | - def update(self, other): | ||
| 389 | - if isinstance(other, self.__class__): | ||
| 390 | - self.data.update(other.data) | ||
| 391 | - else: | ||
| 392 | - for element in other: | ||
| 393 | - self.add(element) | ||
| 394 | - __ior__ = update | ||
| 395 | - | ||
| 396 | - # Helper functions for simple delegating methods. | ||
| 397 | - def _apply(self, other, method): | ||
| 398 | - if not isinstance(other, self.__class__): | ||
| 399 | - other = self.__class__(other) | ||
| 400 | - newdata = method(other.data) | ||
| 401 | - newset = self.__class__() | ||
| 402 | - newset.data = newdata | ||
| 403 | - return newset | ||
| 404 | - | ||
| 405 | - def _apply_mutate(self, other, method): | ||
| 406 | - if not isinstance(other, self.__class__): | ||
| 407 | - other = self.__class__(other) | ||
| 408 | - method(other) | ||
| 409 | - | ||
| 410 | - def difference(self, other): | ||
| 411 | - return self._apply(other, self.data.difference) | ||
| 412 | - __sub__ = difference | ||
| 413 | - | ||
| 414 | - def difference_update(self, other): | ||
| 415 | - self._apply_mutate(self, self.data.difference_update) | ||
| 416 | - __isub__ = difference_update | ||
| 417 | - | ||
| 418 | - def intersection(self, other): | ||
| 419 | - return self._apply(other, self.data.intersection) | ||
| 420 | - __and__ = intersection | ||
| 421 | - | ||
| 422 | - def intersection_update(self, other): | ||
| 423 | - self._apply_mutate(self, self.data.intersection_update) | ||
| 424 | - __iand__ = intersection_update | ||
| 425 | - | ||
| 426 | - def issubset(self, other): | ||
| 427 | - return self.data.issubset(ref(item) for item in other) | ||
| 428 | - __lt__ = issubset | ||
| 429 | - | ||
| 430 | - def issuperset(self, other): | ||
| 431 | - return self.data.issuperset(ref(item) for item in other) | ||
| 432 | - __gt__ = issuperset | ||
| 433 | - | ||
| 434 | - def symmetric_difference(self, other): | ||
| 435 | - return self._apply(other, self.data.symmetric_difference) | ||
| 436 | - __xor__ = symmetric_difference | ||
| 437 | - | ||
| 438 | - def symmetric_difference_update(self, other): | ||
| 439 | - self._apply_mutate(other, self.data.symmetric_difference_update) | ||
| 440 | - __ixor__ = symmetric_difference_update | ||
| 441 | - | ||
| 442 | - def union(self, other): | ||
| 443 | - self._apply_mutate(other, self.data.union) | ||
| 444 | - __or__ = union | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments