Created
March 13, 2021 05:30
-
-
Save skshetry/68eccf53234bdff27c41586f0f655c0e to your computer and use it in GitHub Desktop.
Capturing tqdm instances
This file contains 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
import pytest | |
from funcy import wrap_with | |
import threading | |
class TdqmCapture: | |
def __init__(self): | |
self._instances = [] | |
@property | |
def instances(self): | |
return self._instances | |
def __enter__(self): | |
self.clear() | |
yield self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
pass | |
def clear(self): | |
self._instances.clear() | |
@wrap_with(threading.Lock()) | |
def add(self, obj): | |
self._instances.append(obj) | |
return obj | |
@pytest.fixture | |
def captqdm(mocker): | |
from dvc.progress import Tqdm | |
tqdm_og_init = Tqdm.__init__ | |
result = TdqmCapture() | |
def spy(obj, *args, **kwargs): | |
return result.add(tqdm_og_init(obj, *args, **kwargs)) | |
mocker.patch('dvc.progress.Tqdm.__init__', spy) | |
yield result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment