mirror of https://github.com/tqdm/tqdm.git
add efficient `__contains__`
This commit is contained in:
parent
fc69d5dcf5
commit
983b66c132
|
@ -1973,3 +1973,14 @@ def test_closed():
|
|||
for i in trange(9, file=our_file, miniters=1, mininterval=0):
|
||||
if i == 5:
|
||||
our_file.close()
|
||||
|
||||
|
||||
def test_contains(capsys):
|
||||
"""Test __contains__ doesn't iterate"""
|
||||
with tqdm(_range(9)) as t:
|
||||
assert 9 not in t
|
||||
assert all(i in t for i in range(9))
|
||||
out, err = capsys.readouterr()
|
||||
assert not out
|
||||
assert ' 0%' in err
|
||||
assert '100%' not in err
|
||||
|
|
|
@ -1131,6 +1131,10 @@ class tqdm(Comparable):
|
|||
else self.iterable.__length_hint__() if hasattr(self.iterable, "__length_hint__")
|
||||
else getattr(self, "total", None))
|
||||
|
||||
def __contains__(self, item):
|
||||
contains = getattr(self.iterable, '__contains__', None)
|
||||
return contains(item) if contains is not None else item in self.__iter__()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
|
|
Loading…
Reference in New Issue