>>> states [True, False] >>> states = [] >>> with printer_lock: ... states.append(bool(printer_lock.locked())) ... # Critical section - print stuff here. >>> states.append(bool(printer_lock.locked())) >>> states [True, False] ''' self.release() def __repr__(self): return '<{} key={} value={} timeout={}>'.format( self.__class__.__name__, self.key, self._value, self.locked(), ) if __name__ == '__main__': # pragma: no cover # Run the doctests in this module with: # $ source venv/bin/activate # $ python3 -m pottery.redlock # $ deactivate with contextlib.suppress(ImportError): from tests.base import run_doctests run_doctests()
Please note that this method returns an approximation, not an exact value. So please don't rely on it for anything important like financial systems or cat gif websites. More about the formula that this method implements: https://en.wikipedia.org/wiki/Bloom_filter#Approximating_the_number_of_items_in_a_Bloom_filter ''' len_ = ( -self.size() / self.num_hashes() * math.log(1 - self._num_bits_set() / self.size()) ) return math.floor(len_) def __repr__(self): 'Return the string representation of a BloomFilter. O(1)' return '<{} key={}>'.format(self.__class__.__name__, self.key) if __name__ == '__main__': # pragma: no cover # Run the doctests in this module with: # $ source venv/bin/activate # $ python3 -m pottery.bloom # $ deactivate import contextlib with contextlib.suppress(ImportError): from tests.base import run_doctests run_doctests()