Last active
December 10, 2016 18:10
-
-
Save anddam/3c4d63eff12e38c2acbb70b6001eb507 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [18]: %run semaphores.py | |
In [19]: working.size | |
Out[19]: 0 | |
In [20]: len(working) | |
Out[20]: 0 | |
In [21]: nonworking.size | |
Out[21]: 0 | |
In [22]: len(nonworking) | |
--------------------------------------------------------------------------- | |
TypeError Traceback (most recent call last) | |
<ipython-input-22-8b85c1cd9107> in <module>() | |
----> 1 len(nonworking) | |
TypeError: 'int' object is not callable | |
In [23]: test.size | |
Out[23]: 0 | |
In [24]: len(test) | |
Out[24]: 0 | |
In [25]: len(SemaphoreTest) | |
--------------------------------------------------------------------------- | |
TypeError Traceback (most recent call last) | |
<ipython-input-25-db83fa0370cc> in <module>() | |
----> 1 len(SemaphoreTest) | |
TypeError: object of type 'type' has no len() | |
In [26]: SemaphoreTest.__len__ | |
Out[26]: <function __main__.SemaphoreTest.size> | |
In [27]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import BoundedSemaphore | |
class SemaphoreWorking(BoundedSemaphore): | |
def _size(self): | |
return self._initial_value - self._value | |
size = property(_size) | |
__len__ = _size | |
class SemaphoreNonWorking(BoundedSemaphore): | |
@property | |
def size(self): | |
return self._initial_value - self._value | |
__len__ = size | |
class SemaphoreTest(BoundedSemaphore): | |
@property | |
def size(self): | |
return self._initial_value - self._value | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.__class__.__len__ = self.__class__.size.fget | |
N = 10 | |
working = SemaphoreWorking(N) | |
nonworking = SemaphoreNonWorking(N) | |
test = SemaphoreTest(N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment